| 1 | package edu.ucsb.cs156.frontiers.services; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.frontiers.entities.Course; | |
| 4 | import edu.ucsb.cs156.frontiers.entities.CourseStaff; | |
| 5 | import edu.ucsb.cs156.frontiers.entities.User; | |
| 6 | import edu.ucsb.cs156.frontiers.enums.OrgStatus; | |
| 7 | import edu.ucsb.cs156.frontiers.repositories.CourseStaffRepository; | |
| 8 | import edu.ucsb.cs156.frontiers.repositories.UserRepository; | |
| 9 | import java.util.Optional; | |
| 10 | import lombok.extern.slf4j.Slf4j; | |
| 11 | import org.springframework.beans.factory.annotation.Autowired; | |
| 12 | import org.springframework.stereotype.Service; | |
| 13 | ||
| 14 | @Service | |
| 15 | @Slf4j | |
| 16 | public class CourseStaffService { | |
| 17 | ||
| 18 | @Autowired private CourseStaffRepository courseStaffRepository; | |
| 19 | ||
| 20 | @Autowired private UserRepository userRepository; | |
| 21 | ||
| 22 | /** | |
| 23 | * Returns true if a staff member is the course's instructor, matched by email. A course with no | |
| 24 | * instructor email has no instructor staff member. | |
| 25 | * | |
| 26 | * <p>This is static so that controllers get the real check even in tests where the | |
| 27 | * CourseStaffService bean is mocked. | |
| 28 | * | |
| 29 | * @param course the course | |
| 30 | * @param staff a staff member of that course | |
| 31 | * @return true if the staff member's email is the course's instructor email | |
| 32 | */ | |
| 33 | public static boolean isInstructorStaff(Course course, CourseStaff staff) { | |
| 34 | String instructorEmail = course.getInstructorEmail(); | |
| 35 |
3
1. isInstructorStaff : negated conditional → KILLED 2. isInstructorStaff : replaced boolean return with true for edu/ucsb/cs156/frontiers/services/CourseStaffService::isInstructorStaff → KILLED 3. isInstructorStaff : negated conditional → KILLED |
return instructorEmail != null && instructorEmail.equals(staff.getEmail()); |
| 36 | } | |
| 37 | ||
| 38 | /** | |
| 39 | * Makes sure the instructor of a course is on that course's staff list. If the instructor is | |
| 40 | * already a staff member, nothing happens, so this is safe to call more than once. | |
| 41 | * | |
| 42 | * <p>The staff member's name is taken from the instructor's User record if they have logged in | |
| 43 | * before; otherwise it is left blank. The org status follows the same rule as adding a staff | |
| 44 | * member by hand: JOINCOURSE if the course is linked to a GitHub org, PENDING if not. | |
| 45 | * | |
| 46 | * @param course the course whose instructor should be on staff | |
| 47 | */ | |
| 48 | public void ensureInstructorIsStaff(Course course) { | |
| 49 | String instructorEmail = course.getInstructorEmail(); | |
| 50 | ||
| 51 |
1
1. ensureInstructorIsStaff : negated conditional → KILLED |
if (courseStaffRepository.existsByEmailAndCourse(instructorEmail, course)) { |
| 52 | return; | |
| 53 | } | |
| 54 | ||
| 55 | Optional<User> user = userRepository.findByEmail(instructorEmail); | |
| 56 | ||
| 57 | CourseStaff instructorStaff = | |
| 58 | CourseStaff.builder() | |
| 59 | .firstName(user.map(User::getGivenName).orElse("")) | |
| 60 | .lastName(user.map(User::getFamilyName).orElse("")) | |
| 61 | .email(instructorEmail) | |
| 62 | .course(course) | |
| 63 | .user(user.orElse(null)) | |
| 64 | .orgStatus( | |
| 65 |
1
1. ensureInstructorIsStaff : negated conditional → KILLED |
course.getInstallationId() != null ? OrgStatus.JOINCOURSE : OrgStatus.PENDING) |
| 66 | .build(); | |
| 67 | ||
| 68 | courseStaffRepository.save(instructorStaff); | |
| 69 | log.info("Added instructor {} as staff for course id {}", instructorEmail, course.getId()); | |
| 70 | } | |
| 71 | } | |
Mutations | ||
| 35 |
1.1 2.2 3.3 |
|
| 51 |
1.1 |
|
| 65 |
1.1 |