ShiftController.java

  1. package edu.ucsb.cs156.gauchoride.controllers;

  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import com.fasterxml.jackson.databind.ObjectMapper;

  4. import edu.ucsb.cs156.gauchoride.entities.Ride;
  5. import edu.ucsb.cs156.gauchoride.entities.Shift;
  6. import edu.ucsb.cs156.gauchoride.repositories.ShiftRepository;
  7. import edu.ucsb.cs156.gauchoride.repositories.UserRepository;
  8. import edu.ucsb.cs156.gauchoride.errors.EntityNotFoundException;
  9. import edu.ucsb.cs156.gauchoride.models.CurrentUser;

  10. import java.nio.file.AccessDeniedException;
  11. import java.time.LocalTime;

  12. import javax.validation.Valid;

  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.http.ResponseEntity;
  15. import org.springframework.security.access.prepost.PreAuthorize;
  16. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  17. import org.springframework.web.bind.annotation.DeleteMapping;
  18. import org.springframework.web.bind.annotation.GetMapping;
  19. import org.springframework.web.bind.annotation.PostMapping;
  20. import org.springframework.web.bind.annotation.PutMapping;
  21. import org.springframework.web.bind.annotation.RequestBody;
  22. import org.springframework.web.bind.annotation.PathVariable;
  23. import org.springframework.web.bind.annotation.RequestMapping;
  24. import org.springframework.web.bind.annotation.RequestParam;
  25. import org.springframework.web.bind.annotation.RestController;

  26. import io.swagger.v3.oas.annotations.tags.Tag;
  27. import io.swagger.v3.oas.annotations.Operation;
  28. import io.swagger.v3.oas.annotations.Parameter;


  29. @Tag(name = "Shift information")
  30. @RequestMapping("/api/shift")
  31. @RestController
  32. public class ShiftController extends ApiController {
  33.     @Autowired
  34.     ShiftRepository shiftRepository;

  35.     @Autowired
  36.     ObjectMapper mapper;

  37.     @Operation(summary = "Get a list of all shifts")
  38.     @PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_DRIVER') || hasRole('ROLE_RIDER')")
  39.     @GetMapping("/all")
  40.     public ResponseEntity<String> allShifts()
  41.             throws JsonProcessingException {
  42.         Iterable<Shift> shifts = shiftRepository.findAll();
  43.         String body = mapper.writeValueAsString(shifts);
  44.         return ResponseEntity.ok().body(body);
  45.     }

  46.     @Operation(summary = "Get shift by id")
  47.     @PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_DRIVER') || hasRole('ROLE_RIDER')")
  48.     @GetMapping("/get")
  49.     public Shift shiftByID(
  50.             @Parameter(name = "id", description = "Long, id number of shift to get", example = "1", required = true) @RequestParam Long id)
  51.             throws JsonProcessingException {
  52.         Shift shift = shiftRepository.findById(id)
  53.                 .orElseThrow(() -> new EntityNotFoundException(Shift.class, id));
  54.         return shift;
  55.     }

  56.     @Operation(summary = "Get a list of all shifts for given driverId")
  57.     @PreAuthorize("hasRole('ROLE_DRIVER')")
  58.     @GetMapping("/drivershifts")
  59.     public ResponseEntity<String> driverShifts()
  60.             throws JsonProcessingException {
  61.         Long driverID = super.getCurrentUser().getUser().getId();
  62.         Iterable<Shift> shifts = shiftRepository.findByDriverID(driverID);
  63.         String body = mapper.writeValueAsString(shifts);
  64.         return ResponseEntity.ok().body(body);
  65.     }


  66.     @Operation(summary = "Create a new shift")
  67.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  68.     @PostMapping("/post")
  69.     public Shift postShift(
  70.         @Parameter(name="day", description ="String, day of the week", example = "Monday") @RequestParam String day,
  71.         @Parameter(name="shiftStart", description="String, Format: HH:MM(A/P)M", example="11:00AM") @RequestParam String shiftStart,
  72.         @Parameter(name="shiftEnd", description="String, Format: HH:MM(A/P)M", example="11:00AM") @RequestParam String shiftEnd,
  73.         @Parameter(name="driverID", description = "Long, driver ID") @RequestParam long driverID ,
  74.         @Parameter(name="driverBackupID", description = "Long, driver backup id") @RequestParam long driverBackupID
  75.         )
  76.         {

  77.         Shift shift = new Shift();

  78.         shift.setDriverID(driverID);
  79.         shift.setDay(day);
  80.         shift.setShiftStart(shiftStart);
  81.         shift.setShiftEnd(shiftEnd);
  82.         shift.setDriverBackupID(driverBackupID);

  83.         Shift savedShift = shiftRepository.save(shift);

  84.         return savedShift;
  85.     }



  86.     @Operation(summary= "Delete a Shift")
  87.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  88.     @DeleteMapping("")
  89.     public Object deleteShift(
  90.         @Parameter(name="id", description = "Long, id number of shift to get", example = "1", required = true)
  91.         @RequestParam long id){
  92.             Shift shift = shiftRepository.findById(id)
  93.                     .orElseThrow(() -> new EntityNotFoundException(Shift.class, id));
  94.    
  95.             shiftRepository.delete(shift);
  96.             return genericMessage("Shift with id %s deleted".formatted(id));

  97.     }


  98.     @Operation(summary = "Update a single shift")
  99.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  100.     @PutMapping("")
  101.     public Shift updateShift(
  102.             @Parameter(name="id", description = "Long, id number of shift to get", example = "1", required = true)
  103.             @RequestParam Long id,
  104.             @RequestBody @Valid Shift incoming) {

  105.         Shift shift = shiftRepository.findById(id)
  106.                     .orElseThrow(() -> new EntityNotFoundException(Shift.class, id));

  107.        
  108.         shift.setDay(incoming.getDay());
  109.         shift.setShiftStart(incoming.getShiftStart());
  110.         shift.setShiftEnd(incoming.getShiftEnd());
  111.         shift.setDriverID(incoming.getDriverID());
  112.         shift.setDriverBackupID(incoming.getDriverBackupID());

  113.         shiftRepository.save(shift);

  114.         return shift;
  115.     }

  116. }