| 1 | package edu.ucsb.cs156.frontiers.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.frontiers.entities.Assignment; | |
| 4 | import edu.ucsb.cs156.frontiers.entities.Course; | |
| 5 | import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException; | |
| 6 | import edu.ucsb.cs156.frontiers.repositories.AssignmentRepository; | |
| 7 | import edu.ucsb.cs156.frontiers.repositories.CourseRepository; | |
| 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 lombok.extern.slf4j.Slf4j; | |
| 12 | import org.springframework.beans.factory.annotation.Autowired; | |
| 13 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 14 | import org.springframework.web.bind.annotation.*; | |
| 15 | ||
| 16 | @Tag(name = "Assignment") | |
| 17 | @RequestMapping("/api/assignments") | |
| 18 | @RestController | |
| 19 | @Slf4j | |
| 20 | public class AssignmentsController extends ApiController { | |
| 21 | ||
| 22 | @Autowired private AssignmentRepository assignmentRepository; | |
| 23 | ||
| 24 | @Autowired private CourseRepository courseRepository; | |
| 25 | ||
| 26 | /** | |
| 27 | * This method creates a new Assignment. | |
| 28 | * | |
| 29 | * @param courseId the id of the course | |
| 30 | * @param name the name of the assignment | |
| 31 | * @param asn_type the type of the assigment | |
| 32 | * @param visibility the visibility of the assignment | |
| 33 | * @param permission the permission on the assignment | |
| 34 | * @return the created assignment | |
| 35 | */ | |
| 36 | @Operation(summary = "Create a new assignment") | |
| 37 | @PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_INSTRUCTOR')") | |
| 38 | @PostMapping("/post") | |
| 39 | public InstructorAssignmentView postAssignment( | |
| 40 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
| 41 | @Parameter(name = "name") @RequestParam String name, | |
| 42 | @Parameter(name = "asn_type") @RequestParam String asn_type, | |
| 43 | @Parameter(name = "visibility") @RequestParam String visibility, | |
| 44 | @Parameter(name = "permission") @RequestParam String permission) { | |
| 45 | // get course by courseId | |
| 46 | Course course = | |
| 47 | courseRepository | |
| 48 | .findById(courseId) | |
| 49 |
1
1. lambda$postAssignment$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/AssignmentsController::lambda$postAssignment$0 → KILLED |
.orElseThrow(() -> new IllegalArgumentException("Course not found: " + courseId)); |
| 50 | Assignment assignment = | |
| 51 | Assignment.builder() | |
| 52 | .course(course) | |
| 53 | .name(name) | |
| 54 | .asnType(asn_type) | |
| 55 | .visibility(visibility) | |
| 56 | .permission(permission) | |
| 57 | .build(); | |
| 58 | Assignment savedAssignment = assignmentRepository.save(assignment); | |
| 59 | ||
| 60 |
1
1. postAssignment : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/AssignmentsController::postAssignment → KILLED |
return new InstructorAssignmentView(savedAssignment); |
| 61 | } | |
| 62 | ||
| 63 | /** Projection of Assignment entity with fields that are relevant for instructors and admins */ | |
| 64 | public static record InstructorAssignmentView( | |
| 65 | Long id, Course course, String name, String asnType, String visibility, String permission) { | |
| 66 | ||
| 67 | // Creates view from Assignment entity | |
| 68 | public InstructorAssignmentView(Assignment a) { | |
| 69 | this( | |
| 70 | a.getId(), | |
| 71 | a.getCourse(), | |
| 72 | a.getName(), | |
| 73 | a.getAsnType(), | |
| 74 | a.getVisibility(), | |
| 75 | a.getPermission()); | |
| 76 | } | |
| 77 | } | |
| 78 | ||
| 79 | /** | |
| 80 | * This method updates an existing assignment. | |
| 81 | * | |
| 82 | * @param id the id of the assignment | |
| 83 | * @param asn_type the new type of the assignment | |
| 84 | * @param visibility the new visibility of the assignment | |
| 85 | * @param permission the new permission on the assignment | |
| 86 | * @return the updated assignment | |
| 87 | */ | |
| 88 | @Operation(summary = "Update an existing assignment") | |
| 89 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #id)") | |
| 90 | @PutMapping("") | |
| 91 | public InstructorAssignmentView updateAssignment( | |
| 92 | @Parameter(name = "id") @RequestParam Long id, | |
| 93 | @Parameter(name = "asn_type") @RequestParam String asn_type, | |
| 94 | @Parameter(name = "visibility") @RequestParam String visibility, | |
| 95 | @Parameter(name = "permission") @RequestParam String permission) { | |
| 96 | ||
| 97 | Assignment assignment = | |
| 98 | assignmentRepository | |
| 99 | .findById(id) | |
| 100 |
1
1. lambda$updateAssignment$1 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/AssignmentsController::lambda$updateAssignment$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Assignment.class, id)); |
| 101 | ||
| 102 |
1
1. updateAssignment : removed call to edu/ucsb/cs156/frontiers/entities/Assignment::setAsnType → KILLED |
assignment.setAsnType(asn_type); |
| 103 |
1
1. updateAssignment : removed call to edu/ucsb/cs156/frontiers/entities/Assignment::setVisibility → KILLED |
assignment.setVisibility(visibility); |
| 104 |
1
1. updateAssignment : removed call to edu/ucsb/cs156/frontiers/entities/Assignment::setPermission → KILLED |
assignment.setPermission(permission); |
| 105 | ||
| 106 | Assignment savedAssignment = assignmentRepository.save(assignment); | |
| 107 | ||
| 108 |
1
1. updateAssignment : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/AssignmentsController::updateAssignment → KILLED |
return new InstructorAssignmentView(savedAssignment); |
| 109 | } | |
| 110 | } | |
Mutations | ||
| 49 |
1.1 |
|
| 60 |
1.1 |
|
| 100 |
1.1 |
|
| 102 |
1.1 |
|
| 103 |
1.1 |
|
| 104 |
1.1 |
|
| 108 |
1.1 |