| 1 | package edu.ucsb.cs156.frontiers.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.frontiers.entities.Course; | |
| 4 | import edu.ucsb.cs156.frontiers.entities.Section; | |
| 5 | import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException; | |
| 6 | import edu.ucsb.cs156.frontiers.repositories.CourseRepository; | |
| 7 | import edu.ucsb.cs156.frontiers.repositories.SectionRepository; | |
| 8 | import io.swagger.v3.oas.annotations.Operation; | |
| 9 | import io.swagger.v3.oas.annotations.Parameter; | |
| 10 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 11 | import java.util.List; | |
| 12 | import java.util.Optional; | |
| 13 | import lombok.extern.slf4j.Slf4j; | |
| 14 | import org.springframework.beans.factory.annotation.Autowired; | |
| 15 | import org.springframework.http.HttpStatus; | |
| 16 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 17 | import org.springframework.transaction.annotation.Transactional; | |
| 18 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 19 | import org.springframework.web.bind.annotation.GetMapping; | |
| 20 | import org.springframework.web.bind.annotation.PathVariable; | |
| 21 | import org.springframework.web.bind.annotation.PostMapping; | |
| 22 | import org.springframework.web.bind.annotation.PutMapping; | |
| 23 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 24 | import org.springframework.web.bind.annotation.RequestParam; | |
| 25 | import org.springframework.web.bind.annotation.RestController; | |
| 26 | import org.springframework.web.server.ResponseStatusException; | |
| 27 | ||
| 28 | /** | |
| 29 | * CRUD endpoints for the Sections table, scoped by course. Sections map a raw section identifier | |
| 30 | * (e.g. the value that appears in a roster CSV) to a human readable label; they are used when the | |
| 31 | * TRANSLATE_SECTIONS course option is enabled. | |
| 32 | */ | |
| 33 | @Tag(name = "Sections") | |
| 34 | @RequestMapping("/api/courses/{courseId}/sections") | |
| 35 | @RestController | |
| 36 | @Slf4j | |
| 37 | public class SectionsController extends ApiController { | |
| 38 | ||
| 39 | @Autowired private SectionRepository sectionRepository; | |
| 40 | ||
| 41 | @Autowired private CourseRepository courseRepository; | |
| 42 | ||
| 43 | /** | |
| 44 | * List all sections for a course, ordered by section. | |
| 45 | * | |
| 46 | * @param courseId the id of the course | |
| 47 | * @return the sections for the course | |
| 48 | */ | |
| 49 | @Operation(summary = "List all sections for a course") | |
| 50 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 51 | @GetMapping("") | |
| 52 | public List<Section> getSections(@Parameter(name = "courseId") @PathVariable Long courseId) { | |
| 53 | ensureCourseExists(courseId); | |
| 54 |
1
1. getSections : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/SectionsController::getSections → KILLED |
return sectionRepository.findByCourseIdOrderBySectionAsc(courseId); |
| 55 | } | |
| 56 | ||
| 57 | /** | |
| 58 | * Create a new section for a course. | |
| 59 | * | |
| 60 | * @param courseId the id of the course | |
| 61 | * @param section the section identifier (must be unique within the course) | |
| 62 | * @param label the human readable label for the section | |
| 63 | * @param slackChannelName the Slack channel associated with the section, if any | |
| 64 | * @return the created section | |
| 65 | */ | |
| 66 | @Operation(summary = "Create a new section for a course") | |
| 67 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 68 | @PostMapping("") | |
| 69 | public Section postSection( | |
| 70 | @Parameter(name = "courseId") @PathVariable Long courseId, | |
| 71 | @Parameter(name = "section") @RequestParam String section, | |
| 72 | @Parameter(name = "label") @RequestParam String label, | |
| 73 | @Parameter(name = "slackChannelName") @RequestParam(required = false) | |
| 74 | String slackChannelName) { | |
| 75 | Course course = ensureCourseExists(courseId); | |
| 76 | String normalizedSection = normalizeRequired("section", section); | |
| 77 | String normalizedLabel = normalizeRequired("label", label); | |
| 78 | String normalizedSlackChannelName = normalizeOptional(slackChannelName); | |
| 79 | ||
| 80 |
1
1. postSection : negated conditional → KILLED |
if (sectionRepository.findByCourseIdAndSection(courseId, normalizedSection).isPresent()) { |
| 81 | throw new ResponseStatusException( | |
| 82 | HttpStatus.CONFLICT, | |
| 83 | "Section %s already exists for course %d".formatted(normalizedSection, courseId)); | |
| 84 | } | |
| 85 | ||
| 86 | Section newSection = | |
| 87 | Section.builder() | |
| 88 | .course(course) | |
| 89 | .section(normalizedSection) | |
| 90 | .label(normalizedLabel) | |
| 91 | .slackChannelName(normalizedSlackChannelName) | |
| 92 | .build(); | |
| 93 |
1
1. postSection : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SectionsController::postSection → KILLED |
return sectionRepository.save(newSection); |
| 94 | } | |
| 95 | ||
| 96 | /** | |
| 97 | * Update an existing section for a course. | |
| 98 | * | |
| 99 | * @param courseId the id of the course | |
| 100 | * @param id the id of the section to update | |
| 101 | * @param section the new section identifier (must be unique within the course) | |
| 102 | * @param label the new human readable label for the section | |
| 103 | * @param slackChannelName the Slack channel associated with the section, if any | |
| 104 | * @return the updated section | |
| 105 | */ | |
| 106 | @Operation(summary = "Update a section for a course") | |
| 107 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 108 | @PutMapping("/{id}") | |
| 109 | public Section putSection( | |
| 110 | @Parameter(name = "courseId") @PathVariable Long courseId, | |
| 111 | @Parameter(name = "id") @PathVariable Long id, | |
| 112 | @Parameter(name = "section") @RequestParam String section, | |
| 113 | @Parameter(name = "label") @RequestParam String label, | |
| 114 | @Parameter(name = "slackChannelName") @RequestParam(required = false) | |
| 115 | String slackChannelName) { | |
| 116 | ensureCourseExists(courseId); | |
| 117 | Section existing = findSectionInCourse(courseId, id); | |
| 118 | String normalizedSection = normalizeRequired("section", section); | |
| 119 | String normalizedLabel = normalizeRequired("label", label); | |
| 120 | String normalizedSlackChannelName = normalizeOptional(slackChannelName); | |
| 121 | ||
| 122 | Optional<Section> duplicate = | |
| 123 | sectionRepository.findByCourseIdAndSection(courseId, normalizedSection); | |
| 124 |
2
1. putSection : negated conditional → KILLED 2. putSection : negated conditional → KILLED |
if (duplicate.isPresent() && !duplicate.get().getId().equals(id)) { |
| 125 | throw new ResponseStatusException( | |
| 126 | HttpStatus.CONFLICT, | |
| 127 | "Section %s already exists for course %d".formatted(normalizedSection, courseId)); | |
| 128 | } | |
| 129 | ||
| 130 |
1
1. putSection : removed call to edu/ucsb/cs156/frontiers/entities/Section::setSection → KILLED |
existing.setSection(normalizedSection); |
| 131 |
1
1. putSection : removed call to edu/ucsb/cs156/frontiers/entities/Section::setLabel → KILLED |
existing.setLabel(normalizedLabel); |
| 132 |
1
1. putSection : removed call to edu/ucsb/cs156/frontiers/entities/Section::setSlackChannelName → KILLED |
existing.setSlackChannelName(normalizedSlackChannelName); |
| 133 |
1
1. putSection : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SectionsController::putSection → KILLED |
return sectionRepository.save(existing); |
| 134 | } | |
| 135 | ||
| 136 | /** | |
| 137 | * Delete a section from a course. | |
| 138 | * | |
| 139 | * @param courseId the id of the course | |
| 140 | * @param id the id of the section to delete | |
| 141 | * @return a message indicating the section was deleted | |
| 142 | */ | |
| 143 | @Operation(summary = "Delete a section from a course") | |
| 144 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 145 | @DeleteMapping("/{id}") | |
| 146 | @Transactional | |
| 147 | public Object deleteSection( | |
| 148 | @Parameter(name = "courseId") @PathVariable Long courseId, | |
| 149 | @Parameter(name = "id") @PathVariable Long id) { | |
| 150 | Course course = ensureCourseExists(courseId); | |
| 151 | Section existing = findSectionInCourse(courseId, id); | |
| 152 | ||
| 153 | // Disconnect from course so that the cascade=ALL relationship on Course.sections | |
| 154 | // does not cause Hibernate to re-persist the section when the course is flushed. | |
| 155 | course.getSections().remove(existing); | |
| 156 |
1
1. deleteSection : removed call to edu/ucsb/cs156/frontiers/entities/Section::setCourse → KILLED |
existing.setCourse(null); |
| 157 | ||
| 158 |
1
1. deleteSection : removed call to edu/ucsb/cs156/frontiers/repositories/SectionRepository::delete → KILLED |
sectionRepository.delete(existing); |
| 159 |
1
1. deleteSection : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SectionsController::deleteSection → KILLED |
return genericMessage("Section with id %d deleted".formatted(id)); |
| 160 | } | |
| 161 | ||
| 162 | private Course ensureCourseExists(Long courseId) { | |
| 163 |
1
1. ensureCourseExists : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SectionsController::ensureCourseExists → KILLED |
return courseRepository |
| 164 | .findById(courseId) | |
| 165 |
1
1. lambda$ensureCourseExists$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SectionsController::lambda$ensureCourseExists$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 166 | } | |
| 167 | ||
| 168 | /** | |
| 169 | * Look up a section by id and verify that it belongs to the given course. A section that exists | |
| 170 | * but belongs to a different course is treated as not found, so that callers cannot read or | |
| 171 | * modify sections of courses they do not have access to. | |
| 172 | */ | |
| 173 | private Section findSectionInCourse(Long courseId, Long id) { | |
| 174 |
1
1. findSectionInCourse : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SectionsController::findSectionInCourse → KILLED |
return sectionRepository |
| 175 | .findById(id) | |
| 176 |
2
1. lambda$findSectionInCourse$0 : replaced boolean return with true for edu/ucsb/cs156/frontiers/controllers/SectionsController::lambda$findSectionInCourse$0 → KILLED 2. lambda$findSectionInCourse$0 : replaced boolean return with false for edu/ucsb/cs156/frontiers/controllers/SectionsController::lambda$findSectionInCourse$0 → KILLED |
.filter(s -> s.getCourse().getId().equals(courseId)) |
| 177 |
1
1. lambda$findSectionInCourse$1 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/SectionsController::lambda$findSectionInCourse$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Section.class, id)); |
| 178 | } | |
| 179 | ||
| 180 | private String normalizeRequired(String fieldName, String value) { | |
| 181 | String normalized = value.strip(); | |
| 182 |
1
1. normalizeRequired : negated conditional → KILLED |
if (normalized.isEmpty()) { |
| 183 | throw new IllegalArgumentException("%s must not be blank".formatted(fieldName)); | |
| 184 | } | |
| 185 |
1
1. normalizeRequired : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/SectionsController::normalizeRequired → KILLED |
return normalized; |
| 186 | } | |
| 187 | ||
| 188 | /** | |
| 189 | * Normalizes an optional field: {@code null} or blank values are treated as "not set" and | |
| 190 | * normalized to {@code null}; otherwise the trimmed value is returned. | |
| 191 | */ | |
| 192 | private String normalizeOptional(String value) { | |
| 193 |
1
1. normalizeOptional : negated conditional → KILLED |
if (value == null) { |
| 194 |
1
1. normalizeOptional : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/SectionsController::normalizeOptional → KILLED |
return null; |
| 195 | } | |
| 196 | String normalized = value.strip(); | |
| 197 |
2
1. normalizeOptional : negated conditional → KILLED 2. normalizeOptional : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/SectionsController::normalizeOptional → KILLED |
return normalized.isEmpty() ? null : normalized; |
| 198 | } | |
| 199 | } | |
Mutations | ||
| 54 |
1.1 |
|
| 80 |
1.1 |
|
| 93 |
1.1 |
|
| 124 |
1.1 2.2 |
|
| 130 |
1.1 |
|
| 131 |
1.1 |
|
| 132 |
1.1 |
|
| 133 |
1.1 |
|
| 156 |
1.1 |
|
| 158 |
1.1 |
|
| 159 |
1.1 |
|
| 163 |
1.1 |
|
| 165 |
1.1 |
|
| 174 |
1.1 |
|
| 176 |
1.1 2.2 |
|
| 177 |
1.1 |
|
| 182 |
1.1 |
|
| 185 |
1.1 |
|
| 193 |
1.1 |
|
| 194 |
1.1 |
|
| 197 |
1.1 2.2 |