diff --git a/src/main/java/cactusServer/entities/Account.java b/src/main/java/cactusServer/entities/Account.java index c46e353..e0dfb91 100644 --- a/src/main/java/cactusServer/entities/Account.java +++ b/src/main/java/cactusServer/entities/Account.java @@ -1,5 +1,31 @@ package cactusServer.entities; public class Account { + private int id; + private String name; + public Account(int userId, String userName) { + setId(userId); + setName(userName); + } + + public Account() { + + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } } diff --git a/src/main/java/cactusServer/models/Accounts.java b/src/main/java/cactusServer/models/Accounts.java index 33bcb52..8c86756 100644 --- a/src/main/java/cactusServer/models/Accounts.java +++ b/src/main/java/cactusServer/models/Accounts.java @@ -1,5 +1,110 @@ package cactusServer.models; -public class Accounts { +import javax.ws.*; +import javax.inject.Singleton; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import cactusServer.entities.Account; + +import java.util.ArrayList; + +@Singleton +@Path("/accounts") +public class Accounts { + private static Accounts theInstance = null; + private ArrayList accounts = new ArrayList(); + private int userID = 0; + + public Accounts() { + if (theInstance == null) { + theInstance = this; + } + } + + public static Accounts getInstance() { + if (theInstance == null) { + theInstance = new Accounts(); + } + return theInstance; + } + + @POST + @Produces(MediaType.APPLICATION_JSON) + public Account createAcount(@FormParam("userName") String userName) { + System.out.println(userName); + + Account newAccount = new Account(this.userID, userName); + // newAccount.setUserName(userName); + // accounts.add(new Account(this.userID,userName)); + accounts.add(newAccount); + this.userID++; + + System.out.println(userID); + + return newAccount; + } + + public void createAccount(int userId) { + Account newAccount = new Account(userId, "user" + userId); + accounts.add(newAccount); + System.out.println(accounts.size()); + } + + @GET + @Produces(MediaType.APPLICATION_JSON) + public ArrayList getAccountList() { + return this.accounts; + } + + @Path("/{userID}") + @PUT + @Produces(MediaType.APPLICATION_JSON) + public Account updateAcount(@PathParam("userID") String userID, @FormParam("userName") String userName, + @FormParam("mode") String mode) { + Account editAccount = accounts.get(Integer.valueOf(userID)); + + accounts.set(Integer.valueOf(userID), editAccount); + + return editAccount; + } + + @Path("/{userID}") + @GET + @Produces(MediaType.APPLICATION_JSON) + public Account getAcount(@PathParam("userID") String userID) { + Account editAccount = accounts.get(Integer.valueOf(userID)); + return editAccount; + } + + public Accounts getAccount() { + Account ac = new Account(); + this.userID = ac.getId(); + return new Accounts(); + } + + public boolean checkCreatedUser(int userId) { + for (Account account : accounts) { + if (account.getId() == userId) { + return true; + } + } + return false; + } + + public ArrayList getAccounts() { + return accounts; + } + + public void setAccounts(ArrayList accounts) { + this.accounts = accounts; + } + + public String getUserNameById(int userId) { + for (Account account : accounts) { + if (account.getId() == userId) + return account.getName(); + } + return "nameNone"; + } } diff --git a/src/main/java/cactusServer/resources/AccountsRest.java b/src/main/java/cactusServer/resources/AccountsRest.java index 1f20921..b1975ae 100644 --- a/src/main/java/cactusServer/resources/AccountsRest.java +++ b/src/main/java/cactusServer/resources/AccountsRest.java @@ -1,5 +1,14 @@ package cactusServer.resources; -public class AccountsRest { +import javax.ws.*; +import javax.inject.Singleton; +import javax.ws.rs.Path; +import javax.ws.rs.core.MediaType; +import java.util.ArrayList; + +@Singleton +@Path("AccontsRest") +public class AccountsRest { + }