Skip to content

Commit 611db90

Browse files
authored
Merge pull request #87 from boolean-uk/feat/added-PATCH-for-teacher-profile
moueedAli-Feat/added patch for teacher profile
2 parents 557aa13 + 65c326f commit 611db90

File tree

3 files changed

+90
-28
lines changed

3 files changed

+90
-28
lines changed

src/main/java/com/booleanuk/cohorts/controllers/TeacherController.java

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.booleanuk.cohorts.models.User;
77
import com.booleanuk.cohorts.payload.request.StudentRequest;
88
import com.booleanuk.cohorts.payload.request.TeacherEditStudentRequest;
9+
import com.booleanuk.cohorts.payload.request.TeacherRequest;
910
import com.booleanuk.cohorts.payload.response.ProfileListResponse;
1011
import com.booleanuk.cohorts.payload.response.Response;
1112
import com.booleanuk.cohorts.repository.CohortRepository;
@@ -38,33 +39,6 @@ public class TeacherController {
3839

3940
@Autowired
4041
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-
}
6842

6943
@GetMapping
7044
public ResponseEntity<?> getAllTeachers(){
@@ -111,5 +85,72 @@ public ResponseEntity<?> getTeachersByCohortId(@PathVariable int id){
11185
return ResponseEntity.ok(teacherListResponse);
11286
}
11387

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+
}
114155
}
115156

src/main/java/com/booleanuk/cohorts/payload/request/StudentRequest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,5 @@ public class StudentRequest {
1919

2020
public StudentRequest() {
2121
}
22-
2322
}
2423

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.booleanuk.cohorts.payload.request;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
@Getter
7+
@Setter
8+
public class TeacherRequest {
9+
10+
private String photo;
11+
private String first_name;
12+
private String last_name;
13+
private String username;
14+
private String github_username;
15+
private String email;
16+
private String mobile;
17+
private String password;
18+
private String bio;
19+
20+
public TeacherRequest() {
21+
}
22+
}

0 commit comments

Comments
 (0)