Skip to content

Commit 66187b3

Browse files
authored
Merge pull request #90 from boolean-uk/quick_fix_add_student
Fixed add new student
2 parents aa31523 + 4f85673 commit 66187b3

File tree

1 file changed

+104
-4
lines changed

1 file changed

+104
-4
lines changed

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

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
package com.booleanuk.cohorts.controllers;
33

44

5-
import com.booleanuk.cohorts.models.Profile;
6-
import com.booleanuk.cohorts.models.User;
5+
import com.booleanuk.cohorts.models.*;
76
import com.booleanuk.cohorts.payload.request.StudentRequest;
7+
import com.booleanuk.cohorts.payload.response.MessageResponse;
88
import com.booleanuk.cohorts.payload.response.ProfileListResponse;
9+
import com.booleanuk.cohorts.repository.CohortRepository;
910
import com.booleanuk.cohorts.repository.ProfileRepository;
11+
import com.booleanuk.cohorts.repository.RoleRepository;
1012
import com.booleanuk.cohorts.repository.UserRepository;
1113
import org.springframework.beans.factory.annotation.Autowired;
1214
import org.springframework.dao.DataIntegrityViolationException;
@@ -15,8 +17,8 @@
1517
import org.springframework.security.crypto.password.PasswordEncoder;
1618
import org.springframework.web.bind.annotation.*;
1719

18-
import java.util.ArrayList;
19-
import java.util.List;
20+
import java.time.format.DateTimeParseException;
21+
import java.util.*;
2022

2123
@CrossOrigin(origins = "*", maxAge = 3600)
2224
@RestController
@@ -28,6 +30,11 @@ public class StudentController {
2830
@Autowired
2931
private ProfileRepository profileRepository;
3032

33+
@Autowired
34+
private CohortRepository cohortRepository;
35+
@Autowired
36+
private RoleRepository roleRepository;
37+
3138
@Autowired
3239
PasswordEncoder encoder;
3340

@@ -51,6 +58,99 @@ public ResponseEntity<ProfileListResponse> getAllStudents() {
5158
return ResponseEntity.ok(studentListResponse);
5259
}
5360

61+
record PostUserProfile(
62+
String first_name,
63+
String last_name,
64+
String username,
65+
String github_username,
66+
String email,
67+
String mobile,
68+
String password,
69+
String bio,
70+
String role,
71+
String specialism,
72+
int cohort,
73+
String start_date,
74+
String end_date,
75+
String photo
76+
){}
77+
78+
@PostMapping("/create")
79+
public ResponseEntity<?> createNewStudent(@RequestBody PostUserProfile newUserProfile){
80+
81+
System.err.println(newUserProfile);
82+
System.out.println("test----------------------------");
83+
84+
// Lag ny bruker
85+
if (userRepository.existsByEmail(newUserProfile.email)) {
86+
return ResponseEntity.badRequest().body(new MessageResponse("Error: Email is already in use!"));
87+
}
88+
89+
90+
String emailRegex = "^\\w+([.-]?\\w+)*@\\w+([.-]?\\w+)*(\\.\\w{2,3})+$";
91+
String passwordRegex = "^(?=.*[A-Z])(?=.*[0-9])(?=.*[#?!@$%^&-]).{8,}$";
92+
93+
if(!newUserProfile.email.matches(emailRegex))
94+
return ResponseEntity.badRequest().body(new MessageResponse("Email is incorrectly formatted"));
95+
96+
if(!newUserProfile.password.matches(passwordRegex))
97+
return ResponseEntity.badRequest().body(new MessageResponse("Password is incorrectly formatted"));
98+
99+
// Create a new user add salt here if using one
100+
User user = new User(newUserProfile.email, encoder.encode(newUserProfile.password));
101+
102+
//Lag ny profil som er koblet opp til ny bruker
103+
104+
if(newUserProfile.first_name == null || newUserProfile.first_name == "" || newUserProfile.last_name == null || newUserProfile.last_name == ""){
105+
return new ResponseEntity<>("First and last name can't be empty or NULL. First name: " + newUserProfile.first_name + " Last name: " + newUserProfile.last_name, HttpStatus.BAD_REQUEST);
106+
}
107+
108+
109+
Optional<Role> optionalRole = roleRepository.findByName(ERole.valueOf(newUserProfile.role));
110+
if (optionalRole.isEmpty()) {
111+
return new ResponseEntity<>("Role for id "+ newUserProfile.role + " not found", HttpStatus.BAD_REQUEST);
112+
}
113+
114+
Role role = optionalRole.get();
115+
116+
Optional<Cohort> optionalCohort = cohortRepository.findById(newUserProfile.cohort);
117+
if (optionalCohort.isEmpty()) {
118+
return new ResponseEntity<>("Cohort for id "+ newUserProfile.cohort + " not found", HttpStatus.BAD_REQUEST);
119+
}
120+
121+
Cohort cohort = optionalCohort.get();
122+
123+
Profile newProfile = null;
124+
try {
125+
newProfile = new Profile(
126+
user,
127+
newUserProfile.first_name,
128+
newUserProfile.last_name,
129+
newUserProfile.username,
130+
"https://github.com/" + newUserProfile.github_username,
131+
newUserProfile.mobile,
132+
newUserProfile.bio,
133+
role,
134+
newUserProfile.specialism,
135+
cohort,
136+
newUserProfile.photo
137+
);
138+
} catch (DateTimeParseException e) {
139+
return new ResponseEntity<>("Wrong formatting for start_date or end_date. Plese use the following format: 2025-09-14",
140+
HttpStatus.BAD_REQUEST);
141+
}
142+
143+
newProfile.setUser(user);
144+
user.setProfile(newProfile);
145+
146+
147+
try {
148+
return new ResponseEntity<>(userRepository.save(user), HttpStatus.OK);
149+
} catch (DataIntegrityViolationException e) {
150+
return new ResponseEntity<>("User has an existing profile", HttpStatus.BAD_REQUEST);
151+
}
152+
153+
}
54154

55155
@PatchMapping("{id}")
56156
public ResponseEntity<?> updateStudent(@PathVariable int id, @RequestBody StudentRequest studentRequest) {

0 commit comments

Comments
 (0)