1 | package edu.ucsb.cs156.happiercows.controllers; | |
2 | ||
3 | import edu.ucsb.cs156.happiercows.entities.Announcements; | |
4 | import edu.ucsb.cs156.happiercows.repositories.AnnouncementsRepository; | |
5 | import edu.ucsb.cs156.happiercows.repositories.CommonsRepository; | |
6 | ||
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 lombok.extern.slf4j.Slf4j; | |
11 | ||
12 | import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException; | |
13 | import com.fasterxml.jackson.core.JsonProcessingException; | |
14 | import com.fasterxml.jackson.databind.ObjectMapper; | |
15 | ||
16 | import org.springframework.beans.factory.annotation.Autowired; | |
17 | import org.springframework.format.annotation.DateTimeFormat; | |
18 | import org.springframework.http.ResponseEntity; | |
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 | ||
29 | import javax.validation.Valid; | |
30 | ||
31 | import java.time.LocalDateTime; | |
32 | ||
33 | @Tag(name = "Announcements") | |
34 | @RequestMapping("/api/announcements") | |
35 | @RestController | |
36 | public class AnnouncementsController extends ApiController{ | |
37 | @Autowired | |
38 | AnnouncementsRepository announcementsRepository; | |
39 | ||
40 | @Autowired | |
41 | CommonsRepository commonsRepository; | |
42 | ||
43 | @Autowired | |
44 | ObjectMapper mapper; | |
45 | ||
46 | @Operation(summary= "List all announcements") | |
47 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
48 | @GetMapping("/all") | |
49 | public Iterable<Announcements> allAnnouncements() { | |
50 | Iterable<Announcements> announcements = announcementsRepository.findAll(); | |
51 |
1
1. allAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::allAnnouncements → KILLED |
return announcements; |
52 | } | |
53 | ||
54 | @Operation(summary= "Create a new announcement") | |
55 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
56 | @PostMapping("/post") | |
57 | public ResponseEntity<?> postAnnouncements( | |
58 | @Parameter(name="commonsId") @RequestParam long commonsId, | |
59 | @Parameter(name = "start", description = "in iso format, e.g. YYYY-mm-ddTHH:MM:SSorg/wiki/ISO_8601") @RequestParam("start") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start, | |
60 | @Parameter(name = "end", description = "in iso format, e.g. YYYY-mm-ddTHH:MM:SSorg/wiki/ISO_8601") @RequestParam(value = "end", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end, | |
61 | @Parameter(name = "announcement") @RequestParam String announcement) | |
62 | throws JsonProcessingException { | |
63 | | |
64 | // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) | |
65 | // See: https://www.baeldung.com/spring-date-parameters | |
66 |
2
1. postAnnouncements : negated conditional → KILLED 2. postAnnouncements : negated conditional → KILLED |
if (end != null && end.isBefore(start)) { |
67 |
1
1. postAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::postAnnouncements → KILLED |
return ResponseEntity.badRequest().body("End date must be after start date"); |
68 | } | |
69 |
1
1. postAnnouncements : negated conditional → KILLED |
if (commonsRepository.findById(commonsId).isEmpty()) { |
70 |
1
1. postAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::postAnnouncements → KILLED |
return ResponseEntity.badRequest().body("Invalid commondId"); |
71 | } | |
72 | Announcements newAnnouncement = Announcements.builder() | |
73 | .commonsId(commonsId) | |
74 | .start(start) | |
75 | .end(end) | |
76 | .announcement(announcement) | |
77 | .build(); | |
78 | ||
79 | Announcements savedAnnouncement = announcementsRepository.save(newAnnouncement); | |
80 | String body = mapper.writeValueAsString(savedAnnouncement); | |
81 | ||
82 |
1
1. postAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::postAnnouncements → KILLED |
return ResponseEntity.ok().body(body); |
83 | } | |
84 | ||
85 | ||
86 | @Operation(summary= "Get a single announcement") | |
87 | @PreAuthorize("hasRole('ROLE_USER')") | |
88 | @GetMapping("") | |
89 | public Announcements getById( | |
90 | @Parameter(name="id") @RequestParam Long id) { | |
91 | Announcements announcements = announcementsRepository.findById(id) | |
92 |
1
1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$getById$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Announcements.class, id)); |
93 | ||
94 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::getById → KILLED |
return announcements; |
95 | } | |
96 | ||
97 | ||
98 | @Operation(summary= "Delete a announcement") | |
99 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
100 | @DeleteMapping("") | |
101 | public Object deleteAnnouncements( | |
102 | @Parameter(name="id") @RequestParam Long id) { | |
103 | Announcements announcements = announcementsRepository.findById(id) | |
104 |
1
1. lambda$deleteAnnouncements$1 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$deleteAnnouncements$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Announcements.class, id)); |
105 | ||
106 |
1
1. deleteAnnouncements : removed call to edu/ucsb/cs156/happiercows/repositories/AnnouncementsRepository::delete → KILLED |
announcementsRepository.delete(announcements); |
107 |
1
1. deleteAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::deleteAnnouncements → KILLED |
return genericMessage("Announcement with id %s deleted".formatted(id)); |
108 | } | |
109 | ||
110 | @Operation(summary= "Update a single announcements") | |
111 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
112 | @PutMapping("") | |
113 | public Announcements updateAnnouncements( | |
114 | @Parameter(name="id") @RequestParam Long id, | |
115 | @RequestBody @Valid Announcements incoming) { | |
116 | ||
117 | Announcements announcements = announcementsRepository.findById(id) | |
118 |
1
1. lambda$updateAnnouncements$2 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$updateAnnouncements$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Announcements.class, id)); |
119 | ||
120 |
1
1. updateAnnouncements : removed call to edu/ucsb/cs156/happiercows/entities/Announcements::setCommonsId → KILLED |
announcements.setCommonsId(incoming.getCommonsId()); |
121 |
1
1. updateAnnouncements : removed call to edu/ucsb/cs156/happiercows/entities/Announcements::setStart → KILLED |
announcements.setStart(incoming.getStart()); |
122 |
1
1. updateAnnouncements : removed call to edu/ucsb/cs156/happiercows/entities/Announcements::setEnd → KILLED |
announcements.setEnd(incoming.getEnd()); |
123 |
1
1. updateAnnouncements : removed call to edu/ucsb/cs156/happiercows/entities/Announcements::setAnnouncement → KILLED |
announcements.setAnnouncement(incoming.getAnnouncement()); |
124 | ||
125 | announcementsRepository.save(announcements); | |
126 | ||
127 |
1
1. updateAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::updateAnnouncements → KILLED |
return announcements; |
128 | } | |
129 | } | |
Mutations | ||
51 |
1.1 |
|
66 |
1.1 2.2 |
|
67 |
1.1 |
|
69 |
1.1 |
|
70 |
1.1 |
|
82 |
1.1 |
|
92 |
1.1 |
|
94 |
1.1 |
|
104 |
1.1 |
|
106 |
1.1 |
|
107 |
1.1 |
|
118 |
1.1 |
|
120 |
1.1 |
|
121 |
1.1 |
|
122 |
1.1 |
|
123 |
1.1 |
|
127 |
1.1 |