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