Compare commits

..

No commits in common. "f7df234312ebed12e03d100d9578ba08cd356042" and "f2507ddcdd6b05d34b5e7f30615b80a4ad8e5c41" have entirely different histories.

7 changed files with 53 additions and 139 deletions

View File

@ -6,12 +6,10 @@ 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;
@ -41,38 +39,19 @@ public class CourseController {
if (foundCourse == null)
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(ProtectionService.courseWithoutPassword(foundCourse), HttpStatus.OK);
}
@GetMapping("/courses")
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<>();
for (Course course: courses){
coursesWithoutPassword.add(ProtectionService.courseWithoutPassword(course));
}
return new ResponseEntity<>(coursesWithoutPassword,HttpStatus.OK);
return new ResponseEntity<>(courseWithoutPassword(foundCourse), HttpStatus.OK);
}
@PostMapping("/course")
public ResponseEntity<Map<String ,Object>> postCourse(@RequestHeader("Authorization") String token,
public ResponseEntity<Course> postCourse(@RequestHeader("Authorization") String token,
@RequestBody Course course)
{
if (authServ.isNotIn(new Role[]{Role.Secretary,Role.Admin},token))
return new UnauthorizedResponse<>(null);
Course createdCourse = courseServ.save(course);
if (createdCourse == null)
return new ResponseEntity<>(null,HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(ProtectionService.courseWithoutPassword(createdCourse), HttpStatus.CREATED);
return new ResponseEntity<>(courseServ.save(course), HttpStatus.CREATED);
}
@ -108,4 +87,16 @@ 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

@ -30,13 +30,13 @@ public class CurriculumController {
}
@GetMapping("/curriculum/{id}")
public ResponseEntity<Map<String,Object>> findById(@PathVariable long id){
public ResponseEntity<Curriculum> findById(@PathVariable long id){
Curriculum foundCurriculum = curriculumServ.findById(id);
if (foundCurriculum == null)
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(curriculumCourseServ.getDepthCurriculum(foundCurriculum), HttpStatus.OK);
return new ResponseEntity<>(foundCurriculum, HttpStatus.OK);
}
@GetMapping("/curriculums")
@ -52,19 +52,4 @@ public class CurriculumController {
return new ResponseEntity<>(curriculumServ.save(curriculum),HttpStatus.CREATED);
}
@PostMapping("/curriculum/{id}")
public ResponseEntity<String> postCoursesToCurriculum(@RequestHeader("Authorization") String token,
@RequestBody Iterable<Long> coursesIds,
@PathVariable long id)
{
if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token))
return new UnauthorizedResponse<>(null);
if (!curriculumCourseServ.saveAll(coursesIds, curriculumServ.findById(id)))
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(HttpStatus.OK);
}
}

View File

@ -5,7 +5,6 @@ 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;
@ -36,7 +35,7 @@ public class UserController {
User user = authServ.getUserFromToken(token);
if (user == null) return new UnauthorizedResponse<>(null);
return new ResponseEntity<>(ProtectionService.userWithoutPassword(user), HttpStatus.OK);
return new ResponseEntity<>(authServ.userWithoutPassword(user), HttpStatus.OK);
}
@PostMapping("/user")
@ -45,7 +44,7 @@ public class UserController {
if (authServ.isNotIn(new Role[]{Role.Admin,Role.InscriptionService,Role.Secretary},token))
return new UnauthorizedResponse<>(null);
return new ResponseEntity<>(ProtectionService.userWithoutPassword(userService.save(user)),HttpStatus.CREATED);
return new ResponseEntity<>(authServ.userWithoutPassword(userService.save(user)),HttpStatus.CREATED);
}
@GetMapping("/users")
@ -58,7 +57,7 @@ public class UserController {
ArrayList<HashMap<String, Object>> withoutPassword = new ArrayList<>();
for (User u :users){
withoutPassword.add(ProtectionService.userWithoutPassword(u));
withoutPassword.add(authServ.userWithoutPassword(u));
}
return new ResponseEntity<>(withoutPassword, HttpStatus.OK);
}
@ -96,7 +95,7 @@ public class UserController {
ArrayList<HashMap<String, Object>> withoutPassword = new ArrayList<>();
for (User t: teachers){
withoutPassword.add(ProtectionService.userWithoutPassword(t));
withoutPassword.add(authServ.userWithoutPassword(t));
}
return new ResponseEntity<>(withoutPassword, HttpStatus.OK);

View File

@ -1,7 +1,10 @@
package ovh.herisson.Clyde.Services;
import org.springframework.stereotype.Service;
import ovh.herisson.Clyde.Tables.*;
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 java.util.Date;
import java.util.HashMap;
@ -50,5 +53,25 @@ 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

@ -17,8 +17,6 @@ public class CourseService {
}
public Course save(Course course){
if (course.getOwner().getRole() != Role.Teacher)
return null;
return courseRepo.save(course);
}
@ -26,11 +24,6 @@ public class CourseService {
return courseRepo.findById(id);
}
public Iterable<Course> findAll() {
return courseRepo.findAll();
}
public boolean modifyData(long id, Map<String, Object> updates, Role role) {
Course target = courseRepo.findById(id);
@ -69,5 +62,4 @@ public class CourseService {
courseRepo.save(target);
return true;
}
}

View File

@ -1,11 +1,10 @@
package ovh.herisson.Clyde.Services;
import org.springframework.stereotype.Service;
import ovh.herisson.Clyde.Repositories.CourseRepository;
import ovh.herisson.Clyde.Repositories.CurriculumCourseRepository;
import ovh.herisson.Clyde.Repositories.CurriculumRepository;
import ovh.herisson.Clyde.Tables.*;
import ovh.herisson.Clyde.Tables.Course;
import ovh.herisson.Clyde.Tables.Curriculum;
import ovh.herisson.Clyde.Tables.CurriculumCourse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@ -15,14 +14,9 @@ public class CurriculumCourseService {
private final CurriculumCourseRepository curriculumCourseRepo;
private final CourseRepository courseRepo;
private final CurriculumRepository curriculumRepo;
public CurriculumCourseService(CurriculumCourseRepository curriculumCourseRepository, CourseRepository courseRepo, CurriculumRepository curriculumRepo) {
public CurriculumCourseService(CurriculumCourseRepository curriculumCourseRepository) {
this.curriculumCourseRepo = curriculumCourseRepository;
this.courseRepo = courseRepo;
this.curriculumRepo = curriculumRepo;
}
public void save(CurriculumCourse curriculumCourse){
@ -36,11 +30,9 @@ public class CurriculumCourseService {
return null;
HashMap<String ,Object> toReturn = new HashMap<>();
ArrayList<Map<String ,Object>> courses = new ArrayList<>();
Iterable<Course> foundCourses = curriculumCourseRepo.findCoursesByCurriculum(curriculum);
for (Course c: foundCourses){
courses.add(ProtectionService.courseWithoutPassword(c));
ArrayList<Course> courses = new ArrayList<>();
for (Course c: curriculumCourseRepo.findCoursesByCurriculum(curriculum)){
courses.add(c);
}
toReturn.put("courses",courses);
toReturn.put("curriculumId", curriculum.getCurriculumId());
@ -55,39 +47,9 @@ public class CurriculumCourseService {
ArrayList<Map<String,Object>> toReturn = new ArrayList<>();
for (Curriculum curriculum : curriculumRepo.findAll()){
for (Curriculum curriculum : curriculumCourseRepo.findDistinctCurriculums()){
toReturn.add(getDepthCurriculum(curriculum));
}
return toReturn;
}
/** tries to add all courses to the curriculum
*
* @param coursesIds the ids of the courses to be added
* @param curriculum the curriculum to add the courses to
* @return if the changes were made
*/
public boolean saveAll(Iterable<Long> coursesIds, Curriculum curriculum) {
if (curriculum == null || coursesIds == null)
return false;
ArrayList<Course> toAdd = new ArrayList<>();
for (Long courseId : coursesIds){
Course course = courseRepo.findById((long) courseId);
if (course == null)
return false;
if (!toAdd.contains(course))
toAdd.add(course);
}
for (Course course : toAdd){
curriculumCourseRepo.save(new CurriculumCourse(curriculum,course));
}
return true;
}
}

View File

@ -1,38 +0,0 @@
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;
}
}