package hr.vsite.mentor.servlet.rest.resources;

import java.util.List;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.transaction.Transactional;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import hr.vsite.mentor.course.Course;
import hr.vsite.mentor.course.CourseFilter;
import hr.vsite.mentor.course.CourseManager;
import hr.vsite.mentor.user.User;

@Path("course")
public class CourseResource {

	@Inject
	public CourseResource(Provider<CourseManager> courseProvider) {
		this.courseProvider = courseProvider;
	}
	
	@GET
	@Path("list")
	@Produces(MediaType.APPLICATION_JSON)
	@Transactional
	public List<Course> list(
		@QueryParam("title") String title,
		@QueryParam("author") User author,
		@QueryParam("count") Integer count,
		@QueryParam("offset") Integer offset
	) {

		CourseFilter filter = new CourseFilter();
		filter.setTitle(title);
		filter.setAuthor(author);
		
		return courseProvider.get().list(filter, count, offset);
		
	}
   
	@GET
	@Path("{course}")
	@Produces(MediaType.APPLICATION_JSON)
	public Course findById(
		@PathParam("course") Course course
	) {

		if (course == null)
			throw new NotFoundException();

		return course;

	}
	
	@POST
	@Path("insert")
	@Transactional
	@Consumes(MediaType.APPLICATION_JSON)
	public void insert(Course course) {
		courseProvider.get().insert(course);
	}

	private final Provider<CourseManager> courseProvider;
    
}
@@ -65,16 +65,40 @@
 l.List;%0A
+import java.util.UUID;%0A%0A
 import j
@@ -265,16 +265,40 @@
 s.POST;%0A
+import javax.ws.rs.PUT;%0A
 import j
@@ -1450,18 +1450,17 @@
 %09%7D%0A%09%0A%09@P
-OS
+U
 T%0A%09@Path
@@ -1461,22 +1461,16 @@
 %09@Path(%22
-insert
 %22)%0A%09@Tra
@@ -1519,24 +1519,63 @@
 N_JSON)%0A
+%09@Produces(MediaType.APPLICATION_JSON)%0A
 %09public 
 void ins
@@ -1570,12 +1570,14 @@
 lic 
-void
+Course
  ins
@@ -1599,16 +1599,23 @@
 se) %7B%0A%09%09
+return 
 coursePr
@@ -1645,16 +1645,259 @@
 rse);%0A%09%7D
+%0A%09%0A%09@POST%0A%09@Path(%22%7Bid%7D%22)%0A%09@Transactional%0A%09@Consumes(MediaType.APPLICATION_JSON)%0A%09@Produces(MediaType.APPLICATION_JSON)%0A%09public Course update(%0A%09%09%09@PathParam(%22id%22) UUID id,%0A%09%09%09Course course) %7B%0A%09%09return courseProvider.get().update(id, course);%0A%09%7D
 %0A%0A%09priva

Split insert method to separate insert and update