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