package gr.sperfect.djuqbox.webapp.server.rest;

import gr.sperfect.djuqbox.webapp.server.db.DBHelper;
import gr.sperfect.djuqbox.webapp.server.db.IDB;
import gr.sperfect.djuqbox.webapp.shared.data.PlayList;
import gr.sperfect.djuqbox.webapp.shared.data.Room;
import gr.sperfect.djuqbox.webapp.shared.data.User;
import gr.sperfect.djuqbox.webapp.shared.data.YoutubePlayList;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

@Path("/users")
public class UserResource extends BaseResource {

	static final IDB<User> db = DBHelper.getDB(User.class);

	@Context
	HttpHeaders headers;
	@Context
	Request req;
	@Context
	UriInfo uri;

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public List<User> getUsers() {

		List<User> ret = db.getAllObjects(null);

		return ret;
	}

	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public User createUser(User aUserParam) {
		User newUser = db.createObject(aUserParam);
		// save assign...

		Response.status(201);
		return newUser;
	}

	@PUT
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public User updateRoom(User aUserParam) {

		User newUser = db.updateObject(aUserParam);

		// save assign...
		return newUser;
	}

	@DELETE
	// @Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public void deleteRoom(User aUserParam) {

		db.deleteObject(aUserParam);

	}

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/{user_id}")
	public User getUser(@PathParam("user_id") Long aUserId) {

		Log(aUserId.toString());

		User u = db.getObjectById(aUserId);
				
		return u;		

	}

}

@@ -972,93 +972,10 @@
 %0A%0D%0A%09
-@Context%0D%0A%09HttpHeaders headers;%0D%0A%09@Context%0D%0A%09Request req;%0D%0A%09@Context%0D%0A%09UriInfo uri;
 %0D%0A
+
 %0D%0A%09@
@@ -1250,17 +1250,352 @@
 rParam) 
-%7B
+throws Exception %7B%0D%0A%09%09%0D%0A%09%09if (aUserParam.getName() == null %7C%7C aUserParam.getName() == %22%22)%0D%0A%09%09%7B%0D%0A%09%09%09throw new Exception(%22name needed for user%22);%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%09//check if user exists%0D%0A%09%09if(db.findObjectWithValue(%22name%22, aUserParam.getName()) != null)%0D%0A%09%09%7B%0D%0A%09%09%09throw new Exception(%22user %22+ aUserParam.getName()+%22 already existrs%22);%0D%0A%09%09%7D%0D%0A%09%09%09
 %0D%0A%09%09User
@@ -1840,36 +1840,33 @@
 am) %7B%0D%0A%0D%0A%09%09User 
-newU
+u
 ser = db.updateO
@@ -1909,36 +1909,33 @@
 gn...%0D%0A%09%09return 
-newU
+u
 ser;%0D%0A%09%7D%0D%0A%0D%0A%09@DE

check if user exists