Implement TripService REST (2.1.3.1)

This commit is contained in:
2021-04-29 11:23:36 +02:00
parent f5298764ba
commit 15b6d0a4a5
2 changed files with 104 additions and 16 deletions
@@ -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();
}
}