From 15b6d0a4a576a6a9b40fb734b7ab213f85df2998 Mon Sep 17 00:00:00 2001 From: Tobias Eidelpes Date: Thu, 29 Apr 2021 11:23:36 +0200 Subject: [PATCH] Implement TripService REST (2.1.3.1) --- .../api/trip/rest/ITripServiceResource.java | 45 +++++++---- .../trip/impl/TripServiceResource.java | 75 +++++++++++++++++++ 2 files changed, 104 insertions(+), 16 deletions(-) create mode 100644 ass2-service/trip/src/main/java/dst/ass2/service/trip/impl/TripServiceResource.java diff --git a/ass2-service/api/src/main/java/dst/ass2/service/api/trip/rest/ITripServiceResource.java b/ass2-service/api/src/main/java/dst/ass2/service/api/trip/rest/ITripServiceResource.java index 2ee8a06..77e5292 100644 --- a/ass2-service/api/src/main/java/dst/ass2/service/api/trip/rest/ITripServiceResource.java +++ b/ass2-service/api/src/main/java/dst/ass2/service/api/trip/rest/ITripServiceResource.java @@ -2,35 +2,48 @@ package dst.ass2.service.api.trip.rest; import dst.ass2.service.api.trip.*; +import javax.ws.rs.*; import javax.ws.rs.core.Response; /** * This interface exposes the {@code ITripService} as a RESTful interface. */ +@Path("trips") public interface ITripServiceResource { + @POST + Response createTrip(@FormParam("riderId") Long riderId, @FormParam("pickupId") Long pickupId, @FormParam("destinationId") Long destinationId) + throws EntityNotFoundException, InvalidTripException; - // TODO annotate the class and methods with the correct javax.ws.rs annotations + @PATCH + @Path("{id}/confirm") + Response confirm(@PathParam("id") Long tripId) throws EntityNotFoundException, InvalidTripException; - Response createTrip(Long riderId, Long pickupId, Long destinationId) - throws EntityNotFoundException, InvalidTripException; + @GET + @Path("{id}") + Response getTrip(@PathParam("id") Long tripId) throws EntityNotFoundException; - Response confirm(Long tripId) throws EntityNotFoundException, InvalidTripException; + @DELETE + @Path("{id}") + Response deleteTrip(@PathParam("id") Long tripId) throws EntityNotFoundException; - Response getTrip(Long tripId) throws EntityNotFoundException; + @POST + @Path("{id}/stops") + Response addStop(@PathParam("id") Long tripId, @FormParam("locationId") Long locationId) throws InvalidTripException, EntityNotFoundException; - Response deleteTrip(Long tripId) throws EntityNotFoundException; - - Response addStop(Long tripId, Long locationId) throws InvalidTripException, EntityNotFoundException; - - Response removeStop(Long tripId, Long locationId) throws InvalidTripException, EntityNotFoundException; - - Response match(Long tripId, MatchDTO matchDTO) throws EntityNotFoundException, DriverNotAvailableException; - - Response complete(Long tripId, TripInfoDTO tripInfoDTO) throws EntityNotFoundException; - - Response cancel(Long tripId) throws EntityNotFoundException; + @DELETE + @Path("{id}/stops") + Response removeStop(@PathParam("id") Long tripId, @FormParam("locationId") Long locationId) throws InvalidTripException, EntityNotFoundException; + @POST + @Path("{id}/match") + Response match(@PathParam("id") Long tripId, MatchDTO matchDTO) throws EntityNotFoundException, DriverNotAvailableException; + @POST + @Path("{id}/complete") + Response complete(@PathParam("id") Long tripId, TripInfoDTO tripInfoDTO) throws EntityNotFoundException; + @PATCH + @Path("{id}/cancel") + Response cancel(@PathParam("id") Long tripId) throws EntityNotFoundException; } diff --git a/ass2-service/trip/src/main/java/dst/ass2/service/trip/impl/TripServiceResource.java b/ass2-service/trip/src/main/java/dst/ass2/service/trip/impl/TripServiceResource.java new file mode 100644 index 0000000..33f3ff5 --- /dev/null +++ b/ass2-service/trip/src/main/java/dst/ass2/service/trip/impl/TripServiceResource.java @@ -0,0 +1,75 @@ +package dst.ass2.service.trip.impl; + +import dst.ass1.jpa.model.ITrip; +import dst.ass2.service.api.trip.*; +import dst.ass2.service.api.trip.rest.ITripServiceResource; + +import javax.annotation.Resource; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.Provider; + +@Provider +public class TripServiceResource implements ITripServiceResource { + @Resource + private ITripService tripService; + + @Override + public Response createTrip(Long riderId, Long pickupId, Long destinationId) throws EntityNotFoundException, InvalidTripException { + TripDTO trip = tripService.create(riderId, pickupId, destinationId); + return Response.ok(trip.getId()).build(); + } + + @Override + public Response confirm(Long tripId) throws EntityNotFoundException, InvalidTripException { + tripService.confirm(tripId); + return Response.ok().build(); + } + + @Override + public Response getTrip(Long tripId) throws EntityNotFoundException { + TripDTO trip = tripService.find(tripId); + if (trip == null) + throw new EntityNotFoundException("Could not find trip with id " + tripId); + return Response.ok(trip).build(); + } + + @Override + public Response deleteTrip(Long tripId) throws EntityNotFoundException { + tripService.delete(tripId); + return Response.ok().build(); + } + + @Override + public Response addStop(Long tripId, Long locationId) throws InvalidTripException, EntityNotFoundException { + TripDTO trip = tripService.find(tripId); + if (!tripService.addStop(trip, locationId)) + throw new InvalidTripException("Could not add stop with id " + locationId + " to trip with id " + tripId); + return Response.ok(trip.getFare()).build(); + } + + @Override + public Response removeStop(Long tripId, Long locationId) throws InvalidTripException, EntityNotFoundException { + TripDTO trip = tripService.find(tripId); + if (!tripService.removeStop(trip, locationId)) + throw new InvalidTripException("Could not remove stop with id " + locationId + " from trip with id " + tripId); + return Response.ok().build(); + } + + @Override + public Response match(Long tripId, MatchDTO matchDTO) throws EntityNotFoundException, DriverNotAvailableException { + tripService.match(tripId, matchDTO); + return Response.ok(matchDTO).build(); + } + + @Override + public Response complete(Long tripId, TripInfoDTO tripInfoDTO) throws EntityNotFoundException { + tripService.complete(tripId, tripInfoDTO); + return Response.ok(tripInfoDTO).build(); + } + + @Override + public Response cancel(Long tripId) throws EntityNotFoundException { + tripService.cancel(tripId); + return Response.ok().build(); + } +}