/** * Updates a category. * * @param categoryRepresentation the category representation * @param categoryId the category id * @return the updated category or 400 if the request was malformed or 404 if the category doesn't * exist */ @PUT @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @Path("/{categoryId}") public Response update( CategoryRepresentation categoryRepresentation, @PathParam("categoryId") long categoryId) { if (categoryRepresentation.getId() != categoryId) { // bad request: the ids don't match return Response.status(Response.Status.BAD_REQUEST) .type(MediaType.TEXT_PLAIN_TYPE) .entity("Category identifier cannot be updated") .build(); } Category category; try { category = fluentAssembler .merge(categoryRepresentation) .with(MODEL_MAPPER) .into(Category.class) .fromRepository() .orFail(); } catch (AggregateNotFoundException e) { return Response.status(Response.Status.NOT_FOUND).build(); } category = categoryRepository.saveCategory(category); if (category == null) { return Response.status(Response.Status.NOT_MODIFIED).build(); } CategoryRepresentation categoryRepresentation1 = fluentAssembler.assemble(category).with(MODEL_MAPPER).to(CategoryRepresentation.class); return Response.ok(categoryRepresentation1).build(); }
/** * Creates a category * * @param categoryRepresentation the category representation * @return the created category */ @POST @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public Response add(CategoryRepresentation categoryRepresentation) { Category category = fluentAssembler .merge(categoryRepresentation) .with(MODEL_MAPPER) .into(Category.class) .fromFactory(); categoryRepository.persistCategory(category); CategoryRepresentation categoryRepresentation1; categoryRepresentation1 = fluentAssembler.assemble(category).with(MODEL_MAPPER).to(CategoryRepresentation.class); return Response.created(URI.create(uriInfo.getRequestUri() + "/" + category.getEntityId())) .entity(categoryRepresentation1) .build(); }