diff --git a/src/main/java/org/ntlab/amaryllis/server/entities/Account.java b/src/main/java/org/ntlab/amaryllis/server/entities/Account.java index 424e4e9..3791906 100644 --- a/src/main/java/org/ntlab/amaryllis/server/entities/Account.java +++ b/src/main/java/org/ntlab/amaryllis/server/entities/Account.java @@ -4,6 +4,8 @@ import org.ntlab.amaryllis.server.models.Accounts; import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; import java.util.UUID; public class Account { @@ -14,8 +16,8 @@ @JsonIgnore private String password; private String introduction; - private ArrayList followings; - private ArrayList followers; + private HashSet followings; + private HashSet followers; private ArrayList contributions; private ArrayList history; @@ -23,8 +25,8 @@ this.name = name; this.password = password; this.uid = UUID.randomUUID().toString(); - followings = new ArrayList<>(); - followers = new ArrayList<>(); + followings = new HashSet<>(); + followers = new HashSet<>(); history = new ArrayList<>(); contributions = new ArrayList<>(); introduction = ""; @@ -52,11 +54,11 @@ return contributions; } - public ArrayList getFollowers() { + public HashSet getFollowers() { return followers; } - public ArrayList getFollowings() { + public HashSet getFollowings() { return followings; } @@ -76,4 +78,14 @@ this.token = token; } + public void setName(String name) { + this.name = name; + } + public void setPassword(String password){ + this.password=password; + } + + public void setIntroduction(String introduction) { + this.introduction = introduction; + } } diff --git a/src/main/java/org/ntlab/amaryllis/server/models/Accounts.java b/src/main/java/org/ntlab/amaryllis/server/models/Accounts.java index 02c3b62..b468076 100644 --- a/src/main/java/org/ntlab/amaryllis/server/models/Accounts.java +++ b/src/main/java/org/ntlab/amaryllis/server/models/Accounts.java @@ -6,11 +6,12 @@ import javax.ws.rs.WebApplicationException; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; +import java.util.List; public class Accounts { private static Accounts theInstance = null; private HashMap accountHashMap=new HashMap<>(); - public static Accounts getInstance() { if (theInstance == null) { theInstance = new Accounts(); @@ -24,10 +25,14 @@ } public Account getAccount(String uid) { - return accountHashMap.get(uid); - } + + /** + *名前でアカウントを検索するメソッド + * @param name 検索したいアカウントのname + * @return 同じnameを持つアカウント + */ public Account getAccountByName(String name){ for(String key : accountHashMap.keySet()){ @@ -37,6 +42,11 @@ return null; } + /** + *同じ名前のアカウントが既に登録されているかを判定するメソッド + * @param name 判定したいアカウントのname + * @return 同じnameを持つアカウントが既に登録されていればtrue + */ public boolean isRegisteredName(String name) { for(String key : accountHashMap.keySet()){ Account a=accountHashMap.get(key); @@ -44,6 +54,7 @@ } return false; } + /** *パスワードが一致しているかを判定するメソッド * @param uid 判定したいアカウントのuid @@ -53,6 +64,7 @@ public boolean matchesPassword(String uid,String password){ return getAccount(uid).getPassword().equals(password); } + /** *トークンが一致しているかを判定するメソッド. * (passwordを必要としない通信用のメソッド) @@ -63,6 +75,7 @@ public boolean matchesToken(String uid,String token){ return getAccount(uid).getToken().equals(token); } + /** *メソッドに渡されたuidが同一かを判定するメソッド. * (自分自身をフォローしないようにする為に実装しました) @@ -89,13 +102,21 @@ addAccount(newAccount); return newAccount; } - + /** + *アカウントの削除時に実行するメソッド. + * 削除前にすべてのfollowingsに対してフォロー解除を行う + * @param uid 削除するアカウントのuid + */ public void removeAccount(String uid) { + + for(String followUid:getAccount(uid).getFollowings()){ + getAccount(followUid).getFollowers().remove(uid); + } accountHashMap.remove(uid); } /** - *誰かをフォローした時に実行するメソッド. + *誰かをフォローする時に実行するメソッド. * @param fromUid フォローを実行したアカウントのuid * @param toUid  フォローされたアカウントのuid */ @@ -105,7 +126,7 @@ } /** - *誰かのフォローを外した時に実行するメソッド. + *誰かのフォローを外す時に実行するメソッド. * @param fromUid フォロー外しを実行したアカウントのuid * @param toUid  フォローを外されたアカウントのuid */ diff --git a/src/main/java/org/ntlab/amaryllis/server/resources/AccountsRest.java b/src/main/java/org/ntlab/amaryllis/server/resources/AccountsRest.java index 164aa92..9293ea4 100644 --- a/src/main/java/org/ntlab/amaryllis/server/resources/AccountsRest.java +++ b/src/main/java/org/ntlab/amaryllis/server/resources/AccountsRest.java @@ -120,12 +120,15 @@ } throw new WebApplicationException(400); } + @Path("/{uid}/followings") @GET @Produces(MediaType.APPLICATION_JSON) - public ArrayList getFollowings(@PathParam("uid")String uid){ + public HashSet getFollowings(@PathParam("uid")String uid){ return getAccount(uid).getFollowings(); + } + @Path("/{uid}/followings") @POST @Produces(MediaType.APPLICATION_JSON) @@ -136,6 +139,7 @@ } throw new WebApplicationException(400); } + @Path("/{uid}/followings") @DELETE @Produces(MediaType.APPLICATION_JSON) @@ -146,4 +150,53 @@ } throw new WebApplicationException(400); } + + @Path("/{uid}/name") + @GET + @Produces(MediaType.APPLICATION_JSON) + public String getName(@PathParam("uid")String uid){ + return accounts.getAccount(uid).getName(); + } + + @Path("/{uid}/name") + @PUT + @Produces(MediaType.APPLICATION_JSON) + public MessageJson changeName(@PathParam("uid")String uid,@FormParam("token")String token,@FormParam("new_name")String newName){ + if(accounts.matchesToken(uid,token)){ + accounts.getAccount(uid).setName(newName); + return new MessageJson("success"); + } + throw new WebApplicationException(400); + } + + + @Path("/{uid}/password") + @PUT + @Produces(MediaType.APPLICATION_JSON) + public MessageJson changePassword(@PathParam("uid")String uid,@FormParam("password")String password,@FormParam("new_password")String newPassword){ + if(accounts.matchesPassword(uid,password)){ + accounts.getAccount(uid).setPassword(newPassword); + return new MessageJson("success"); + } + throw new WebApplicationException(400); + } + + @Path("/{uid}/introduction") + @PUT + @Produces(MediaType.APPLICATION_JSON) + public MessageJson changeIntroduction(@PathParam("uid")String uid,@FormParam("token")String token,@FormParam("new_introduction")String newIntroduction){ + if(accounts.matchesToken(uid,token)){ + accounts.getAccount(uid).setIntroduction(newIntroduction); + return new MessageJson("success"); + } + throw new WebApplicationException(400); + } + + @Path("/{uid}/history") + @GET + @Produces(MediaType.APPLICATION_JSON) + public ArrayList getHistory(@PathParam("uid")String uid){ + return accounts.getAccount(uid).getHistory(); + } + } \ No newline at end of file