Newer
Older
CactusServer / src / main / java / cactusServer / resources / AccountsRest.java
y-ota on 23 Oct 2018 2 KB format
package cactusServer.resources;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

import cactusServer.entities.*;
import cactusServer.models.Accounts;
import net.arnx.jsonic.JSON;

@Path("/accounts")
public class AccountsRest {
	@PUT
	// @Produces(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public String loginAccount(@FormParam("userID") String userID, @FormParam("userPass") String userPass) {
		URIAddressedAccount session = Accounts.getInstance().loginAccount(userID, userPass);
		if (session != null) {
			return JSON.encode(session);
		} else {
			throw new WebApplicationException(400);
		}
	}

	@POST
	@Path("/logout")
	// @Produces(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public String logoutAccount(@FormParam("token") String token) {
		return JSON.encode(Accounts.getInstance().logoutAccount(token));
	}

	@POST
	// @Produces(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public String createAccount(@FormParam("userID") String userID, @FormParam("userName") String userName,
			@FormParam("userPass") String userPass) {
		URIAddressedAccount editAccount = Accounts.getInstance().createAcount(userID, userName, userPass);
		if (editAccount != null) {
			return JSON.encode(editAccount);
		} else {
			throw new WebApplicationException(409);
		}
	}

	@GET
	@Path("/{uniqueID}")
	// @Produces(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public String getAccount(@PathParam("uniqueID") String uniqueID) {
		return JSON.encode(Accounts.getInstance().getAccountByuniqueID(uniqueID));
	}

	@DELETE
	@Path("/{uniqueID}")
	// @Produces(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public String deleteAccount(@PathParam("uniqueID") String uniqueID) {
		if (Accounts.getInstance().getAccountByuniqueID(uniqueID) != null) {
			return JSON.encode(Accounts.getInstance().deleteAccount(uniqueID));
		} else {
			throw new WebApplicationException(400);
		}
	}

	@GET
	// @Produces(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public String getAccounts() {
		return JSON.encode(Accounts.getInstance().getAccounts());
	}

}