| 1 | package edu.ucsb.cs156.happiercows.services; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.happiercows.entities.Commons; | |
| 4 | import edu.ucsb.cs156.happiercows.entities.Staff; | |
| 5 | import edu.ucsb.cs156.happiercows.entities.Student; | |
| 6 | import edu.ucsb.cs156.happiercows.entities.User; | |
| 7 | import edu.ucsb.cs156.happiercows.repositories.StaffRepository; | |
| 8 | import edu.ucsb.cs156.happiercows.repositories.StudentRepository; | |
| 9 | import org.springframework.beans.factory.annotation.Autowired; | |
| 10 | import org.springframework.stereotype.Service; | |
| 11 | ||
| 12 | import java.util.ArrayList; | |
| 13 | import java.util.List; | |
| 14 | ||
| 15 | /** | |
| 16 | * Determines whether a user has access to a course-linked Commons, based on | |
| 17 | * whether their email appears on the roster (as a Student or Staff member) | |
| 18 | * of the course the Commons is linked to. See issue #251. | |
| 19 | */ | |
| 20 | @Service | |
| 21 | public class CourseAccessService { | |
| 22 | ||
| 23 | @Autowired | |
| 24 | private StudentRepository studentRepository; | |
| 25 | ||
| 26 | @Autowired | |
| 27 | private StaffRepository staffRepository; | |
| 28 | ||
| 29 | /** | |
| 30 | * A user is eligible for a course-linked commons if they are an admin, or | |
| 31 | * if their email appears on the course's roster of students or staff. | |
| 32 | * A commons with no course (courseId == null) is open to everyone. | |
| 33 | */ | |
| 34 | public boolean isEligibleForCommons(User user, Commons commons) { | |
| 35 |
1
1. isEligibleForCommons : negated conditional → KILLED |
if (commons.getCourseId() == null) { |
| 36 |
1
1. isEligibleForCommons : replaced boolean return with false for edu/ucsb/cs156/happiercows/services/CourseAccessService::isEligibleForCommons → KILLED |
return true; |
| 37 | } | |
| 38 |
1
1. isEligibleForCommons : negated conditional → KILLED |
if (user.isAdmin()) { |
| 39 |
1
1. isEligibleForCommons : replaced boolean return with false for edu/ucsb/cs156/happiercows/services/CourseAccessService::isEligibleForCommons → KILLED |
return true; |
| 40 | } | |
| 41 |
2
1. isEligibleForCommons : replaced boolean return with false for edu/ucsb/cs156/happiercows/services/CourseAccessService::isEligibleForCommons → KILLED 2. isEligibleForCommons : replaced boolean return with true for edu/ucsb/cs156/happiercows/services/CourseAccessService::isEligibleForCommons → KILLED |
return getCourseIdsForUser(user).contains(commons.getCourseId()); |
| 42 | } | |
| 43 | ||
| 44 | /** | |
| 45 | * Returns the distinct list of course ids for which this user's email | |
| 46 | * appears on the roster as a student or as staff. | |
| 47 | */ | |
| 48 | public List<Long> getCourseIdsForUser(User user) { | |
| 49 | List<Long> courseIds = new ArrayList<>(); | |
| 50 | String email = user.getEmail(); | |
| 51 | ||
| 52 | for (Student student : studentRepository.findByEmail(email)) { | |
| 53 |
2
1. getCourseIdsForUser : negated conditional → KILLED 2. getCourseIdsForUser : negated conditional → KILLED |
if (student.getCourseId() != null && !courseIds.contains(student.getCourseId())) { |
| 54 | courseIds.add(student.getCourseId()); | |
| 55 | } | |
| 56 | } | |
| 57 | ||
| 58 | for (Staff staff : staffRepository.findByEmail(email)) { | |
| 59 |
2
1. getCourseIdsForUser : negated conditional → KILLED 2. getCourseIdsForUser : negated conditional → KILLED |
if (staff.getCourseId() != null && !courseIds.contains(staff.getCourseId())) { |
| 60 | courseIds.add(staff.getCourseId()); | |
| 61 | } | |
| 62 | } | |
| 63 | ||
| 64 |
1
1. getCourseIdsForUser : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/services/CourseAccessService::getCourseIdsForUser → KILLED |
return courseIds; |
| 65 | } | |
| 66 | } | |
Mutations | ||
| 35 |
1.1 |
|
| 36 |
1.1 |
|
| 38 |
1.1 |
|
| 39 |
1.1 |
|
| 41 |
1.1 2.2 |
|
| 53 |
1.1 2.2 |
|
| 59 |
1.1 2.2 |
|
| 64 |
1.1 |