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..a31cbab --- /dev/null +++ b/src/main/java/org/ntlab/acanthus_server/entities/FollowAddJson.java @@ -0,0 +1,33 @@ +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..dfd8d84 --- /dev/null +++ b/src/main/java/org/ntlab/acanthus_server/entities/FollowUidJson.java @@ -0,0 +1,26 @@ +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 index 93f59bd..ca37ded 100644 --- a/src/main/java/org/ntlab/acanthus_server/resources/accounts/FollowsRest.java +++ b/src/main/java/org/ntlab/acanthus_server/resources/accounts/FollowsRest.java @@ -1,7 +1,6 @@ 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.entities.*; import org.ntlab.acanthus_server.models.Accounts; import org.springframework.stereotype.Component; @@ -21,17 +20,13 @@ @Path("/{uid}/follows") @GET @Produces(MediaType.APPLICATION_JSON) - public Collection getFollows(@PathParam("uid") Integer uid){ + public FollowUidJson 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)); - } + if (account != null) {//アカウント認証 + Collection followUidList = account.getFollowsHashMap().keySet(); - return followJsonList; + return new FollowUidJson(followUidList); }else { var response = Response.status(401).entity("ユーザーIDが間違っています"); throw new WebApplicationException(response.build()); @@ -43,14 +38,22 @@ @Path("/{uid}/follows") @POST @Produces(MediaType.APPLICATION_JSON) - public void addFollows(@PathParam("uid") Integer uid, @FormParam("token") String token, @FormParam("followUid") Integer followUid){ + 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) { - account.addFollowsHashMap(followAccount); + if (account != null && account.getToken().equals(token)){//アカウント認証及びトークン認証 + if (followAccount != null){//フォローするアカウントの認証 + if (account.getFollowsHashMap().get(followUid) == null) {//既にフォローしているユーザーか確認 + if (uid != followUid){//誤って自分自身をフォローしているか確認 + account.addFollowsHashMap(followAccount); + + + 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()); @@ -72,12 +75,13 @@ @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); + var followAccount = account.getFollowsHashMap().get(followUid);//フォロー内アカウント参照 if (account != null && account.getToken().equals(token)){ - if (followAccount != null){ + if (followAccount != null){//フォロー内にユーザーが登録されているか確認 account.getFollowsHashMap().remove(followUid); + return "フォローを解除しました"; }else { var response = Response.status(401).entity("フォローしているユーザーでないかユーザーIDが間違っています");