| 1 | package edu.ucsb.cs156.organic.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.DeleteMapping; | |
| 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 org.springframework.security.access.AccessDeniedException; | |
| 17 | import io.swagger.v3.oas.annotations.Operation; | |
| 18 | ||
| 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 | ||
| 23 | import lombok.extern.slf4j.Slf4j; | |
| 24 | import jakarta.transaction.Transactional; | |
| 25 | import jakarta.validation.Valid; | |
| 26 | ||
| 27 | import java.time.LocalDateTime; | |
| 28 | import java.util.Optional; | |
| 29 | ||
| 30 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 31 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 32 | ||
| 33 | import edu.ucsb.cs156.organic.entities.School; | |
| 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 | ||
| 39 | @Tag(name = "school") | |
| 40 | @RequestMapping("/api/schools") | |
| 41 | @RestController | |
| 42 | @Slf4j // what does this do | |
| 43 | public class SchoolController extends ApiController{ | |
| 44 |     @Autowired | |
| 45 |     SchoolRepository schoolRepository; | |
| 46 | ||
| 47 |     @Autowired | |
| 48 |     ObjectMapper mapper; | |
| 49 |      | |
| 50 |     @Autowired | |
| 51 |     UserRepository userRepository; | |
| 52 | ||
| 53 |     @Operation(summary = "Update information for a school") | |
| 54 |     // allow for roles of ADMIN or INSTRUCTOR but only if the user is a staff member | |
| 55 |     // for the course | |
| 56 |     @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_INSTRUCTOR')") | |
| 57 |     @PutMapping("/update") | |
| 58 |     public School updateSchool( | |
| 59 |             @Parameter(name = "abbrev") @RequestParam String abbrev, | |
| 60 |             @RequestBody @Valid School incoming) { | |
| 61 | ||
| 62 |                 School school = schoolRepository.findById(abbrev) | |
| 63 | 
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)); | 
| 64 | ||
| 65 | 
1
1. updateSchool : removed call to edu/ucsb/cs156/organic/entities/School::setName → KILLED | 
        school.setName(incoming.getName()); | 
| 66 | 
1
1. updateSchool : removed call to edu/ucsb/cs156/organic/entities/School::setTermRegex → KILLED | 
        school.setTermRegex(incoming.getTermRegex()); | 
| 67 | 
1
1. updateSchool : removed call to edu/ucsb/cs156/organic/entities/School::setTermDescription → KILLED | 
        school.setTermDescription(incoming.getTermDescription()); | 
| 68 | 
1
1. updateSchool : removed call to edu/ucsb/cs156/organic/entities/School::setTermError → KILLED | 
        school.setTermError(incoming.getTermError()); | 
| 69 | ||
| 70 |         schoolRepository.save(school); | |
| 71 | ||
| 72 |         log.info("school={}", school); | |
| 73 | ||
| 74 | 
1
1. updateSchool : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::updateSchool → KILLED | 
        return school; | 
| 75 |     } | |
| 76 | ||
| 77 | ||
| 78 |     @Operation(summary= "List all schools") | |
| 79 |     @PreAuthorize("hasRole('ROLE_USER')") | |
| 80 |     @GetMapping("/all") | |
| 81 |     public Iterable<School> allSchools() { | |
| 82 |         Iterable<School> schools = schoolRepository.findAll(); | |
| 83 | 
1
1. allSchools : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::allSchools → KILLED | 
        return schools; | 
| 84 |     } | |
| 85 | ||
| 86 |     @Operation(summary= "Get a single school by abbreviation") | |
| 87 |     @PreAuthorize("hasRole('ROLE_USER')") | |
| 88 |     @GetMapping("") | |
| 89 |     public School getById( | |
| 90 |         @Parameter(name="abbrev") @RequestParam String abbrev) { | |
| 91 |         Optional<School> schoolOptional = schoolRepository.findById(abbrev); | |
| 92 | 
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)); | 
| 93 | 
1
1. getById : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::getById → KILLED | 
        return school; | 
| 94 |     } | |
| 95 | ||
| 96 |     @Operation(summary= "Delete a school") | |
| 97 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 98 |     @DeleteMapping("") | |
| 99 |     public Object deleteSchool( | |
| 100 |             @Parameter(name="abbrev") @RequestParam String abbrev) { | |
| 101 |                 School school = schoolRepository.findById(abbrev) | |
| 102 | 
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)); | 
| 103 | ||
| 104 | 
1
1. deleteSchool : removed call to edu/ucsb/cs156/organic/repositories/SchoolRepository::delete → KILLED | 
        schoolRepository.delete(school); | 
| 105 | 
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)); | 
| 106 |     } | |
| 107 | ||
| 108 | ||
| 109 |     @Operation(summary= "Create a new school") | |
| 110 | ||
| 111 |   @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 112 |     @PostMapping("/post") | |
| 113 |     public School postSchool( | |
| 114 |         @Parameter(name = "school", description="school in json format") @RequestBody School school | |
| 115 |         ) | |
| 116 |     { | |
| 117 | ||
| 118 | 
1
1. postSchool : negated conditional → KILLED | 
        if (!school.getAbbrev().equals(school.getAbbrev().toLowerCase())){ | 
| 119 |             throw new IllegalArgumentException("Invalid abbrev format. Abbrev must be all lowercase"); | |
| 120 |         } | |
| 121 | ||
| 122 |         School savedSchool = schoolRepository.save(school); | |
| 123 | ||
| 124 | 
1
1. postSchool : replaced return value with null for edu/ucsb/cs156/organic/controllers/SchoolController::postSchool → KILLED | 
        return savedSchool; | 
| 125 |     } | |
| 126 | ||
| 127 | ||
| 128 | ||
| 129 | ||
| 130 | } | |
Mutations | ||
| 63 | 
 
 1.1  | 
|
| 65 | 
 
 1.1  | 
|
| 66 | 
 
 1.1  | 
|
| 67 | 
 
 1.1  | 
|
| 68 | 
 
 1.1  | 
|
| 74 | 
 
 1.1  | 
|
| 83 | 
 
 1.1  | 
|
| 92 | 
 
 1.1  | 
|
| 93 | 
 
 1.1  | 
|
| 102 | 
 
 1.1  | 
|
| 104 | 
 
 1.1  | 
|
| 105 | 
 
 1.1  | 
|
| 118 | 
 
 1.1  | 
|
| 124 | 
 
 1.1  |