| 1 | package edu.ucsb.cs156.example.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.example.entities.UCSBDate; | |
| 4 | import edu.ucsb.cs156.example.repositories.UCSBDateRepository; | |
| 5 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 6 | import io.swagger.v3.oas.annotations.Operation; | |
| 7 | import io.swagger.v3.oas.annotations.Parameter; | |
| 8 | import lombok.extern.slf4j.Slf4j; | |
| 9 | ||
| 10 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 11 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 12 | ||
| 13 | import org.springframework.beans.factory.annotation.Autowired; | |
| 14 | import org.springframework.format.annotation.DateTimeFormat; | |
| 15 | import org.springframework.http.ResponseEntity; | |
| 16 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 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.RequestMapping; | |
| 23 | import org.springframework.web.bind.annotation.RequestParam; | |
| 24 | import org.springframework.web.bind.annotation.RestController; | |
| 25 | ||
| 26 | import jakarta.validation.Valid; | |
| 27 | ||
| 28 | import java.time.LocalDateTime; | |
| 29 | import java.util.Optional; | |
| 30 | ||
| 31 | /** | |
| 32 |  * This is a REST controller for UCSBDates | |
| 33 |  */ | |
| 34 | ||
| 35 | @Tag(name = "UCSBDates") | |
| 36 | @RequestMapping("/api/ucsbdates") | |
| 37 | @RestController | |
| 38 | @Slf4j | |
| 39 | public class UCSBDatesController extends ApiController { | |
| 40 | ||
| 41 |     /** | |
| 42 |      * This inner class helps us factor out some code for checking | |
| 43 |      * whether UCSBDate's exist, | |
| 44 |      * along with the error messages pertaining to those situations. It | |
| 45 |      * bundles together the state needed for those checks. | |
| 46 |      */ | |
| 47 |     private static class UCSBDateOrError { | |
| 48 |         Long id; | |
| 49 |         UCSBDate ucsbDate; | |
| 50 |         ResponseEntity<String> error; | |
| 51 | ||
| 52 |         public UCSBDateOrError(Long id) { | |
| 53 |             this.id = id; | |
| 54 |         } | |
| 55 |     } | |
| 56 | ||
| 57 |     @Autowired | |
| 58 |     UCSBDateRepository ucsbDateRepository; | |
| 59 | ||
| 60 |     @Autowired | |
| 61 |     ObjectMapper mapper; | |
| 62 | ||
| 63 |     /** | |
| 64 |      * List all UCSB dates | |
| 65 |      *  | |
| 66 |      * @return an iterable of UCSBDate | |
| 67 |      */ | |
| 68 | ||
| 69 |     @Operation(summary = "List all ucsb dates") | |
| 70 |     @PreAuthorize("hasRole('ROLE_USER')") | |
| 71 |     @GetMapping("/all") | |
| 72 |     public Iterable<UCSBDate> allUCSBDates() { | |
| 73 |         Iterable<UCSBDate> dates = ucsbDateRepository.findAll(); | |
| 74 | 1
1. allUCSBDates : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/UCSBDatesController::allUCSBDates → KILLED |         return dates; | 
| 75 |     } | |
| 76 | ||
| 77 |     /** | |
| 78 |      * Get a single date by id | |
| 79 |      *  | |
| 80 |      * @param id the id of the date | |
| 81 |      * @return a UCSBDate | |
| 82 |      * @throws JsonProcessingException if there is an error processing the JSON | |
| 83 |      */ | |
| 84 |     @Operation(summary = "Get a single date") | |
| 85 |     @PreAuthorize("hasRole('ROLE_USER')") | |
| 86 |     @GetMapping("") | |
| 87 |     public ResponseEntity<String> getById( | |
| 88 |             @Parameter(name = "id") @RequestParam Long id) throws JsonProcessingException { | |
| 89 |         UCSBDateOrError uoe = new UCSBDateOrError(id); | |
| 90 | ||
| 91 |         uoe = doesUCSBDateExist(uoe); | |
| 92 | 1
1. getById : negated conditional → KILLED |         if (uoe.error != null) { | 
| 93 | 1
1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/UCSBDatesController::getById → KILLED |             return uoe.error; | 
| 94 |         } | |
| 95 | ||
| 96 |         String body = mapper.writeValueAsString(uoe.ucsbDate); | |
| 97 | 1
1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/UCSBDatesController::getById → KILLED |         return ResponseEntity.ok().body(body); | 
| 98 |     } | |
| 99 | ||
| 100 |     /** | |
| 101 |      * Create a new date | |
| 102 |      *  | |
| 103 |      * @param quarterYYYYQ  the quarter in the format YYYYQ | |
| 104 |      * @param name          the name of the date | |
| 105 |      * @param localDateTime the date | |
| 106 |      * @return a ResponseEntity with the new date | |
| 107 |      * @throws JsonProcessingException if there is an error processing the JSON | |
| 108 |      */ | |
| 109 |     @Operation(summary = "Create a new date") | |
| 110 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 111 |     @PostMapping("/post") | |
| 112 |     public ResponseEntity<String> postUCSBDate( | |
| 113 |             @Parameter(name = "quarterYYYYQ") @RequestParam String quarterYYYYQ, | |
| 114 |             @Parameter(name = "name") @RequestParam String name, | |
| 115 |             @Parameter(name = "date (in iso format, e.g. YYYY-mm-ddTHH:MM:SS; see https://en.wikipedia.org/wiki/ISO_8601)") @RequestParam("localDateTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime localDateTime) | |
| 116 |             throws JsonProcessingException { | |
| 117 | ||
| 118 |         // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) | |
| 119 |         // See: https://www.baeldung.com/spring-date-parameters | |
| 120 | ||
| 121 |         log.info("localDateTime={}", localDateTime); | |
| 122 | ||
| 123 |         UCSBDate ucsbDate = new UCSBDate(); | |
| 124 | 1
1. postUCSBDate : removed call to edu/ucsb/cs156/example/entities/UCSBDate::setQuarterYYYYQ → KILLED |         ucsbDate.setQuarterYYYYQ(quarterYYYYQ); | 
| 125 | 1
1. postUCSBDate : removed call to edu/ucsb/cs156/example/entities/UCSBDate::setName → KILLED |         ucsbDate.setName(name); | 
| 126 | 1
1. postUCSBDate : removed call to edu/ucsb/cs156/example/entities/UCSBDate::setLocalDateTime → KILLED |         ucsbDate.setLocalDateTime(localDateTime); | 
| 127 | ||
| 128 |         UCSBDate savedUcsbDate = ucsbDateRepository.save(ucsbDate); | |
| 129 |         String json = mapper.writeValueAsString(savedUcsbDate); | |
| 130 | 1
1. postUCSBDate : replaced return value with null for edu/ucsb/cs156/example/controllers/UCSBDatesController::postUCSBDate → KILLED |         return ResponseEntity.ok().body(json); | 
| 131 |     } | |
| 132 | ||
| 133 |     /** | |
| 134 |      * Delete a UCSBDate | |
| 135 |      *  | |
| 136 |      * @param id the id of the date to delete | |
| 137 |      * @return a ResponseEntity with a message | |
| 138 |      */ | |
| 139 |     @Operation(summary = "Delete a UCSBDate") | |
| 140 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 141 |     @DeleteMapping("") | |
| 142 |     public ResponseEntity<String> deleteUCSBDate( | |
| 143 |             @Parameter(name = "id") @RequestParam Long id) { | |
| 144 |         UCSBDateOrError uoe = new UCSBDateOrError(id); | |
| 145 | ||
| 146 |         uoe = doesUCSBDateExist(uoe); | |
| 147 | 1
1. deleteUCSBDate : negated conditional → KILLED |         if (uoe.error != null) { | 
| 148 | 1
1. deleteUCSBDate : replaced return value with null for edu/ucsb/cs156/example/controllers/UCSBDatesController::deleteUCSBDate → KILLED |             return uoe.error; | 
| 149 |         } | |
| 150 | ||
| 151 | 1
1. deleteUCSBDate : removed call to edu/ucsb/cs156/example/repositories/UCSBDateRepository::deleteById → KILLED |         ucsbDateRepository.deleteById(id); | 
| 152 | 1
1. deleteUCSBDate : replaced return value with null for edu/ucsb/cs156/example/controllers/UCSBDatesController::deleteUCSBDate → KILLED |         return ResponseEntity.ok().body(String.format("UCSBDate with id %d deleted", id)); | 
| 153 |     } | |
| 154 | ||
| 155 |     /** | |
| 156 |      * Update a single date | |
| 157 |      *  | |
| 158 |      * @param id       id of the date to update | |
| 159 |      * @param incoming the new date | |
| 160 |      * @return response entity with the updated date | |
| 161 |      * @throws JsonProcessingException if there is an error processing the JSON | |
| 162 |      */ | |
| 163 |     @Operation(summary = "Update a single date") | |
| 164 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 165 |     @PutMapping("") | |
| 166 |     public ResponseEntity<String> updateUCSBDate( | |
| 167 |             @Parameter(name = "id") @RequestParam Long id, | |
| 168 |             @RequestBody @Valid UCSBDate incoming) throws JsonProcessingException { | |
| 169 |         UCSBDateOrError uoe = new UCSBDateOrError(id); | |
| 170 | ||
| 171 |         uoe = doesUCSBDateExist(uoe); | |
| 172 | 1
1. updateUCSBDate : negated conditional → KILLED |         if (uoe.error != null) { | 
| 173 | 1
1. updateUCSBDate : replaced return value with null for edu/ucsb/cs156/example/controllers/UCSBDatesController::updateUCSBDate → KILLED |             return uoe.error; | 
| 174 |         } | |
| 175 | ||
| 176 |         UCSBDate oldDate = uoe.ucsbDate; | |
| 177 | 1
1. updateUCSBDate : removed call to edu/ucsb/cs156/example/entities/UCSBDate::setQuarterYYYYQ → KILLED |         oldDate.setQuarterYYYYQ(incoming.getQuarterYYYYQ()); | 
| 178 | 1
1. updateUCSBDate : removed call to edu/ucsb/cs156/example/entities/UCSBDate::setName → KILLED |         oldDate.setName(incoming.getName()); | 
| 179 | 1
1. updateUCSBDate : removed call to edu/ucsb/cs156/example/entities/UCSBDate::setLocalDateTime → KILLED |         oldDate.setLocalDateTime(incoming.getLocalDateTime()); | 
| 180 | ||
| 181 |         ucsbDateRepository.save(oldDate); | |
| 182 | ||
| 183 |         String body = mapper.writeValueAsString(oldDate); | |
| 184 | 1
1. updateUCSBDate : replaced return value with null for edu/ucsb/cs156/example/controllers/UCSBDatesController::updateUCSBDate → KILLED |         return ResponseEntity.ok().body(body); | 
| 185 |     } | |
| 186 | ||
| 187 |     /** | |
| 188 |      * Pre-conditions: uoe.id is value to look up,  | |
| 189 |      * uoe.ucsbDate and uoe.error are null | |
| 190 |      * | |
| 191 |      * Post-condition: if UCSBDate with id uoe.id exists, uoe.ucsbDate now refers to | |
| 192 |      * it, and error is null. | |
| 193 |      *  | |
| 194 |      * Otherwise, UCSBDate with id uoe.id does not exist, and error is a suitable | |
| 195 |      * return value to report this error condition. | |
| 196 |      *  | |
| 197 |      * @param uoe the UCSBDateOrError object | |
| 198 |      * @return the UCSBDateOrError object | |
| 199 |      */ | |
| 200 |     public UCSBDateOrError doesUCSBDateExist(UCSBDateOrError uoe) { | |
| 201 | ||
| 202 |         Optional<UCSBDate> optionalUCSBDate = ucsbDateRepository.findById(uoe.id); | |
| 203 | ||
| 204 | 1
1. doesUCSBDateExist : negated conditional → KILLED |         if (optionalUCSBDate.isEmpty()) { | 
| 205 |             uoe.error = ResponseEntity | |
| 206 |                     .badRequest() | |
| 207 |                     .body(String.format("UCSBDate with id %d not found", uoe.id)); | |
| 208 |         } else { | |
| 209 |             uoe.ucsbDate = optionalUCSBDate.get(); | |
| 210 |         } | |
| 211 | 1
1. doesUCSBDateExist : replaced return value with null for edu/ucsb/cs156/example/controllers/UCSBDatesController::doesUCSBDateExist → KILLED |         return uoe; | 
| 212 |     } | |
| 213 | ||
| 214 | } | |
| Mutations | ||
| 74 | 1.1 | |
| 92 | 1.1 | |
| 93 | 1.1 | |
| 97 | 1.1 | |
| 124 | 1.1 | |
| 125 | 1.1 | |
| 126 | 1.1 | |
| 130 | 1.1 | |
| 147 | 1.1 | |
| 148 | 1.1 | |
| 151 | 1.1 | |
| 152 | 1.1 | |
| 172 | 1.1 | |
| 173 | 1.1 | |
| 177 | 1.1 | |
| 178 | 1.1 | |
| 179 | 1.1 | |
| 184 | 1.1 | |
| 204 | 1.1 | |
| 211 | 1.1 |