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 404301f..c6807aa 100644 --- a/src/main/java/org/ntlab/acanthus_server/entities/Account.java +++ b/src/main/java/org/ntlab/acanthus_server/entities/Account.java @@ -20,62 +20,65 @@ // //////////////////////////////////////////////////////////////////////////////////////// package org.ntlab.acanthus_server.entities; +import com.fasterxml.jackson.annotation.JsonIgnore; + +import java.util.ArrayList; import java.util.UUID; public class Account { + + private Integer uid; private String name; private String email; + private ArrayList workList = new ArrayList<>(); + + @JsonIgnore private String token; + @JsonIgnore private String password; - private Work work; //----------------------------------------------------------------- //コンストラクト - public Account(String name, String email, String password) { + public Account(Integer uid, String name, String email, String password) { + this.uid = uid; this.name = name; this.email = email; this.password = password; - this.work = new Work(); } //----------------------------------------------------------------- //----------------------------------------------------------------- // setter + public void setUid(Integer uid){this.uid = uid;} public void setName(String name) { this.name = name; } - //----------------------------------------------------------------- public void setEmail(String email) { this.email = email; } - //----------------------------------------------------------------- public void setPassword(String password){ this.password = password; } - //----------------------------------------------------------------- - public void setWork(Work work){ - this.work = work; - } + public void addWork(Work work){ this.workList.add(work); } + //----------------------------------------------------------------- // getter + public Integer getUid() {return (this.uid);} public String getName(){ return (this.name); } - //----------------------------------------------------------------- public String getEmail() { return (this.email); } - //----------------------------------------------------------------- public String getPassword() { return (this.password); } - //----------------------------------------------------------------- public String getToken() { return (this.token); } - //----------------------------------------------------------------- - public Work getWork() { - return (this.work); + public ArrayList getWork() { + return (this.workList); } + //----------------------------------------------------------------- //----------------------------------------------------------------- // トークンを更新する 藤井 diff --git a/src/main/java/org/ntlab/acanthus_server/models/Accounts.java b/src/main/java/org/ntlab/acanthus_server/models/Accounts.java index 2a8976e..52de49b 100644 --- a/src/main/java/org/ntlab/acanthus_server/models/Accounts.java +++ b/src/main/java/org/ntlab/acanthus_server/models/Accounts.java @@ -2,6 +2,7 @@ import org.ntlab.acanthus_server.entities.Account; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Random; @@ -30,34 +31,59 @@ //----------------------------------------------------------------- //----------------------------------------------------------------- - // Uidからアカウントを返す + // アカウントを全て返す + public Collection getAllAccounts() { + return accountHashMap.values(); + } + //----------------------------------------------------------------- + // Uidからアカウントを返す + /** + * @param uid ユーザーID + */ public Account getAccountByUid(int uid) { return accountHashMap.get(uid); } //----------------------------------------------------------------- - // e-Mailからアカウントを返す - //----------------------------------------------------------------- - public Account getAccountByEMail(String email) { + // e-mailからアカウントを返す + /** + * @param email メアド + */ + public Account getAccountByEmail(String email) { for (var account : accountHashMap.values()) { if (account.getEmail().equals(email)) return account; } return null; } + //----------------------------------------------------------------- - //accountを全て返す - public Collection getAllAccounts(){ - return accountHashMap.values(); + // 名前からアカウントのリストを返す + /** + * @param name ユーザー名 + */ + public ArrayList getAccountsByName(String name) { + var accountsList = new ArrayList(); + + for (var account : accountHashMap.values()) { + if (account.getName().equals(name)) accountsList.add(account); + } + + return accountsList; } //----------------------------------------------------------------- - //accountを追加する - public int registAccount(String name, String email, String password){ - var newAccount = new Account(name, email, password); + // アカウントを登録する. + /** + * @param name ユーザー名 + * @param email メアド + * @param password パスワード + */ + public Account registerAccount(String name, String email, String password) { var uid = new Random().nextInt(); + var newAccount = new Account (uid, name, email, password); + accountHashMap.put(uid, newAccount); - accountHashMap.put(uid,newAccount); - return uid; + return newAccount; } - + //----------------------------------------------------------------- } diff --git a/src/main/java/org/ntlab/acanthus_server/resources/accounts/AccountsRest.java b/src/main/java/org/ntlab/acanthus_server/resources/accounts/AccountsRest.java index 2b183cb..c230fe2 100644 --- a/src/main/java/org/ntlab/acanthus_server/resources/accounts/AccountsRest.java +++ b/src/main/java/org/ntlab/acanthus_server/resources/accounts/AccountsRest.java @@ -1,5 +1,6 @@ package org.ntlab.acanthus_server.resources.accounts; +import org.apache.catalina.loader.WebappClassLoader; import org.ntlab.acanthus_server.entities.Account; import org.ntlab.acanthus_server.models.Accounts; import org.springframework.stereotype.Component; @@ -14,17 +15,44 @@ private Accounts accounts = Accounts.getInstance(); //----------------------------------------------------------------- + // UIdに一致するアカウントを返す @GET @Produces(MediaType.APPLICATION_JSON) - public Collection getAccounts(@QueryParam("uid") int uid, @QueryParam("token") String token) { - return accounts.getAllAccounts(); + public Account getAccount(@QueryParam("uid") int uid) { + return accounts.getAccountByUid(uid); } //----------------------------------------------------------------- + // 名前が一致するアカウントをすべて返す. + @GET + @Produces(MediaType.APPLICATION_JSON) + public Collection getAccounts(@QueryParam("name") String name) { + return accounts.getAccountsByName(name); + } + + //----------------------------------------------------------------- + // アカウントの新規作成 + /** + * @param name ユーザー名 + * @param email メアド + * @param password パスワード + */ @POST @Produces(MediaType.APPLICATION_JSON) public int createAccount(@FormParam("name") String name , @FormParam("email") String email, @FormParam("password") String password){ - return accounts.registAccount(name,email,password); + + // password: 最低8文字以上の入力 + var passMinLen = 8; + if(password.length() < passMinLen) throw new WebApplicationException(401); + + // すでに同じメールアドレスが存在しているか + var existAccount = accounts.getAccountByEmail(email); + if(existAccount != null) throw new WebApplicationException(400); + + // アカウント登録 + var newAccount = accounts.registerAccount(name, email, password); + + return newAccount.getUid(); } } diff --git a/src/main/java/org/ntlab/acanthus_server/resources/accounts/LoginRest.java b/src/main/java/org/ntlab/acanthus_server/resources/accounts/LoginRest.java index 55ceb99..1faf919 100644 --- a/src/main/java/org/ntlab/acanthus_server/resources/accounts/LoginRest.java +++ b/src/main/java/org/ntlab/acanthus_server/resources/accounts/LoginRest.java @@ -40,7 +40,7 @@ @Produces(MediaType.APPLICATION_JSON) public void authenticateLoginToken(@FormParam("e-mail") String email, @FormParam("password") String password) { - var searchAccount = accounts.getAccountByEMail(email); + var searchAccount = accounts.getAccountByEmail(email); if (searchAccount == null) throw new WebApplicationException(404); if (searchAccount.isMatchedPassword(password)) throw new WebApplicationException(401);