Implement TripService REST (2.1.3.1)

This commit is contained in:
Tobias Eidelpes 2021-04-29 11:23:36 +02:00
parent f5298764ba
commit 15b6d0a4a5
2 changed files with 104 additions and 16 deletions

View File

@ -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 {
// TODO annotate the class and methods with the correct javax.ws.rs annotations
Response createTrip(Long riderId, Long pickupId, Long destinationId)
@POST
Response createTrip(@FormParam("riderId") Long riderId, @FormParam("pickupId") Long pickupId, @FormParam("destinationId") Long destinationId)
throws EntityNotFoundException, InvalidTripException;
Response confirm(Long tripId) throws EntityNotFoundException, InvalidTripException;
@PATCH
@Path("{id}/confirm")
Response confirm(@PathParam("id") Long tripId) throws EntityNotFoundException, InvalidTripException;
Response getTrip(Long tripId) throws EntityNotFoundException;
@GET
@Path("{id}")
Response getTrip(@PathParam("id") Long tripId) throws EntityNotFoundException;
Response deleteTrip(Long tripId) throws EntityNotFoundException;
@DELETE
@Path("{id}")
Response deleteTrip(@PathParam("id") Long tripId) throws EntityNotFoundException;
Response addStop(Long tripId, Long locationId) throws InvalidTripException, EntityNotFoundException;
@POST
@Path("{id}/stops")
Response addStop(@PathParam("id") Long tripId, @FormParam("locationId") 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;
}

View File

@ -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();
}
}