| 1 | package edu.ucsb.cs156.rec.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import edu.ucsb.cs156.rec.entities.RequestType; | |
| 5 | import edu.ucsb.cs156.rec.errors.EntityNotFoundException; | |
| 6 | import edu.ucsb.cs156.rec.repositories.RequestTypeRepository; | |
| 7 | import io.swagger.v3.oas.annotations.Operation; | |
| 8 | import io.swagger.v3.oas.annotations.Parameter; | |
| 9 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 10 | import jakarta.validation.Valid; | |
| 11 | import lombok.extern.slf4j.Slf4j; | |
| 12 | import org.springframework.beans.factory.annotation.Autowired; | |
| 13 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 14 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 15 | import org.springframework.web.bind.annotation.GetMapping; | |
| 16 | import org.springframework.web.bind.annotation.PostMapping; | |
| 17 | import org.springframework.web.bind.annotation.PutMapping; | |
| 18 | import org.springframework.web.bind.annotation.RequestBody; | |
| 19 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 20 | import org.springframework.web.bind.annotation.RequestParam; | |
| 21 | import org.springframework.web.bind.annotation.RestController; | |
| 22 | ||
| 23 | /** This is a REST controller for RequestType */ | |
| 24 | @Tag(name = "RequestType") | |
| 25 | @RequestMapping("/api/requesttypes") | |
| 26 | @RestController | |
| 27 | @Slf4j | |
| 28 | public class RequestTypeController extends ApiController { | |
| 29 | ||
| 30 | @Autowired RequestTypeRepository requestTypeRepository; | |
| 31 | ||
| 32 | /** | |
| 33 | * This method returns a list of all Request Types. | |
| 34 | * | |
| 35 | * @return a list of all Request Types. | |
| 36 | */ | |
| 37 | @Operation(summary = "List all request types") | |
| 38 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 39 | @GetMapping("/all") | |
| 40 | public Iterable<RequestType> allRequestTypes() { | |
| 41 | Iterable<RequestType> requestTypes = requestTypeRepository.findAll(); | |
| 42 |
1
1. allRequestTypes : replaced return value with Collections.emptyList for edu/ucsb/cs156/rec/controllers/RequestTypeController::allRequestTypes → KILLED |
return requestTypes; |
| 43 | } | |
| 44 | ||
| 45 | /** | |
| 46 | * Get a single request type by id | |
| 47 | * | |
| 48 | * @param id the id of the request type | |
| 49 | * @return a RequestTYpe | |
| 50 | */ | |
| 51 | @Operation(summary = "Get a single request type") | |
| 52 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 53 | @GetMapping("") | |
| 54 | public RequestType getById(@Parameter(name = "id") @RequestParam Long id) { | |
| 55 | RequestType requestType = | |
| 56 | requestTypeRepository | |
| 57 | .findById(id) | |
| 58 |
1
1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::lambda$getById$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RequestType.class, id)); |
| 59 | ||
| 60 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::getById → KILLED |
return requestType; |
| 61 | } | |
| 62 | ||
| 63 | /** | |
| 64 | * Create a new request type | |
| 65 | * | |
| 66 | * @param requestType the title of the request type | |
| 67 | * @return the saved requesttype | |
| 68 | */ | |
| 69 | @Operation(summary = "Create a new request type") | |
| 70 | @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_PROFESSOR')") | |
| 71 | @PostMapping("/post") | |
| 72 | public RequestType postRequestType( | |
| 73 | @Parameter(name = "requestType") @RequestParam String requestType) | |
| 74 | throws JsonProcessingException { | |
| 75 | ||
| 76 | log.info("requestType={}", requestType); | |
| 77 | ||
| 78 | // Check for duplicates | |
| 79 | Iterable<RequestType> existingRequestTypes = requestTypeRepository.findAll(); | |
| 80 | for (RequestType existing : existingRequestTypes) { | |
| 81 |
1
1. postRequestType : negated conditional → KILLED |
if (existing.getRequestType().equals(requestType)) { |
| 82 | throw new IllegalArgumentException("Duplicate request type: " + requestType); | |
| 83 | } | |
| 84 | } | |
| 85 | ||
| 86 | // Create new request type | |
| 87 | RequestType requestTypeNew = new RequestType(); | |
| 88 |
1
1. postRequestType : removed call to edu/ucsb/cs156/rec/entities/RequestType::setRequestType → KILLED |
requestTypeNew.setRequestType(requestType); |
| 89 | ||
| 90 | RequestType savedRequestType = requestTypeRepository.save(requestTypeNew); | |
| 91 | ||
| 92 |
1
1. postRequestType : replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::postRequestType → KILLED |
return savedRequestType; |
| 93 | } | |
| 94 | ||
| 95 | /** | |
| 96 | * Delete a UCSBDate | |
| 97 | * | |
| 98 | * @param id the id of the request type to delete | |
| 99 | * @return a message indicating the date was deleted | |
| 100 | */ | |
| 101 | @Operation(summary = "Delete a request type") | |
| 102 | @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_PROFESSOR')") | |
| 103 | @DeleteMapping("") | |
| 104 | public Object deleteRequestType(@Parameter(name = "id") @RequestParam Long id) { | |
| 105 | RequestType requestType = | |
| 106 | requestTypeRepository | |
| 107 | .findById(id) | |
| 108 |
1
1. lambda$deleteRequestType$1 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::lambda$deleteRequestType$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RequestType.class, id)); |
| 109 | ||
| 110 |
1
1. deleteRequestType : removed call to edu/ucsb/cs156/rec/repositories/RequestTypeRepository::delete → KILLED |
requestTypeRepository.delete(requestType); |
| 111 |
1
1. deleteRequestType : replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::deleteRequestType → KILLED |
return genericMessage("Request type with id %s deleted".formatted(id)); |
| 112 | } | |
| 113 | ||
| 114 | /** | |
| 115 | * Update a single request type | |
| 116 | * | |
| 117 | * @param id id of the request to update | |
| 118 | * @param incoming the new request type | |
| 119 | * @return the updated request type object | |
| 120 | */ | |
| 121 | @Operation(summary = "Update a single request type") | |
| 122 | @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_PROFESSOR')") | |
| 123 | @PutMapping("") | |
| 124 | public RequestType updateRequestType( | |
| 125 | @Parameter(name = "id") @RequestParam Long id, @RequestBody @Valid RequestType incoming) { | |
| 126 | ||
| 127 | RequestType requestType = | |
| 128 | requestTypeRepository | |
| 129 | .findById(id) | |
| 130 |
1
1. lambda$updateRequestType$2 : replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::lambda$updateRequestType$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RequestType.class, id)); |
| 131 | ||
| 132 | // Check for duplicates | |
| 133 | Iterable<RequestType> existingRequestTypes = requestTypeRepository.findAll(); | |
| 134 | for (RequestType existing : existingRequestTypes) { | |
| 135 |
1
1. updateRequestType : negated conditional → KILLED |
if (incoming.getRequestType().isEmpty()) { |
| 136 | throw new IllegalArgumentException("Request type cannot be empty"); | |
| 137 | } | |
| 138 |
1
1. updateRequestType : negated conditional → KILLED |
if (existing.getId() == id) { |
| 139 | continue; | |
| 140 | } | |
| 141 |
1
1. updateRequestType : negated conditional → KILLED |
if (existing.getRequestType().equals(incoming.getRequestType())) { |
| 142 | throw new IllegalArgumentException("Duplicate request type: " + incoming); | |
| 143 | } | |
| 144 | } | |
| 145 | ||
| 146 | // Update and save the request type | |
| 147 |
1
1. updateRequestType : removed call to edu/ucsb/cs156/rec/entities/RequestType::setRequestType → KILLED |
requestType.setRequestType(incoming.getRequestType()); |
| 148 | requestTypeRepository.save(requestType); | |
| 149 | ||
| 150 |
1
1. updateRequestType : replaced return value with null for edu/ucsb/cs156/rec/controllers/RequestTypeController::updateRequestType → KILLED |
return requestType; |
| 151 | } | |
| 152 | } | |
Mutations | ||
| 42 |
1.1 |
|
| 58 |
1.1 |
|
| 60 |
1.1 |
|
| 81 |
1.1 |
|
| 88 |
1.1 |
|
| 92 |
1.1 |
|
| 108 |
1.1 |
|
| 110 |
1.1 |
|
| 111 |
1.1 |
|
| 130 |
1.1 |
|
| 135 |
1.1 |
|
| 138 |
1.1 |
|
| 141 |
1.1 |
|
| 147 |
1.1 |
|
| 150 |
1.1 |