diff --git a/build.gradle b/build.gradle index 7acb447..a6e7827 100644 --- a/build.gradle +++ b/build.gradle @@ -2,8 +2,12 @@ id 'org.springframework.boot' version '2.3.0.RELEASE' id 'io.spring.dependency-management' version '1.0.9.RELEASE' id 'java' + id 'war' } - +war { + enabled = true + archiveName 'amaryllis.war' +} group = 'org.ntlab.amaryllis' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' 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 e02df83..1b79454 100644 --- a/src/main/java/org/ntlab/amaryllis/server/resources/AccountsRest.java +++ b/src/main/java/org/ntlab/amaryllis/server/resources/AccountsRest.java @@ -17,6 +17,7 @@ import javax.swing.*; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; @Component @Path("/accounts") @@ -24,6 +25,7 @@ private Accounts accounts = Accounts.getInstance(); AccountsRest() { + /** * ใƒ†ใ‚นใƒˆ็”จ * */ @@ -35,81 +37,41 @@ } @GET - public String getAccounts() { - ObjectMapper mapper = new ObjectMapper(); + @Produces(MediaType.APPLICATION_JSON) + public ArrayList getAccounts() { + String json = ""; - try { - @JsonInclude(JsonInclude.Include.NON_NULL) - @JsonPropertyOrder({ - "accounts" - }) - class Message { - - @JsonProperty("accounts") - private List accounts = null; - @JsonIgnore - private Map additionalProperties = new HashMap(); - - @JsonProperty("accounts") - public List getAccounts() { - return accounts; - } - - @JsonProperty("accounts") - public void setAccounts(List accounts) { - this.accounts = accounts; - } - - @JsonAnyGetter - public Map getAdditionalProperties() { - return this.additionalProperties; - } - - @JsonAnySetter - public void setAdditionalProperty(String name, Object value) { - this.additionalProperties.put(name, value); - } - - } - Message message = new Message(); - List list = new ArrayList(); - for (Account a : accounts.getList()) list.add(a.getUid()); - message.setAccounts(list); - json = mapper.writeValueAsString(message); - } catch (IOException e) { - e.printStackTrace(); - System.out.println("failed"); - } System.out.println(json); - return json; + return accounts.getList(); } @POST @Produces(MediaType.APPLICATION_JSON) - public AbstractJson createAccount(@FormParam("name") String name, @FormParam("password") String password) { + public HashMap createAccount(@FormParam("name") String name, @FormParam("password") String password) { if (accounts.isRegisteredName(name)) { - return new AbstractJson() { - String message = "conflict"; + throw new WebApplicationException(400); - public String getMessage() { - return message; - } - }; } else { + Account newAccount = accounts.createAccount(name, password); - return new AbstractJson() { - String message = "success"; - String uid = newAccount.getUid(); - - public String getMessage() { - return message; - } - - public String getUid() { - return uid; - } - }; + HashMap response=new HashMap<>(); + response.put("message","success"); + response.put("uid",newAccount.getUid()); + return response; +// +// return new AbstractJson() { +// String message = "success"; +// String uid = newAccount.getUid(); +// +// public String getMessage() { +// return message; +// } +// +// public String getUid() { +// return uid; +// } +// }; } } @@ -117,35 +79,24 @@ @Path("/{uid}/login") @PUT @Produces(MediaType.APPLICATION_JSON) - public AbstractJson login(@PathParam("uid") String uid, @FormParam("password") String password) { + public LoginJson login(@PathParam("uid") String uid, @FormParam("password") String password) { Account account = accounts.getAccount(uid); if (password.equals(account.getPassword())) { account.setToken(UUID.randomUUID().toString()); - return new AbstractJson() { - String message = "success"; - String token = account.getToken(); - - public String getMessage() { - return message; - } - - public String getToken() { - return token; - } - }; + LoginJson loginJson=new LoginJson("success"); + return loginJson; } else { - return new MessageJson("failed"); + throw new WebApplicationException(400); } - } @Path("/{uid}") @GET @Produces(MediaType.APPLICATION_JSON) - public AbstractJson getAccount(@PathParam("uid") String uid) { + public Account getAccount(@PathParam("uid") String uid) { class ResponseJson extends AbstractJson { Account account = accounts.getAccount(uid); @@ -153,10 +104,8 @@ return account; } } - AbstractJson response = new ResponseJson(); + throw new WebApplicationException(400); - - return response; } } \ No newline at end of file