diff --git a/src/main/java/org/ntlab/acanthus_server/entities/Account.java b/src/main/java/org/ntlab/acanthus_server/entities/Account.java index 1b17fd1..225f6df 100644 --- a/src/main/java/org/ntlab/acanthus_server/entities/Account.java +++ b/src/main/java/org/ntlab/acanthus_server/entities/Account.java @@ -45,6 +45,8 @@ private String email; @JsonProperty("work") private HashMap workHashMap = new HashMap<>(); + @JsonProperty("follows") + private HashMap followsHashMap = new HashMap<>(); @JsonIgnore private Integer uid; @@ -97,6 +99,11 @@ this.workHashMap.put(aid, work); } + public void addFollowsHashMap(Account account) { + var uid = account.getUid(); + this.followsHashMap.put(uid, account); + } + //----------------------------------------------------------------- // getter public Integer getUid() { @@ -124,6 +131,10 @@ return this.workHashMap; } + public HashMap getFollowsHashMap() { + return this.followsHashMap; + } + //----------------------------------------------------------------- // 招待されている作品を返す @JsonIgnore diff --git a/src/main/java/org/ntlab/acanthus_server/resources/accounts/FollowsRest.java b/src/main/java/org/ntlab/acanthus_server/resources/accounts/FollowsRest.java new file mode 100644 index 0000000..93f59bd --- /dev/null +++ b/src/main/java/org/ntlab/acanthus_server/resources/accounts/FollowsRest.java @@ -0,0 +1,92 @@ +package org.ntlab.acanthus_server.resources.accounts; + +import org.ntlab.acanthus_server.entities.Account; +import org.ntlab.acanthus_server.entities.AccountJson; +import org.ntlab.acanthus_server.models.Accounts; +import org.springframework.stereotype.Component; + +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.util.ArrayList; +import java.util.Collection; + +@Component +@Path("/accounts") +public class FollowsRest { + private Accounts accounts = Accounts.getInstance(); + + //アカウントの別のフォロー情報を取得するメソッド + + @Path("/{uid}/follows") + @GET + @Produces(MediaType.APPLICATION_JSON) + public Collection getFollows(@PathParam("uid") Integer uid){ + var account = accounts.getAccountByUid(uid);//ユーザー情報取得 + var followJsonList = new ArrayList(); + + if (account != null) { + Collection followList = account.getFollowsHashMap().values(); + for (var follow : followList) { + followJsonList.add(new AccountJson(follow)); + } + + return followJsonList; + }else { + var response = Response.status(401).entity("ユーザーIDが間違っています"); + throw new WebApplicationException(response.build()); + } + } + + //ユーザーをフォローするメソッド + + @Path("/{uid}/follows") + @POST + @Produces(MediaType.APPLICATION_JSON) + public void addFollows(@PathParam("uid") Integer uid, @FormParam("token") String token, @FormParam("followUid") Integer followUid){ + var account = accounts.getAccountByUid(uid); + var followAccount = accounts.getAccountByUid(followUid); + + if (account != null && account.getToken().equals(token)){ + if (followAccount != null){ + if (account.getFollowsHashMap().get(followUid) == null) { + account.addFollowsHashMap(followAccount); + }else { + var response = Response.status(401).entity("そのユーザーは既にフォローしています"); + throw new WebApplicationException(response.build()); + } + }else { + var response = Response.status(401).entity("フォローするユーザーが存在しないかユーザーIDが間違っています"); + throw new WebApplicationException(response.build()); + } + }else { + var response = Response.status(401).entity("ユーザーIDまたはトークンが間違っています"); + throw new WebApplicationException(response.build()); + } + } + + //ユーザーのフォローを解除するメソッド + + @Path("/{uid}/follows") + @DELETE + @Produces(MediaType.APPLICATION_JSON) + public String releaseFollows(@PathParam("uid") Integer uid, @FormParam("token") String token, @FormParam("followUid") Integer followUid){ + var account = accounts.getAccountByUid(uid); + var followAccount = account.getFollowsHashMap().get(followUid); + + if (account != null && account.getToken().equals(token)){ + if (followAccount != null){ + account.getFollowsHashMap().remove(followUid); + + return "フォローを解除しました"; + }else { + var response = Response.status(401).entity("フォローしているユーザーでないかユーザーIDが間違っています"); + throw new WebApplicationException(response.build()); + } + }else { + var response = Response.status(401).entity("ユーザーIDまたはトークンが間違っています"); + throw new WebApplicationException(response.build()); + } + } + +}