package com.cineplexnotifier.rest;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import javax.ejb.EJB;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.jboss.resteasy.annotations.cache.Cache;

import com.cineplexnotifier.data.MovieRepository;
import com.cineplexnotifier.model.Movie;

@Path("movies")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class MoviesResource {
	@EJB
	private MovieRepository movieDao;

	@GET
	@Path("/")
	@Cache(maxAge = 600)
	public List<Movie> getMovies(@DefaultValue(value="false") @QueryParam("all") boolean available ) {
		return movieDao.selectByAvailability(false);
	}

	@POST
	@Path("/")
	public Response putMovie(Movie m) throws URISyntaxException {
		URI r = new URI(m.getCineplexKey());
		if (m.getId() == 0l && movieDao.insert(m) != 0l) {
			return Response.created(r).build();
		}
		throw new WebApplicationException(Status.BAD_REQUEST);
	}

	@GET
	@Path("{id}")
	public Movie getMovieByCineplexKey(@PathParam("id") String cineplexKey) {
		return movieDao.selectByCineplexKey(cineplexKey);
	}

}

@@ -909,17 +909,19 @@
 ue(value
-=
+ = 
 %22false%22)
@@ -957,17 +957,16 @@
 vailable
- 
 ) %7B%0A%09%09re
@@ -1133,18 +1133,253 @@
 Key());%0A
-
 %09%09
+Movie old = movieDao.selectByCineplexKey(m.getCineplexKey());%0A%09%09if (old != null) %7B%0A%09%09%09m.setId(old.getId());%0A%09%09%09movieDao.update(m);%0A%0A%09%09%09// TODO formalize REST conventions used by this app%0A%09%09%09return Response.created(r).build();%0A%09%09%7D else 
 if (m.ge

Handle duplicate POSTs of same entry (by overwriting the old entery)