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..7be6334 100644 --- a/src/main/java/org/ntlab/acanthus_server/entities/Account.java +++ b/src/main/java/org/ntlab/acanthus_server/entities/Account.java @@ -46,6 +46,13 @@ @JsonProperty("work") private HashMap workHashMap = new HashMap<>(); + //@JsonProperty("follows") + @JsonIgnore //アカウント情報取得時のループ対策 + private HashMap followsHashMap = new HashMap<>(); + //@JsonProperty("followers") + @JsonIgnore + private HashMap followersHashMap =new HashMap<>(); + @JsonIgnore private Integer uid; @JsonIgnore @@ -97,6 +104,16 @@ this.workHashMap.put(aid, work); } + public void addFollowsHashMap(Account account) { + var uid = account.getUid(); + this.followsHashMap.put(uid, account); + } + + public void addFollowersHashMap(Account account) { + var uid = account.getUid(); + this.followersHashMap.put(uid, account); + } + //----------------------------------------------------------------- // getter public Integer getUid() { @@ -124,6 +141,14 @@ return this.workHashMap; } + public HashMap getFollowsHashMap() { + return this.followsHashMap; + } + + public HashMap getFollowersHashMap() { + return this.followersHashMap; + } + //----------------------------------------------------------------- // 招待されている作品を返す @JsonIgnore diff --git a/src/main/java/org/ntlab/acanthus_server/entities/FollowAddJson.java b/src/main/java/org/ntlab/acanthus_server/entities/FollowAddJson.java new file mode 100644 index 0000000..741246c --- /dev/null +++ b/src/main/java/org/ntlab/acanthus_server/entities/FollowAddJson.java @@ -0,0 +1,34 @@ +package org.ntlab.acanthus_server.entities; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class FollowAddJson { + @JsonProperty("message") + private String message = "Follow registration success"; + @JsonProperty("uid") + private Integer uid; + @JsonProperty("followUid") + private Integer followUid; + + public FollowAddJson(Account account, Account followAccount){ + this.uid = account.getUid(); + this.followUid = followAccount.getUid(); + } + + public Integer getUid() { + return uid; + } + + public Integer getFollowUid() { + return followUid; + } + + public void setUid(Integer uid) { + this.uid = uid; + } + + public void setFollowUid(Integer followUid) { + this.followUid = followUid; + } + +} diff --git a/src/main/java/org/ntlab/acanthus_server/entities/FollowUidJson.java b/src/main/java/org/ntlab/acanthus_server/entities/FollowUidJson.java new file mode 100644 index 0000000..995d5ac --- /dev/null +++ b/src/main/java/org/ntlab/acanthus_server/entities/FollowUidJson.java @@ -0,0 +1,27 @@ +package org.ntlab.acanthus_server.entities; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.ArrayList; +import java.util.Collection; + +public class FollowUidJson { + @JsonProperty("followsUid") + private Collection uidList = new ArrayList<>(); + + //Json型でフォローしているユーザーIDを表示する処理 + public FollowUidJson(Collection followUidList){ + for (var followUid : followUidList){ + uidList.add(followUid); + } + } + + public Collection getUidList() { + return uidList; + } + + public void setUidList(Collection uidList) { + this.uidList = uidList; + } + +} 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..b36033b --- /dev/null +++ b/src/main/java/org/ntlab/acanthus_server/resources/accounts/FollowsRest.java @@ -0,0 +1,95 @@ +package org.ntlab.acanthus_server.resources.accounts; + +import org.ntlab.acanthus_server.entities.*; +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.Collection; + +@Component +@Path("/accounts") +public class FollowsRest { + private Accounts accounts = Accounts.getInstance(); + + //アカウントの別のフォロー情報を取得するメソッド + + @Path("/{uid}/follows") + @GET + @Produces(MediaType.APPLICATION_JSON) + public FollowUidJson getFollows(@PathParam("uid") Integer uid){ + var account = accounts.getAccountByUid(uid);//ユーザー情報取得 + + if (account != null) {//アカウント認証 + Collection followUidList = account.getFollowsHashMap().keySet(); + + return new FollowUidJson(followUidList); + }else { + var response = Response.status(401).entity("ユーザーIDが間違っています"); + throw new WebApplicationException(response.build()); + } + } + + //ユーザーをフォローするメソッド + + @Path("/{uid}/follows") + @POST + @Produces(MediaType.APPLICATION_JSON) + public FollowAddJson 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) {//既にフォローしているユーザーか確認 + if (uid != followUid){//誤って自分自身をフォローしているか確認 + account.addFollowsHashMap(followAccount); + followAccount.addFollowersHashMap(account); + + return new FollowAddJson(account, followAccount); + }else { + var response = Response.status(401).entity("自分自身をフォロー出来ません"); + throw new WebApplicationException(response.build()); + } + }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); + followAccount.getFollowersHashMap().remove(uid); + + 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()); + } + } + +}