Added findAllStudios and findStudioById

This commit is contained in:
Barbara 2019-12-10 15:21:00 +01:00
parent 9e237adc19
commit 3bd40b6280
2 changed files with 82 additions and 0 deletions

View File

@ -77,6 +77,17 @@ public class MoviesService {
.getResultList();
}
public Studio findStudioById(long id) {
LOGGER.info("findStudioById() >> id=" + id);
Studio studio = em.find(Studio.class, id);
if (studio == null) {
throw new EntityNotFoundException("can't find studio with id=" + id);
}
return studio;
}
// TODO maybe check if the movie already exists?
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public List<Movie> save(List<Movie> movies) {

View File

@ -0,0 +1,71 @@
package at.technikumwien.movies;
import javax.annotation.security.RolesAllowed;
import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.net.URI;
import java.util.List;
@Path("/studio")
@RolesAllowed("MoviesUserRole")
public class StudioResource {
@Inject
private MoviesService moviesService;
@Context
private UriInfo uriInfo;
@GET
@Produces({
MediaType.APPLICATION_JSON,
MediaType.APPLICATION_XML
})
public List<Studio> retrieveAll() {
return moviesService.findAllStudios();
}
@GET
@Produces({
MediaType.APPLICATION_JSON,
MediaType.APPLICATION_XML
})
@Path("/{id}")
public Studio retrieve(@PathParam("id") long id) {
return moviesService.findStudioById(id);
}
/*
@DELETE
@Path("/{id}")
public void delete(@PathParam("id") long id) {
moviesService.removeStudioById(id);
}
@POST
@Consumes({
MediaType.APPLICATION_JSON,
MediaType.APPLICATION_XML
})
public Response create(Movie movie) {
movie.setId(null); // Make sure that a new movie is added, not overwriting existing one
List<Movie> newMovies = moviesService.save(List.of(movie));
URI location = uriInfo.getAbsolutePathBuilder().path(newMovies.get(0).getId().toString()).build();
return Response.created(location).build();
}
@PUT
@Consumes({
MediaType.APPLICATION_JSON,
MediaType.APPLICATION_XML
})
@Path("/{id}")
public void update(@PathParam("id") long id, Movie movie) {
movie.setId(id); // Make sure that a new movie is added, not overwriting existing one
moviesService.save(List.of(movie));
}*/
}