1
0
forked from PGL/Clyde

moved portective method to Static ProtectiveService

This commit is contained in:
Bartha Maxime 2024-03-17 03:06:19 +01:00
parent d855bbe911
commit f7df234312
5 changed files with 59 additions and 48 deletions

View File

@ -6,10 +6,12 @@ import org.springframework.web.bind.annotation.*;
import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
import ovh.herisson.Clyde.Services.AuthenticatorService;
import ovh.herisson.Clyde.Services.CourseService;
import ovh.herisson.Clyde.Services.ProtectionService;
import ovh.herisson.Clyde.Services.TeacherCourseService;
import ovh.herisson.Clyde.Tables.Course;
import ovh.herisson.Clyde.Tables.Role;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@ -39,21 +41,27 @@ public class CourseController {
if (foundCourse == null)
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(courseWithoutPassword(foundCourse), HttpStatus.OK);
return new ResponseEntity<>(ProtectionService.courseWithoutPassword(foundCourse), HttpStatus.OK);
}
@GetMapping("/courses")
public ResponseEntity<Iterable<Course>> getAllCourses(@RequestHeader("Authorization") String token){
public ResponseEntity<Iterable<HashMap<String,Object>>> getAllCourses(@RequestHeader("Authorization") String token){
if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token))
return new UnauthorizedResponse<>(null);
Iterable<Course> courses = courseServ.findAll();
ArrayList<HashMap<String,Object>> coursesWithoutPassword = new ArrayList<>();
return new ResponseEntity<>(courseServ.findAll(),HttpStatus.OK);
for (Course course: courses){
coursesWithoutPassword.add(ProtectionService.courseWithoutPassword(course));
}
return new ResponseEntity<>(coursesWithoutPassword,HttpStatus.OK);
}
@PostMapping("/course")
public ResponseEntity<Course> postCourse(@RequestHeader("Authorization") String token,
public ResponseEntity<Map<String ,Object>> postCourse(@RequestHeader("Authorization") String token,
@RequestBody Course course)
{
@ -64,7 +72,7 @@ public class CourseController {
if (createdCourse == null)
return new ResponseEntity<>(null,HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(createdCourse, HttpStatus.CREATED);
return new ResponseEntity<>(ProtectionService.courseWithoutPassword(createdCourse), HttpStatus.CREATED);
}
@ -100,16 +108,4 @@ public class CourseController {
return new ResponseEntity<>(HttpStatus.OK);
}
private HashMap<String,Object> courseWithoutPassword(Course course){
HashMap<String ,Object> toReturn = new HashMap<>();
toReturn.put("courseId",course.getCourseID());
toReturn.put("credits",course.getCredits());
toReturn.put("title", course.getTitle());
toReturn.put("owner", authServ.userWithoutPassword(course.getOwner()));
return toReturn;
}
}

View File

@ -5,6 +5,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
import ovh.herisson.Clyde.Services.AuthenticatorService;
import ovh.herisson.Clyde.Services.ProtectionService;
import ovh.herisson.Clyde.Services.UserService;
import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.User;
@ -35,7 +36,7 @@ public class UserController {
User user = authServ.getUserFromToken(token);
if (user == null) return new UnauthorizedResponse<>(null);
return new ResponseEntity<>(authServ.userWithoutPassword(user), HttpStatus.OK);
return new ResponseEntity<>(ProtectionService.userWithoutPassword(user), HttpStatus.OK);
}
@PostMapping("/user")
@ -44,7 +45,7 @@ public class UserController {
if (authServ.isNotIn(new Role[]{Role.Admin,Role.InscriptionService,Role.Secretary},token))
return new UnauthorizedResponse<>(null);
return new ResponseEntity<>(authServ.userWithoutPassword(userService.save(user)),HttpStatus.CREATED);
return new ResponseEntity<>(ProtectionService.userWithoutPassword(userService.save(user)),HttpStatus.CREATED);
}
@GetMapping("/users")
@ -57,7 +58,7 @@ public class UserController {
ArrayList<HashMap<String, Object>> withoutPassword = new ArrayList<>();
for (User u :users){
withoutPassword.add(authServ.userWithoutPassword(u));
withoutPassword.add(ProtectionService.userWithoutPassword(u));
}
return new ResponseEntity<>(withoutPassword, HttpStatus.OK);
}
@ -95,7 +96,7 @@ public class UserController {
ArrayList<HashMap<String, Object>> withoutPassword = new ArrayList<>();
for (User t: teachers){
withoutPassword.add(authServ.userWithoutPassword(t));
withoutPassword.add(ProtectionService.userWithoutPassword(t));
}
return new ResponseEntity<>(withoutPassword, HttpStatus.OK);

View File

@ -1,10 +1,7 @@
package ovh.herisson.Clyde.Services;
import org.springframework.stereotype.Service;
import ovh.herisson.Clyde.Tables.InscriptionRequest;
import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.Token;
import ovh.herisson.Clyde.Tables.User;
import ovh.herisson.Clyde.Tables.*;
import java.util.Date;
import java.util.HashMap;
@ -53,25 +50,5 @@ public class AuthenticatorService {
}
return true;
}
/** return user's data except password
* @param user the user to return
* @return all the user data without the password
*/
public HashMap<String,Object> userWithoutPassword(User user){
HashMap<String,Object> toReturn = new HashMap<>();
toReturn.put("regNo",user.getRegNo());
toReturn.put("lastName",user.getLastName());
toReturn.put("firstName",user.getFirstName());
toReturn.put("email", user.getEmail());
toReturn.put("address",user.getAddress());
toReturn.put("birthDate",user.getBirthDate());
toReturn.put("country",user.getCountry());
toReturn.put("profilePictureUrl",user.getProfilePictureUrl());
toReturn.put("role",user.getRole());
return toReturn;
}
}

View File

@ -36,11 +36,11 @@ public class CurriculumCourseService {
return null;
HashMap<String ,Object> toReturn = new HashMap<>();
ArrayList<Course> courses = new ArrayList<>();
ArrayList<Map<String ,Object>> courses = new ArrayList<>();
Iterable<Course> foundCourses = curriculumCourseRepo.findCoursesByCurriculum(curriculum);
for (Course c: foundCourses){
courses.add(c);
courses.add(ProtectionService.courseWithoutPassword(c));
}
toReturn.put("courses",courses);
toReturn.put("curriculumId", curriculum.getCurriculumId());
@ -89,6 +89,5 @@ public class CurriculumCourseService {
curriculumCourseRepo.save(new CurriculumCourse(curriculum,course));
}
return true;
}
}

View File

@ -0,0 +1,38 @@
package ovh.herisson.Clyde.Services;
import ovh.herisson.Clyde.Tables.Course;
import ovh.herisson.Clyde.Tables.User;
import java.util.HashMap;
public class ProtectionService {
/** return user's data except password
* @param user the user to return
* @return all the user data without the password
*/
public static HashMap<String,Object> userWithoutPassword(User user){
HashMap<String,Object> toReturn = new HashMap<>();
toReturn.put("regNo",user.getRegNo());
toReturn.put("lastName",user.getLastName());
toReturn.put("firstName",user.getFirstName());
toReturn.put("email", user.getEmail());
toReturn.put("address",user.getAddress());
toReturn.put("birthDate",user.getBirthDate());
toReturn.put("country",user.getCountry());
toReturn.put("profilePictureUrl",user.getProfilePictureUrl());
toReturn.put("role",user.getRole());
return toReturn;
}
public static HashMap<String,Object> courseWithoutPassword(Course course){
HashMap<String ,Object> toReturn = new HashMap<>();
toReturn.put("courseId",course.getCourseID());
toReturn.put("credits",course.getCredits());
toReturn.put("title", course.getTitle());
toReturn.put("owner", userWithoutPassword(course.getOwner()));
return toReturn;
}
}