Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dst.ass2.service.trip.impl;
- import dst.ass2.service.api.match.IMatchingService;
- import dst.ass2.service.api.trip.*;
- import dst.ass2.service.api.trip.rest.ITripServiceResource;
- import javax.inject.Inject;
- import javax.ws.rs.*;
- import javax.ws.rs.core.MediaType;
- import javax.ws.rs.core.Response;
- import javax.ws.rs.ext.Provider;
- @Provider
- @Path("/trips")
- public class TripServiceResource implements ITripServiceResource {
- @Inject
- private ITripService iTripService;
- @Inject
- private IMatchingService iMatchingService;
- @Override
- @POST
- public Response createTrip(@FormParam("riderId") Long riderId,@FormParam("pickupId") Long pickupId,@FormParam("destinationId") Long destinationId)
- throws EntityNotFoundException, InvalidTripException {
- TripDTO tripDTO = iTripService.create(riderId,pickupId,destinationId);
- return Response.ok().entity(tripDTO.getId()).build();
- }
- @Override
- @Path("/{id}/confirm")
- @PATCH
- public Response confirm(@PathParam("id") Long tripId) throws EntityNotFoundException, InvalidTripException {
- iTripService.confirm(tripId);
- return Response.ok().build();
- }
- @Override
- @GET
- @Path("/{id}")
- @Produces(MediaType.APPLICATION_JSON)
- public Response getTrip(@PathParam("id") Long tripId) throws EntityNotFoundException {
- TripDTO tripDTO = iTripService.find(tripId);
- if(tripDTO == null) {
- throw new EntityNotFoundException("Trip with id: " + tripId + " could not be found.");
- }
- return Response.ok().entity(tripDTO).build();
- }
- @Override
- @DELETE
- @Path("/{id}")
- public Response deleteTrip(@PathParam("id") Long tripId) throws EntityNotFoundException {
- iTripService.delete(tripId);
- return Response.ok().build();
- }
- @Override
- @POST
- @Path("/{id}/stops")
- public Response addStop(@PathParam("id") Long tripId,@FormParam("locationId") Long locationId) throws InvalidTripException, EntityNotFoundException {
- TripDTO tripDTO = iTripService.find(tripId);
- if(tripDTO == null) {
- throw new EntityNotFoundException("Trip with id: " + tripId + " could not be found.");
- }
- boolean added = iTripService.addStop(tripDTO,locationId);
- MoneyDTO moneyDTO;
- if(added) {
- try {
- moneyDTO = iMatchingService.calculateFare(tripDTO);
- } catch (InvalidTripException e) {
- moneyDTO = new MoneyDTO();
- }
- return Response.ok().entity(moneyDTO).build();
- } else {
- return Response.status(Response.Status.CONFLICT).build();
- }
- }
- @Override
- @DELETE
- @Path("/{id}/stops/{locationId}")
- public Response removeStop(@PathParam("id") Long tripId,@PathParam("locationId") Long locationId) throws InvalidTripException, EntityNotFoundException {
- TripDTO tripDTO = iTripService.find(tripId);
- if(tripDTO == null) {
- throw new EntityNotFoundException("Trip with id: " + tripId + " could not be found.");
- }
- boolean removed = iTripService.removeStop(tripDTO,locationId);
- if(removed) {
- return Response.ok().build();
- } else {
- return Response.status(Response.Status.CONFLICT).build();
- }
- }
- @Override
- @POST
- @Path("/{id}/match")
- @Consumes(MediaType.APPLICATION_JSON)
- public Response match(@PathParam("id") Long tripId,MatchDTO matchDTO) throws EntityNotFoundException, DriverNotAvailableException {
- iTripService.match(tripId,matchDTO);
- return Response.ok().build();
- }
- @Override
- @POST
- @Path("/{id}/complete")
- @Consumes(MediaType.APPLICATION_JSON)
- public Response complete(@PathParam("id") Long tripId,TripInfoDTO tripInfoDTO) throws EntityNotFoundException {
- iTripService.complete(tripId,tripInfoDTO);
- return Response.ok().build();
- }
- @Override
- @PATCH
- @Path("/{id}/cancel")
- public Response cancel(@PathParam("id") Long tripId) throws EntityNotFoundException {
- iTripService.cancel(tripId);
- return Response.ok().build();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement