AccountsRestの例外処理を書きました。
1 parent 81f09cd commit edaa47cda40637a6baee737d247e041a05461e03
Shinji authored on 23 Jun 2020
Showing 3 changed files
View
23
src/main/java/org/ntlab/amaryllis/server/entities/SignUpJson.java 0 → 100644
package org.ntlab.amaryllis.server.entities;
 
public class SignUpJson {
private String message;
private String uid;
 
public String getMessage() {
return message;
}
 
public String getUid() {
return uid;
}
 
public void setMessage(String message) {
this.message = message;
}
 
public void setUid(String uid) {
this.uid = uid;
}
}
View
25
src/main/java/org/ntlab/amaryllis/server/models/Accounts.java
import com.fasterxml.jackson.annotation.JsonProperty;
import org.ntlab.amaryllis.server.entities.Account;
 
import java.util.ArrayList;
import java.util.HashMap;
 
public class Accounts {
private static Accounts theInstance = null;
private ArrayList<Account> accounts = new ArrayList<Account>();
private HashMap<String,Account> accountHashMap=new HashMap<>();
 
public static Accounts getInstance() {
if (theInstance == null) {
theInstance = new Accounts();
if (a.getUid().equals(uid)) return a;
}
return null;
}
public Account getAccountByName(String name){
for (Account a : accounts) {
if (a.getName().equals(name)) return a;
}
return null;
 
}
 
public boolean isRegisteredName(String name) {
boolean response;
 
for (Account a : accounts) {
if (a.getName().equals(name)) return true;
}
return false;
}
 
public void addAccount(Account account) {
accounts.add(account);
 
accountHashMap.put(account.getUid(),account);
}
 
public Account createAccount(String name, String password) {
Account newAccount = new Account(name, password);
return newAccount;
}
 
public void removeAccount(String uid) {
 
int index=0;
for(Account a:accounts){
if(a.getUid().equals(uid))accounts.remove(index);
index++;
}
accountHashMap.remove(uid);
}
 
 
}
View
26
src/main/java/org/ntlab/amaryllis/server/resources/AccountsRest.java
}
 
@POST
@Produces(MediaType.APPLICATION_JSON)
public HashMap<String,String> createAccount(@FormParam("name") String name, @FormParam("password") String password) {
public SignUpJson createAccount(@FormParam("name") String name, @FormParam("password") String password) {
if (accounts.isRegisteredName(name)) {
throw new WebApplicationException(400);
 
} else {
 
Account newAccount = accounts.createAccount(name, password);
HashMap<String,String> response=new HashMap<>();
response.put("message","success");
response.put("uid",newAccount.getUid());
SignUpJson response=new SignUpJson();
response.setMessage("success");
response.setUid(newAccount.getUid());
 
return response;
//
// return new AbstractJson() {
// String message = "success";
// }
// };
}
 
}
 
@PUT
@Produces(MediaType.APPLICATION_JSON)
public LoginJson loginByName(@FormParam("name") String name, @FormParam("password") String password) {
Account account = accounts.getAccountByName(name);
 
if (password.equals(account.getPassword())) {
 
account.setToken(UUID.randomUUID().toString());
LoginJson loginJson=new LoginJson("success");
return loginJson;
 
} else {
throw new WebApplicationException(400);
}
}
 
@Path("/{uid}/login")
@PUT