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 c5d5fff..424e4e9 100644 --- a/src/main/java/org/ntlab/amaryllis/server/entities/Account.java +++ b/src/main/java/org/ntlab/amaryllis/server/entities/Account.java @@ -1,6 +1,7 @@ package org.ntlab.amaryllis.server.entities; import com.fasterxml.jackson.annotation.JsonIgnore; +import org.ntlab.amaryllis.server.models.Accounts; import java.util.ArrayList; import java.util.UUID; @@ -30,6 +31,8 @@ } public void setUid(String uid) { + Accounts.getInstance().getMap().remove(getUid()); + Accounts.getInstance().getMap().put(uid,this); this.uid = uid; } diff --git a/src/main/java/org/ntlab/amaryllis/server/entities/LoginJson.java b/src/main/java/org/ntlab/amaryllis/server/entities/LoginJson.java index 2ee4688..d184062 100644 --- a/src/main/java/org/ntlab/amaryllis/server/entities/LoginJson.java +++ b/src/main/java/org/ntlab/amaryllis/server/entities/LoginJson.java @@ -2,7 +2,7 @@ import java.util.UUID; -public class LoginJson extends AbstractJson { +public class LoginJson { private String message; private String token; 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 3eb5348..02c3b62 100644 --- a/src/main/java/org/ntlab/amaryllis/server/models/Accounts.java +++ b/src/main/java/org/ntlab/amaryllis/server/models/Accounts.java @@ -3,12 +3,12 @@ import com.fasterxml.jackson.annotation.JsonProperty; import org.ntlab.amaryllis.server.entities.Account; +import javax.ws.rs.WebApplicationException; import java.util.ArrayList; import java.util.HashMap; public class Accounts { private static Accounts theInstance = null; - private ArrayList accounts = new ArrayList(); private HashMap accountHashMap=new HashMap<>(); public static Accounts getInstance() { @@ -24,33 +24,62 @@ } public Account getAccount(String uid) { - for (Account a : accounts) { - if (a.getUid().equals(uid)) return a; - } - return null; + + return accountHashMap.get(uid); + } public Account getAccountByName(String name){ - for (Account a : accounts) { - if (a.getName().equals(name)) return a; + + for(String key : accountHashMap.keySet()){ + Account a=accountHashMap.get(key); + if(a.getName().equals(name))return a; } return null; - } public boolean isRegisteredName(String name) { - - for (Account a : accounts) { - if (a.getName().equals(name)) return true; + for(String key : accountHashMap.keySet()){ + Account a=accountHashMap.get(key); + if(a.getName().equals(name))return true; } return false; } + /** + *パスワードが一致しているかを判定するメソッド + * @param uid 判定したいアカウントのuid + * @param password 入力されたパスワード + * @return パスワードが一致していればtrue + */ + public boolean matchesPassword(String uid,String password){ + return getAccount(uid).getPassword().equals(password); + } + /** + *トークンが一致しているかを判定するメソッド. + * (passwordを必要としない通信用のメソッド) + * @param uid 判定したいアカウントのuid + * @param token 入力されたトークン + * @return トークンが一致していればtrue + */ + public boolean matchesToken(String uid,String token){ + return getAccount(uid).getToken().equals(token); + } + /** + *メソッドに渡されたuidが同一かを判定するメソッド. + * (自分自身をフォローしないようにする為に実装しました) + * @param uid あるアカウントのuid + * @param targetUid 対象のuid + * @return uidが同一であればtrue + */ + public boolean isSameUid(String uid,String targetUid){ + return getAccount(uid).equals(targetUid); + } - public ArrayList getList() { - return accounts; + + public HashMap getMap(){ + return accountHashMap; } public void addAccount(Account account) { - accounts.add(account); accountHashMap.put(account.getUid(),account); } @@ -62,13 +91,29 @@ } public void removeAccount(String uid) { - int index=0; - for(Account a:accounts){ - if(a.getUid().equals(uid))accounts.remove(index); - index++; - } accountHashMap.remove(uid); } + /** + *誰かをフォローした時に実行するメソッド. + * @param fromUid フォローを実行したアカウントのuid + * @param toUid  フォローされたアカウントのuid + */ + public void followAccount(String fromUid,String toUid){ + getAccount(fromUid).getFollowings().add(toUid); + getAccount(toUid).getFollowers().add(fromUid); + } + + /** + *誰かのフォローを外した時に実行するメソッド. + * @param fromUid フォロー外しを実行したアカウントのuid + * @param toUid  フォローを外されたアカウントのuid + */ + public void unfollowAccount(String fromUid,String toUid){ + getAccount(fromUid).getFollowings().remove(toUid); + getAccount(toUid).getFollowers().remove(fromUid); + } + + } 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 7903db8..164aa92 100644 --- a/src/main/java/org/ntlab/amaryllis/server/resources/AccountsRest.java +++ b/src/main/java/org/ntlab/amaryllis/server/resources/AccountsRest.java @@ -25,21 +25,20 @@ private Accounts accounts = Accounts.getInstance(); AccountsRest() { - /** * テスト用 * */ { - createAccount("name", "pass"); - Account a = accounts.getList().get(0); - a.setUid("test"); + SignUpJson sj=createAccount("name", "pass"); + accounts.getAccountByName("name").setUid("test"); + } } @GET @Produces(MediaType.APPLICATION_JSON) - public ArrayList getAccounts() { - return accounts.getList(); + public Collection getAccounts() { + return accounts.getMap().values(); } @POST @@ -64,14 +63,17 @@ @PUT @Produces(MediaType.APPLICATION_JSON) - public LoginJson loginByName(@FormParam("name") String name, @FormParam("password") String password) { + public SignUpJson loginByName(@FormParam("name") String name, @FormParam("password") String password) { Account account = accounts.getAccountByName(name); - if (password.equals(account.getPassword())) { + if (accounts.matchesPassword(account.getUid(),password)) { - account.setToken(UUID.randomUUID().toString()); - LoginJson loginJson = new LoginJson("success"); - return loginJson; + + SignUpJson signUpJson = new SignUpJson(); + signUpJson.setUid(account.getUid()); + signUpJson.setMessage("success"); + account.setToken(signUpJson.getToken()); + return signUpJson; } else { throw new WebApplicationException(400); @@ -84,10 +86,11 @@ public LoginJson login(@PathParam("uid") String uid, @FormParam("password") String password) { Account account = accounts.getAccount(uid); - if (password.equals(account.getPassword())) { + if (accounts.matchesPassword(uid,password)) { - account.setToken(UUID.randomUUID().toString()); + //account.setToken(UUID.randomUUID().toString()); LoginJson loginJson = new LoginJson("success"); + account.setToken(loginJson.getToken()); return loginJson; } else { @@ -107,4 +110,40 @@ } + @Path("/{uid}") + @DELETE + @Produces(MediaType.APPLICATION_JSON) + public MessageJson deleteAccount(@PathParam("uid")String uid,@FormParam("password")String password){ + if(accounts.matchesPassword(uid,password)) { + accounts.removeAccount(uid); + return new MessageJson("success"); + } + throw new WebApplicationException(400); + } + @Path("/{uid}/followings") + @GET + @Produces(MediaType.APPLICATION_JSON) + public ArrayList getFollowings(@PathParam("uid")String uid){ + return getAccount(uid).getFollowings(); + } + @Path("/{uid}/followings") + @POST + @Produces(MediaType.APPLICATION_JSON) + public MessageJson addFollowings(@PathParam("uid")String uid,@FormParam("token")String token,@FormParam("newFollow")String newFollow){ + if(accounts.matchesToken(uid,token)&&!accounts.isSameUid(uid,newFollow)) { + accounts.followAccount(uid,newFollow); + return new MessageJson("success"); + } + throw new WebApplicationException(400); + } + @Path("/{uid}/followings") + @DELETE + @Produces(MediaType.APPLICATION_JSON) + public MessageJson deleteFollowings(@PathParam("uid")String uid,@FormParam("token")String token,@FormParam("deleteFollow")String deleteFollow){ + if(accounts.matchesToken(uid,token)&&!accounts.isSameUid(uid,deleteFollow)) { + accounts.unfollowAccount(uid,deleteFollow); + return new MessageJson("success"); + } + throw new WebApplicationException(400); + } } \ No newline at end of file