1 | package edu.ucsb.cs156.organic.controllers; | |
2 | ||
3 | import org.springframework.beans.factory.annotation.Autowired; | |
4 | import org.springframework.format.annotation.DateTimeFormat; | |
5 | import org.springframework.security.access.prepost.PreAuthorize; | |
6 | import org.springframework.web.bind.annotation.DeleteMapping; | |
7 | import org.springframework.web.bind.annotation.GetMapping; | |
8 | import org.springframework.web.bind.annotation.RequestBody; | |
9 | import org.springframework.web.bind.annotation.PostMapping; | |
10 | import org.springframework.web.bind.annotation.PutMapping; | |
11 | import org.springframework.web.bind.annotation.RequestMapping; | |
12 | import org.springframework.web.bind.annotation.RequestParam; | |
13 | import org.springframework.web.bind.annotation.RestController; | |
14 | ||
15 | import edu.ucsb.cs156.organic.errors.EntityNotFoundException; | |
16 | import io.swagger.v3.oas.annotations.Operation; | |
17 | ||
18 | import io.swagger.v3.oas.annotations.Parameter; | |
19 | import io.swagger.v3.oas.annotations.tags.Tag; | |
20 | import lombok.extern.slf4j.Slf4j; | |
21 | import javax.validation.Valid; | |
22 | ||
23 | import java.net.URLDecoder; | |
24 | import java.nio.charset.StandardCharsets; | |
25 | import java.time.LocalDateTime; | |
26 | import java.util.Optional; | |
27 | ||
28 | import com.fasterxml.jackson.core.JsonProcessingException; | |
29 | import com.fasterxml.jackson.databind.ObjectMapper; | |
30 | ||
31 | import edu.ucsb.cs156.organic.entities.Course; | |
32 | import edu.ucsb.cs156.organic.entities.School; | |
33 | import edu.ucsb.cs156.organic.entities.Staff; | |
34 | import edu.ucsb.cs156.organic.entities.User; | |
35 | import edu.ucsb.cs156.organic.repositories.SchoolRepository; | |
36 | import edu.ucsb.cs156.organic.repositories.UserRepository; | |
37 | ||
38 | @Tag(name = "school") | |
39 | @RequestMapping("/api/schools") | |
40 | @RestController | |
41 | @Slf4j // what does this do | |
42 | public class SchoolController extends ApiController{ | |
43 | @Autowired | |
44 | SchoolRepository schoolRepository; | |
45 | ||
46 | @Autowired | |
47 | ObjectMapper mapper; | |
48 | | |
49 | @Autowired | |
50 | UserRepository userRepository; | |
51 | ||
52 | @Operation(summary = "Update information for a school") | |
53 | // allow for roles of ADMIN or INSTRUCTOR but only if the user is a staff member | |
54 | // for the course | |
55 | @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_INSTRUCTOR')") | |
56 | @PutMapping("/update") | |
57 | public School updateSchool( | |
58 | @Parameter(name = "abbrev") @RequestParam String abbrev, | |
59 | @RequestBody @Valid School incoming) { | |
60 | ||
61 | School school = schoolRepository.findById(abbrev) | |
62 |
1
1. lambda$updateSchool$0 : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::lambda$updateSchool$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(School.class, abbrev)); |
63 | ||
64 |
1
1. updateSchool : removed call to edu/ucsb/cs156/organic/entities/School::setName → KILLED |
school.setName(incoming.getName()); |
65 |
1
1. updateSchool : removed call to edu/ucsb/cs156/organic/entities/School::setTermRegex → KILLED |
school.setTermRegex(incoming.getTermRegex()); |
66 |
1
1. updateSchool : removed call to edu/ucsb/cs156/organic/entities/School::setTermDescription → KILLED |
school.setTermDescription(incoming.getTermDescription()); |
67 |
1
1. updateSchool : removed call to edu/ucsb/cs156/organic/entities/School::setTermError → KILLED |
school.setTermError(incoming.getTermError()); |
68 | ||
69 | schoolRepository.save(school); | |
70 | ||
71 | log.info("school={}", school); | |
72 | ||
73 |
1
1. updateSchool : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::updateSchool → KILLED |
return school; |
74 | } | |
75 | ||
76 | @Operation(summary= "List all schools") | |
77 | @PreAuthorize("hasRole('ROLE_USER')") | |
78 | @GetMapping("/all") | |
79 | public Iterable<School> allSchools() { | |
80 | Iterable<School> schools = schoolRepository.findAll(); | |
81 |
1
1. allSchools : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::allSchools → KILLED |
return schools; |
82 | } | |
83 | ||
84 | @Operation(summary= "Get a single school by abbreviation") | |
85 | @PreAuthorize("hasRole('ROLE_USER')") | |
86 | @GetMapping("") | |
87 | public School getById( | |
88 | @Parameter(name="abbrev") @RequestParam String abbrev) { | |
89 | Optional<School> schoolOptional = schoolRepository.findById(abbrev); | |
90 |
1
1. lambda$getById$1 : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::lambda$getById$1 → KILLED |
School school = schoolOptional.orElseThrow(() -> new EntityNotFoundException(School.class, abbrev)); |
91 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::getById → KILLED |
return school; |
92 | } | |
93 | ||
94 | @Operation(summary= "Delete a school") | |
95 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
96 | @DeleteMapping("") | |
97 | public Object deleteSchool( | |
98 | @Parameter(name="abbrev") @RequestParam String abbrev) { | |
99 | School school = schoolRepository.findById(abbrev) | |
100 |
1
1. lambda$deleteSchool$2 : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::lambda$deleteSchool$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(School.class, abbrev)); |
101 | ||
102 |
1
1. deleteSchool : removed call to edu/ucsb/cs156/organic/repositories/SchoolRepository::delete → KILLED |
schoolRepository.delete(school); |
103 |
1
1. deleteSchool : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::deleteSchool → KILLED |
return genericMessage("School with id %s deleted".formatted(abbrev)); |
104 | } | |
105 | ||
106 | ||
107 | @Operation(summary= "Create a new school") | |
108 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
109 | @PostMapping("/post") | |
110 | public School postSchool( | |
111 | @Parameter(name="abbrev", description="university domain name", example="ucsb") @RequestParam String abbrev, | |
112 | @Parameter(name="name", description="University name") @RequestParam String name, | |
113 | @Parameter(name="termRegex", description="Format: Example [WSMF]\\d\\d") @RequestParam String termRegex, | |
114 | @Parameter(name="termDescription", description="Enter quarter, e.g. F23, W24, S24, M24") @RequestParam String termDescription, | |
115 | @Parameter(name="termError", description="input error?") @RequestParam(required = false) String termError) | |
116 | throws JsonProcessingException { | |
117 | ||
118 | String decodedTermRegex = URLDecoder.decode(termRegex, StandardCharsets.UTF_8); | |
119 | ||
120 | School school = School.builder().build(); | |
121 |
1
1. postSchool : removed call to edu/ucsb/cs156/organic/entities/School::setAbbrev → KILLED |
school.setAbbrev(abbrev); |
122 |
1
1. postSchool : removed call to edu/ucsb/cs156/organic/entities/School::setName → KILLED |
school.setName(name); |
123 |
1
1. postSchool : removed call to edu/ucsb/cs156/organic/entities/School::setTermRegex → KILLED |
school.setTermRegex(decodedTermRegex); |
124 |
1
1. postSchool : removed call to edu/ucsb/cs156/organic/entities/School::setTermDescription → KILLED |
school.setTermDescription(termDescription); |
125 |
1
1. postSchool : removed call to edu/ucsb/cs156/organic/entities/School::setTermError → KILLED |
school.setTermError(termError); |
126 | ||
127 |
1
1. postSchool : negated conditional → KILLED |
if (!abbrev.equals(abbrev.toLowerCase())){ |
128 | throw new IllegalArgumentException("Invalid abbrev format. Abbrev must be all lowercase"); | |
129 | } | |
130 | ||
131 | School savedSchool = schoolRepository.save(school); | |
132 | ||
133 |
1
1. postSchool : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::postSchool → KILLED |
return savedSchool; |
134 | } | |
135 | ||
136 | } | |
Mutations | ||
62 |
1.1 |
|
64 |
1.1 |
|
65 |
1.1 |
|
66 |
1.1 |
|
67 |
1.1 |
|
73 |
1.1 |
|
81 |
1.1 |
|
90 |
1.1 |
|
91 |
1.1 |
|
100 |
1.1 |
|
102 |
1.1 |
|
103 |
1.1 |
|
121 |
1.1 |
|
122 |
1.1 |
|
123 |
1.1 |
|
124 |
1.1 |
|
125 |
1.1 |
|
127 |
1.1 |
|
133 |
1.1 |