|
6 | 6 | import com.booleanuk.cohorts.models.User; |
7 | 7 | import com.booleanuk.cohorts.payload.request.StudentRequest; |
8 | 8 | import com.booleanuk.cohorts.payload.request.TeacherEditStudentRequest; |
| 9 | +import com.booleanuk.cohorts.payload.request.TeacherRequest; |
9 | 10 | import com.booleanuk.cohorts.payload.response.ProfileListResponse; |
10 | 11 | import com.booleanuk.cohorts.payload.response.Response; |
11 | 12 | import com.booleanuk.cohorts.repository.CohortRepository; |
@@ -38,33 +39,6 @@ public class TeacherController { |
38 | 39 |
|
39 | 40 | @Autowired |
40 | 41 | PasswordEncoder encoder; |
41 | | - |
42 | | - @PatchMapping("{id}") |
43 | | - public ResponseEntity<?> updateStudent(@PathVariable int id, @RequestBody TeacherEditStudentRequest teacherEditStudentRequest) { |
44 | | - |
45 | | - User user = userRepository.findById(id).orElse(null); |
46 | | - if (user == null) { |
47 | | - return new ResponseEntity<>("User not found", HttpStatus.NOT_FOUND); |
48 | | - } |
49 | | - |
50 | | - Profile profile = profileRepository.findById(user.getProfile().getId()).orElse(null); |
51 | | - if (profile == null) { |
52 | | - return new ResponseEntity<>("Profile not found", HttpStatus.NOT_FOUND); |
53 | | - } |
54 | | - |
55 | | - if (profile.getRole().getName().name().equals("ROLE_TEACHER")) { |
56 | | - return new ResponseEntity<>("Teachers can only edit other Students!", HttpStatus.BAD_REQUEST); |
57 | | - } |
58 | | - |
59 | | - Cohort cohort = cohortRepository.findById(teacherEditStudentRequest.getCohort_id()).orElseThrow(); |
60 | | - Role role = roleRepository.findById(teacherEditStudentRequest.getRole_id()).orElseThrow(); |
61 | | - |
62 | | - profile.setCohort(cohort); |
63 | | - profile.setRole(role); |
64 | | - |
65 | | - |
66 | | - return new ResponseEntity<>(profileRepository.save(profile),HttpStatus.OK); |
67 | | - } |
68 | 42 |
|
69 | 43 | @GetMapping |
70 | 44 | public ResponseEntity<?> getAllTeachers(){ |
@@ -111,5 +85,72 @@ public ResponseEntity<?> getTeachersByCohortId(@PathVariable int id){ |
111 | 85 | return ResponseEntity.ok(teacherListResponse); |
112 | 86 | } |
113 | 87 |
|
| 88 | + @PatchMapping("{id}") |
| 89 | + public ResponseEntity<?> updateTeacher(@PathVariable int id, @RequestBody TeacherRequest teacherRequest) { |
| 90 | + |
| 91 | + User user = userRepository.findById(id).orElse(null); |
| 92 | + if (user == null) { |
| 93 | + return new ResponseEntity<>("User not found", HttpStatus.NOT_FOUND); |
| 94 | + } |
| 95 | + |
| 96 | + Profile profile = profileRepository.findById(user.getProfile().getId()).orElse(null); |
| 97 | + if (profile == null) { |
| 98 | + return new ResponseEntity<>("Profile not found", HttpStatus.NOT_FOUND); |
| 99 | + } |
| 100 | + |
| 101 | + if (profile.getRole().getName().name().equals("ROLE_STUDENT")) { |
| 102 | + return new ResponseEntity<>("Only users with the TEACHER role can be viewed.", HttpStatus.BAD_REQUEST); |
| 103 | + } |
| 104 | + |
| 105 | + if (teacherRequest.getPhoto() != null) { |
| 106 | + profile.setPhoto(teacherRequest.getPhoto()); |
| 107 | + } |
| 108 | + |
| 109 | + if (teacherRequest.getFirst_name() != null) { |
| 110 | + profile.setFirstName(teacherRequest.getFirst_name()); |
| 111 | + } |
| 112 | + |
| 113 | + if (teacherRequest.getLast_name() != null) { |
| 114 | + profile.setLastName(teacherRequest.getLast_name()); |
| 115 | + } |
| 116 | + |
| 117 | + if (teacherRequest.getUsername() != null) { |
| 118 | + profile.setUsername(teacherRequest.getUsername()); |
| 119 | + } |
| 120 | + |
| 121 | + if (teacherRequest.getGithub_username() != null) { |
| 122 | + profile.setGithubUrl(teacherRequest.getGithub_username()); |
| 123 | + } |
| 124 | + |
| 125 | + if (teacherRequest.getEmail() != null) { |
| 126 | + boolean emailExists = userRepository.existsByEmail(teacherRequest.getEmail()); |
| 127 | + if (emailExists && !teacherRequest.getEmail().equals(user.getEmail())){ |
| 128 | + return new ResponseEntity<>("Email is already in use", HttpStatus.BAD_REQUEST); |
| 129 | + } |
| 130 | + user.setEmail(teacherRequest.getEmail()); |
| 131 | + } |
| 132 | + |
| 133 | + if (teacherRequest.getMobile() != null) { |
| 134 | + profile.setMobile(teacherRequest.getMobile()); |
| 135 | + } |
| 136 | + |
| 137 | + if (teacherRequest.getPassword() != null && !teacherRequest.getPassword().isBlank()) { |
| 138 | + user.setPassword(encoder.encode(teacherRequest.getPassword())); |
| 139 | + } |
| 140 | + |
| 141 | + if (teacherRequest.getBio() != null) { |
| 142 | + profile.setBio(teacherRequest.getBio()); |
| 143 | + } |
| 144 | + |
| 145 | + profileRepository.save(profile); |
| 146 | + |
| 147 | + user.setProfile(profile); |
| 148 | + |
| 149 | + try { |
| 150 | + return new ResponseEntity<>(userRepository.save(user), HttpStatus.OK); |
| 151 | + } catch (DataIntegrityViolationException e) { |
| 152 | + return new ResponseEntity<>("User has an existing profile", HttpStatus.BAD_REQUEST); |
| 153 | + } |
| 154 | + } |
114 | 155 | } |
115 | 156 |
|
0 commit comments