Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.22 KB | None | 0 0
  1. package dst.ass2.service.trip.impl;
  2.  
  3. import dst.ass2.service.api.match.IMatchingService;
  4. import dst.ass2.service.api.trip.*;
  5. import dst.ass2.service.api.trip.rest.ITripServiceResource;
  6.  
  7. import javax.inject.Inject;
  8. import javax.ws.rs.*;
  9. import javax.ws.rs.core.MediaType;
  10. import javax.ws.rs.core.Response;
  11. import javax.ws.rs.ext.Provider;
  12.  
  13. @Provider
  14. @Path("/trips")
  15. public class TripServiceResource implements ITripServiceResource {
  16.  
  17.     @Inject
  18.     private ITripService iTripService;
  19.     @Inject
  20.     private IMatchingService iMatchingService;
  21.  
  22.     @Override
  23.     @POST
  24.     public Response createTrip(@FormParam("riderId") Long riderId,@FormParam("pickupId") Long pickupId,@FormParam("destinationId") Long destinationId)
  25.             throws EntityNotFoundException, InvalidTripException {
  26.  
  27.         TripDTO tripDTO = iTripService.create(riderId,pickupId,destinationId);
  28.  
  29.         return Response.ok().entity(tripDTO.getId()).build();
  30.     }
  31.  
  32.     @Override
  33.     @Path("/{id}/confirm")
  34.     @PATCH
  35.     public Response confirm(@PathParam("id") Long tripId) throws EntityNotFoundException, InvalidTripException {
  36.         iTripService.confirm(tripId);
  37.         return Response.ok().build();
  38.     }
  39.  
  40.     @Override
  41.     @GET
  42.     @Path("/{id}")
  43.     @Produces(MediaType.APPLICATION_JSON)
  44.     public Response getTrip(@PathParam("id") Long tripId) throws EntityNotFoundException {
  45.         TripDTO tripDTO = iTripService.find(tripId);
  46.         if(tripDTO == null) {
  47.             throw new EntityNotFoundException("Trip with id: " + tripId + " could not be found.");
  48.         }
  49.         return Response.ok().entity(tripDTO).build();
  50.     }
  51.  
  52.     @Override
  53.     @DELETE
  54.     @Path("/{id}")
  55.     public Response deleteTrip(@PathParam("id") Long tripId) throws EntityNotFoundException {
  56.         iTripService.delete(tripId);
  57.         return Response.ok().build();
  58.     }
  59.  
  60.     @Override
  61.     @POST
  62.     @Path("/{id}/stops")
  63.     public Response addStop(@PathParam("id") Long tripId,@FormParam("locationId") Long locationId) throws InvalidTripException, EntityNotFoundException {
  64.         TripDTO tripDTO = iTripService.find(tripId);
  65.         if(tripDTO == null) {
  66.             throw new EntityNotFoundException("Trip with id: " + tripId + " could not be found.");
  67.         }
  68.         boolean added = iTripService.addStop(tripDTO,locationId);
  69.         MoneyDTO moneyDTO;
  70.         if(added) {
  71.             try {
  72.                 moneyDTO = iMatchingService.calculateFare(tripDTO);
  73.             } catch (InvalidTripException e) {
  74.                 moneyDTO = new MoneyDTO();
  75.             }
  76.             return Response.ok().entity(moneyDTO).build();
  77.         } else {
  78.             return Response.status(Response.Status.CONFLICT).build();
  79.         }
  80.  
  81.     }
  82.  
  83.     @Override
  84.     @DELETE
  85.     @Path("/{id}/stops/{locationId}")
  86.     public Response removeStop(@PathParam("id") Long tripId,@PathParam("locationId") Long locationId) throws InvalidTripException, EntityNotFoundException {
  87.         TripDTO tripDTO = iTripService.find(tripId);
  88.         if(tripDTO == null) {
  89.             throw new EntityNotFoundException("Trip with id: " + tripId + " could not be found.");
  90.         }
  91.         boolean removed = iTripService.removeStop(tripDTO,locationId);
  92.         if(removed) {
  93.             return Response.ok().build();
  94.         } else {
  95.             return Response.status(Response.Status.CONFLICT).build();
  96.         }
  97.  
  98.     }
  99.  
  100.     @Override
  101.     @POST
  102.     @Path("/{id}/match")
  103.     @Consumes(MediaType.APPLICATION_JSON)
  104.     public Response match(@PathParam("id") Long tripId,MatchDTO matchDTO) throws EntityNotFoundException, DriverNotAvailableException {
  105.         iTripService.match(tripId,matchDTO);
  106.         return Response.ok().build();
  107.     }
  108.  
  109.     @Override
  110.     @POST
  111.     @Path("/{id}/complete")
  112.     @Consumes(MediaType.APPLICATION_JSON)
  113.     public Response complete(@PathParam("id") Long tripId,TripInfoDTO tripInfoDTO) throws EntityNotFoundException {
  114.         iTripService.complete(tripId,tripInfoDTO);
  115.         return Response.ok().build();
  116.     }
  117.  
  118.     @Override
  119.     @PATCH
  120.     @Path("/{id}/cancel")
  121.     public Response cancel(@PathParam("id") Long tripId) throws EntityNotFoundException {
  122.         iTripService.cancel(tripId);
  123.         return Response.ok().build();
  124.     }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement