Added CRUD operations for Actor

This commit is contained in:
Barbara 2019-12-10 22:41:27 +01:00
parent e54eaf6ee3
commit ede3432050
5 changed files with 160 additions and 15 deletions

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("/actor")
@RolesAllowed("MoviesUserRole")
public class ActorResource {
@Inject
private ActorsService actorsService;
@Context
private UriInfo uriInfo;
@GET
@Produces({
MediaType.APPLICATION_JSON,
MediaType.APPLICATION_XML
})
public List<Actor> retrieveAll() {
return actorsService.findAllActors();
}
@GET
@Produces({
MediaType.APPLICATION_JSON,
MediaType.APPLICATION_XML
})
@Path("/{id}")
public Actor retrieve(@PathParam("id") long id) {
return actorsService.findActorById(id);
}
@DELETE
@Path("/{id}")
public void delete(@PathParam("id") long id) {
actorsService.removeActorById(id);
}
@POST
@Consumes({
MediaType.APPLICATION_JSON,
MediaType.APPLICATION_XML
})
public Response create(Actor actor) {
actor.setId(null); // Make sure that a new actor is added, not overwriting existing one
List<Actor> newActors = actorsService.saveActor(List.of(actor));
URI location = uriInfo.getAbsolutePathBuilder().path(newActors.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, Actor actor) {
actor.setId(id);
actorsService.saveActor(List.of(actor));
}
}

View File

@ -0,0 +1,76 @@
package at.technikumwien.movies;
import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.ejb.*;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.security.enterprise.SecurityContext;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
@Stateless
@TransactionManagement(value=TransactionManagementType.CONTAINER)
@RolesAllowed("MoviesUserRole")
public class ActorsService {
private static final Logger LOGGER = Logger.getLogger(ActorsService.class.getName());
@Resource
private SessionContext context;
@PersistenceContext(unitName = "MoviesPU")
private EntityManager em;
@Inject
private SecurityContext securityContext;
public Actor findActorById(long id) {
LOGGER.info("findActorById() >> id=" + id);
Actor actor = em.find(Actor.class, id);
if (actor == null) {
throw new EntityNotFoundException("can't find actor with id=" + id);
}
return actor;
}
public List<Actor> findAllActors() {
LOGGER.info("findAllActors)");
return em.createNamedQuery("Actors.selectAllActors", Actor.class)
.getResultList();
}
public void removeActorById(long id) {
LOGGER.info("removeActorById() >> id=" + id);
Actor actor = findActorById(id);
em.remove(actor);
}
// TODO maybe check if the actor already exists?
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public List<Actor> saveActor(List<Actor> actors) {
try {
List<Actor> addedActors = new ArrayList<>();
LOGGER.info("save() >> actors" + actors);
for (Actor actorToImport : actors) {
addedActors.add(em.merge(actorToImport));
}
return addedActors;
} catch (Exception e) {
LOGGER.info("Rollback when adding actor!");
context.setRollbackOnly();
e.printStackTrace();
}
return null;
}
}

View File

@ -65,7 +65,7 @@ public class MovieResource {
})
@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
movie.setId(id);
moviesService.save(List.of(movie));
}
}

View File

@ -63,13 +63,6 @@ public class MoviesService {
em.remove(movie); //managed news required
}
public List<Actor> findAllActors() {
LOGGER.info("findAllActors)");
return em.createNamedQuery("Actors.selectAllActors", Actor.class)
.getResultList();
}
// TODO maybe check if the movie already exists?
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public List<Movie> save(List<Movie> movies) {
@ -123,13 +116,18 @@ public class MoviesService {
List<Movie> addedMovies = new ArrayList<>();
// TODO: Add Rollback if error while looping!
try {
LOGGER.info("save() >> movies" + movies);
for (Movie movieToImport : movies) {
addedMovies.add(em.merge(movieToImport));
}
return addedMovies;
} catch (Exception e) {
LOGGER.info("Rollback when adding movie!");
context.setRollbackOnly();
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}

View File

@ -65,7 +65,7 @@ public class StudioResource {
})
@Path("/{id}")
public void update(@PathParam("id") long id, Studio studio) {
studio.setId(id); // Make sure that a new studio is added, not overwriting existing one
studio.setId(id);
studiosService.saveStudio(List.of(studio));
}
}