Commit 06960395 by Tuomas Riihimäki

Add more informative responses for place delete function

1 parent 860254e3
...@@ -82,16 +82,15 @@ public class MapAdminView { ...@@ -82,16 +82,15 @@ public class MapAdminView {
@GET @GET
@Path("/background/{mapId}") @Path("/background/{mapId}")
@ApiResponse(code = 200, message = "Return backround map as JPEG") @ApiResponse(code = 200, message = "Return backround map as JPEG")
public Response getMapBg(@PathParam("mapId") Integer mapid) public Response getMapBg(@PathParam("mapId") Integer mapid) {
{ try {
try { EventMap map = placebean.findMap(mapid);
EventMap map = placebean.findMap(mapid); ByteArrayInputStream istream = new ByteArrayInputStream(map.getMapData());
ByteArrayInputStream istream = new ByteArrayInputStream(map.getMapData()); return Response.ok().entity(istream).type("image/jpeg").build();
return Response.ok().entity(istream).type("image/jpeg").build(); } catch (Exception e) {
} catch(Exception e) { logger.error("GET /placeadmin/background/{mapId} threw an exception", e);
logger.error("GET /placeadmin/background/{mapId} threw an exception", e); throw e;
throw e; }
}
} }
@GET @GET
...@@ -99,43 +98,46 @@ public class MapAdminView { ...@@ -99,43 +98,46 @@ public class MapAdminView {
@ApiOperation(value = "Get All maps", response = MapRoot.class) @ApiOperation(value = "Get All maps", response = MapRoot.class)
@ApiResponse(code = 200, message = "Return all maps") @ApiResponse(code = 200, message = "Return all maps")
public MapRoot getAllMaps() { public MapRoot getAllMaps() {
try { try {
MapRoot ret = PojoUtils.parseMaps(eventbean.getCurrentEvent().getEventMaps()); MapRoot ret = PojoUtils.parseMaps(eventbean.getCurrentEvent().getEventMaps());
logger.info("getallmaps called! {}", ret); logger.info("getallmaps called! {}", ret);
return ret; return ret;
} catch(Exception e) { } catch (Exception e) {
logger.error("GET /placeadmin/maps threw an exception", e); logger.error("GET /placeadmin/maps threw an exception", e);
throw e; throw e;
} }
} }
@GET @GET
@Path("/places/{mapId}") @Path("/places/{mapId}")
@ApiOperation(value = "Get places by mapId", response = PlaceRoot.class) @ApiOperation(value = "Get places by mapId", response = PlaceRoot.class)
public PlaceRoot getPlaces(@PathParam("mapId") Integer mapid) { public PlaceRoot getPlaces(@PathParam("mapId") Integer mapid) {
try { try {
EventMap map = placebean.findMap(mapid); EventMap map = placebean.findMap(mapid);
PlaceRoot ret = PojoUtils.initPlaceRoot(map); PlaceRoot ret = PojoUtils.initPlaceRoot(map);
logger.info("returning map {} entity {}", mapid, ret); logger.info("returning map {} entity {}", mapid, ret);
return ret; return ret;
} catch(Exception e) { } catch (Exception e) {
logger.error("PUT /placeadmin/places/{mapId} threw an exception", e); logger.error("PUT /placeadmin/places/{mapId} threw an exception", e);
throw e; throw e;
} }
} }
@DELETE @DELETE
@Path("/place/{id}") @Path("/place/{id}")
@ApiOperation("Delete a place") @ApiOperation("Delete a place")
public Response deletePlace(@PathParam("id") Integer id) { public Response deletePlace(@PathParam("id") Integer id) {
try { try {
Place place = eventmapbean.findPlace(id); Place place = eventmapbean.findPlace(id);
eventmapbean.deletePlace(place); if (place == null) {
return Response.ok().build(); return Response.status(Status.NOT_FOUND).entity("Place not found for id " + id).build();
} catch(Exception e) { }
logger.error("DELETE /placeadmin/place/{id} threw an exception", e); eventmapbean.deletePlace(place);
throw e; return Response.ok().build();
} } catch (Exception e) {
logger.error("DELETE /placeadmin/place/{id} threw an exception", e);
return Response.serverError().entity("Got exception when removing place: " + e.getMessage()).build();
}
} }
@GET @GET
...@@ -143,105 +145,105 @@ public class MapAdminView { ...@@ -143,105 +145,105 @@ public class MapAdminView {
@ApiOperation(value = "Always returns 406 - Not acceptable") @ApiOperation(value = "Always returns 406 - Not acceptable")
@ApiResponse(code = 406, message = "Nope. Not acceptable.") @ApiResponse(code = 406, message = "Nope. Not acceptable.")
public Response getPlaceError() { public Response getPlaceError() {
try { try {
ResponseBuilder ret = Response.ok(); ResponseBuilder ret = Response.ok();
ret = ret.status(Status.NOT_ACCEPTABLE); ret = ret.status(Status.NOT_ACCEPTABLE);
ret.entity("Method not allowed!\nGET is not supported for /place/. See api for correct use!"); ret.entity("Method not allowed!\nGET is not supported for /place/. See api for correct use!");
return ret.build(); return ret.build();
} catch(Exception e) { } catch (Exception e) {
logger.error("GET /placeadmin/place/ threw an exception", e); logger.error("GET /placeadmin/place/ threw an exception", e);
throw e; throw e;
} }
} }
@POST @POST
@Path("/place/") @Path("/place/")
@ApiOperation(value = "Create a place", response = PlacePojo.class) @ApiOperation(value = "Create a place", response = PlacePojo.class)
public Response createPlace(PlacePojo create) { public Response createPlace(PlacePojo create) {
try { try {
logger.info("Finding map with id {}", create.getMapId()); logger.info("Finding map with id {}", create.getMapId());
EventMap map = eventmapbean.find(create.getMapId()); EventMap map = eventmapbean.find(create.getMapId());
if (map == null) { if (map == null) {
logger.warn("No map found!"); logger.warn("No map found!");
return Response.serverError().entity("Error with mapId").build(); return Response.serverError().entity("Error with mapId").build();
} }
Place place = new Place(); Place place = new Place();
place.setMap(map); place.setMap(map);
setUpdatablePlaceValues(place, create); setUpdatablePlaceValues(place, create);
if (create.getProductId() != null && (place.getProduct() == null || !place.getProduct().getId().equals(create.getProductId()))) if (create.getProductId() != null && (place.getProduct() == null || !place.getProduct().getId().equals(create.getProductId())))
return Response.serverError().entity("Product id unknown!").build(); return Response.serverError().entity("Product id unknown!").build();
logger.info("Creating new place {}", place.getName()); logger.info("Creating new place {}", place.getName());
eventmapbean.createPlace(place); eventmapbean.createPlace(place);
return Response.ok().entity(PojoUtils.initPlacePojo(place)).build(); return Response.ok().entity(PojoUtils.initPlacePojo(place)).build();
} catch(Exception e) { } catch (Exception e) {
logger.error("POST /placeadmin/place/ threw an exception", e); logger.error("POST /placeadmin/place/ threw an exception", e);
throw e; throw e;
} }
} }
@PUT @PUT
@Path("/place/{id}") @Path("/place/{id}")
@ApiOperation(value = "Set a place", response = PlacePojo.class) @ApiOperation(value = "Set a place", response = PlacePojo.class)
public Response updatePlace(@PathParam("id") Integer id, PlacePojo update) { public Response updatePlace(@PathParam("id") Integer id, PlacePojo update) {
try { try {
if (update == null || id == null) { if (update == null || id == null) {
return Response.serverError().entity("'id' field is required!").build(); return Response.serverError().entity("'id' field is required!").build();
} }
Place place = eventmapbean.findPlace(id); Place place = eventmapbean.findPlace(id);
if (place == null) { if (place == null) {
return Response.serverError().entity("place not found with id: " + id).build(); return Response.serverError().entity("place not found with id: " + id).build();
} }
setUpdatablePlaceValues(place, update); setUpdatablePlaceValues(place, update);
if (update.getProductId() != null && (place.getProduct() == null || !place.getProduct().getId().equals(update.getProductId()))) if (update.getProductId() != null && (place.getProduct() == null || !place.getProduct().getId().equals(update.getProductId())))
return Response.serverError().entity("Product id unknown!").build(); return Response.serverError().entity("Product id unknown!").build();
place = eventmapbean.updatePlace(place); place = eventmapbean.updatePlace(place);
return Response.ok(PojoUtils.initPlacePojo(place)).build(); return Response.ok(PojoUtils.initPlacePojo(place)).build();
} catch (Exception e) { } catch (Exception e) {
logger.error("PUT /placeadmin/place/{id} threw an exception", e); logger.error("PUT /placeadmin/place/{id} threw an exception", e);
throw e; throw e;
} }
} }
@PUT @PUT
@Path("/place/{placeId}/release") @Path("/place/{placeId}/release")
@ApiOperation("Release a class") @ApiOperation("Release a class")
public Response releasePlace(@PathParam("placeId") Integer placeId) { public Response releasePlace(@PathParam("placeId") Integer placeId) {
try { try {
Place place = eventmapbean.findPlace(placeId); Place place = eventmapbean.findPlace(placeId);
if (place == null) { if (place == null) {
return Response.serverError().entity("Place not found!").build(); return Response.serverError().entity("Place not found!").build();
} }
logger.info("Releasing place {}", place); logger.info("Releasing place {}", place);
placebean.unbuyPlace(place); placebean.unbuyPlace(place);
return Response.ok().build(); return Response.ok().build();
} catch (Exception e) { } catch (Exception e) {
logger.error("PUT /placeadmin/place/{placeId}/release threw an exception", e); logger.error("PUT /placeadmin/place/{placeId}/release threw an exception", e);
throw e; throw e;
} }
} }
@PUT @PUT
@Path("/place/{placeId}/reserve/{eventuserId}") @Path("/place/{placeId}/reserve/{eventuserId}")
@ApiOperation("Reserve a place") @ApiOperation("Reserve a place")
public Response reserve(@PathParam("placeId") Integer placeId, @PathParam("eventuserId") Integer eventuserId) { public Response reserve(@PathParam("placeId") Integer placeId, @PathParam("eventuserId") Integer eventuserId) {
try { try {
Place place = eventmapbean.findPlace(placeId); Place place = eventmapbean.findPlace(placeId);
EventUser eventuser = eventuserbean.findByEventUserId(eventuserId); EventUser eventuser = eventuserbean.findByEventUserId(eventuserId);
if (placebean.reservePlace(place, eventuser)) { if (placebean.reservePlace(place, eventuser)) {
try { try {
placebean.reserveSelectedPlaces(eventuser); placebean.reserveSelectedPlaces(eventuser);
return Response.ok().build(); return Response.ok().build();
} catch (BortalCatchableException e) { } catch (BortalCatchableException e) {
logger.warn("Wtf! Bortalerror", e); logger.warn("Wtf! Bortalerror", e);
} }
} }
return Response.serverError().build(); return Response.serverError().build();
} catch(Exception e) { } catch (Exception e) {
logger.error("PUT /placeadmin/place/{placeId}/reserve/{eventuserId} threw an exception", e); logger.error("PUT /placeadmin/place/{placeId}/reserve/{eventuserId} threw an exception", e);
throw e; throw e;
} }
} }
private void setUpdatablePlaceValues(Place place, PlacePojo update) { private void setUpdatablePlaceValues(Place place, PlacePojo update) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!