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 b468076..e3beab2 100644 --- a/src/main/java/org/ntlab/amaryllis/server/models/Accounts.java +++ b/src/main/java/org/ntlab/amaryllis/server/models/Accounts.java @@ -19,7 +19,9 @@ return theInstance; } - + /** + *インスタンス生成を禁止するコンストラクタ + */ private Accounts() { } @@ -92,22 +94,29 @@ return accountHashMap; } - public void addAccount(Account account) { + public void addAccount(Account account) { accountHashMap.put(account.getUid(),account); } + /** + * アカウントを新規作成するメソッド + * @param name アカウントの名前 + * @param password アカウントのパスワード + * @return + */ public Account createAccount(String name, String password) { Account newAccount = new Account(name, password); addAccount(newAccount); return newAccount; } + /** - *アカウントの削除時に実行するメソッド. + *アカウントの削除するメソッド. * 削除前にすべてのfollowingsに対してフォロー解除を行う * @param uid 削除するアカウントのuid */ - public void removeAccount(String uid) { + public void deleteAccount(String uid) { for(String followUid:getAccount(uid).getFollowings()){ getAccount(followUid).getFollowers().remove(uid); diff --git a/src/main/java/org/ntlab/amaryllis/server/resources/AccountRest.java b/src/main/java/org/ntlab/amaryllis/server/resources/AccountRest.java new file mode 100644 index 0000000..d0440e8 --- /dev/null +++ b/src/main/java/org/ntlab/amaryllis/server/resources/AccountRest.java @@ -0,0 +1,87 @@ +package org.ntlab.amaryllis.server.resources; + +import org.ntlab.amaryllis.server.entities.Account; +import org.ntlab.amaryllis.server.entities.MessageJson; +import org.ntlab.amaryllis.server.models.Accounts; +import org.springframework.stereotype.Component; + +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import java.util.ArrayList; + +@Component +@Path("/accounts") +public class AccountRest { + private Accounts accounts = Accounts.getInstance(); + + @Path("/{uid}") + @GET + @Produces(MediaType.APPLICATION_JSON) + public Account getAccount(@PathParam("uid") String uid) { + + Account responseAccount = accounts.getAccount(uid); + if (responseAccount != null) return responseAccount; + + throw new WebApplicationException(400); + + } + + @Path("/{uid}") + @DELETE + @Produces(MediaType.APPLICATION_JSON) + public MessageJson deleteAccount(@PathParam("uid")String uid, @FormParam("password")String password){ + if(accounts.matchesPassword(uid,password)) { + accounts.deleteAccount(uid); + return new MessageJson("success"); + } + 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(); + } + +} 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 9293ea4..e80f385 100644 --- a/src/main/java/org/ntlab/amaryllis/server/resources/AccountsRest.java +++ b/src/main/java/org/ntlab/amaryllis/server/resources/AccountsRest.java @@ -31,7 +31,6 @@ { SignUpJson sj=createAccount("name", "pass"); accounts.getAccountByName("name").setUid("test"); - } } @@ -68,7 +67,6 @@ if (accounts.matchesPassword(account.getUid(),password)) { - SignUpJson signUpJson = new SignUpJson(); signUpJson.setUid(account.getUid()); signUpJson.setMessage("success"); @@ -84,6 +82,7 @@ @PUT @Produces(MediaType.APPLICATION_JSON) public LoginJson login(@PathParam("uid") String uid, @FormParam("password") String password) { + System.out.println("CALL!"); Account account = accounts.getAccount(uid); if (accounts.matchesPassword(uid,password)) { @@ -98,105 +97,6 @@ } } - @Path("/{uid}") - @GET - @Produces(MediaType.APPLICATION_JSON) - public Account getAccount(@PathParam("uid") String uid) { - Account responseAccount = accounts.getAccount(uid); - if (responseAccount != null) return responseAccount; - - throw new WebApplicationException(400); - - } - - @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 HashSet 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); - } - - @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 diff --git a/src/main/java/org/ntlab/amaryllis/server/resources/FollowRest.java b/src/main/java/org/ntlab/amaryllis/server/resources/FollowRest.java new file mode 100644 index 0000000..07a47f4 --- /dev/null +++ b/src/main/java/org/ntlab/amaryllis/server/resources/FollowRest.java @@ -0,0 +1,52 @@ +package org.ntlab.amaryllis.server.resources; + +import org.ntlab.amaryllis.server.entities.MessageJson; +import org.ntlab.amaryllis.server.models.Accounts; +import org.springframework.stereotype.Component; + +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import java.util.HashSet; + +@Component +@Path("/accounts") +public class FollowRest { + Accounts accounts= Accounts.getInstance(); + + @Path("/{uid}/followings") + @GET + @Produces(MediaType.APPLICATION_JSON) + public HashSet getFollowings(@PathParam("uid")String uid){ + return accounts.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); + } + + @Path("/{uid}/followers") + @GET + @Produces(MediaType.APPLICATION_JSON) + public HashSet getFollowers(@PathParam("uid")String uid){ + return accounts.getAccount(uid).getFollowers(); + } + +} diff --git a/src/main/java/org/ntlab/amaryllis/server/tests/BaseSingleton.java b/src/main/java/org/ntlab/amaryllis/server/tests/BaseSingleton.java index 8838372..18ae885 100644 --- a/src/main/java/org/ntlab/amaryllis/server/tests/BaseSingleton.java +++ b/src/main/java/org/ntlab/amaryllis/server/tests/BaseSingleton.java @@ -1,9 +1,11 @@ package org.ntlab.amaryllis.server.tests; public class BaseSingleton { - private static BaseSingleton theInstance=null; + protected static BaseSingleton theInstance=null; + public static BaseSingleton getInstance(){ if(theInstance==null)theInstance=new BaseSingleton(); return theInstance; + } }