| 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 | * Makes sure the instructor of a course is on that course's staff list. If the instructor is | |
| 24 | * already a staff member, nothing happens, so this is safe to call more than once. | |
| 25 | * | |
| 26 | * <p>The staff member's name is taken from the instructor's User record if they have logged in | |
| 27 | * before; otherwise it is left blank. The org status follows the same rule as adding a staff | |
| 28 | * member by hand: JOINCOURSE if the course is linked to a GitHub org, PENDING if not. | |
| 29 | * | |
| 30 | * @param course the course whose instructor should be on staff | |
| 31 | */ | |
| 32 | public void ensureInstructorIsStaff(Course course) { | |
| 33 | String instructorEmail = course.getInstructorEmail(); | |
| 34 | ||
| 35 |
1
1. ensureInstructorIsStaff : negated conditional → KILLED |
if (courseStaffRepository.existsByEmailAndCourse(instructorEmail, course)) { |
| 36 | return; | |
| 37 | } | |
| 38 | ||
| 39 | Optional<User> user = userRepository.findByEmail(instructorEmail); | |
| 40 | ||
| 41 | CourseStaff instructorStaff = | |
| 42 | CourseStaff.builder() | |
| 43 | .firstName(user.map(User::getGivenName).orElse("")) | |
| 44 | .lastName(user.map(User::getFamilyName).orElse("")) | |
| 45 | .email(instructorEmail) | |
| 46 | .course(course) | |
| 47 | .user(user.orElse(null)) | |
| 48 | .orgStatus( | |
| 49 |
1
1. ensureInstructorIsStaff : negated conditional → KILLED |
course.getInstallationId() != null ? OrgStatus.JOINCOURSE : OrgStatus.PENDING) |
| 50 | .build(); | |
| 51 | ||
| 52 | courseStaffRepository.save(instructorStaff); | |
| 53 | log.info("Added instructor {} as staff for course id {}", instructorEmail, course.getId()); | |
| 54 | } | |
| 55 | } | |
Mutations | ||
| 35 |
1.1 |
|
| 49 |
1.1 |