Compare commits
	
		
			5 Commits
		
	
	
		
			f2507ddcdd
			...
			f7df234312
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| f7df234312 | |||
| d855bbe911 | |||
| 4cf2ac1aa8 | |||
| 6e6bd285af | |||
| 474a8d3f31 | 
@ -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,19 +41,38 @@ 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<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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @PostMapping("/course")
 | 
			
		||||
    public ResponseEntity<Course> postCourse(@RequestHeader("Authorization") String token,
 | 
			
		||||
    public ResponseEntity<Map<String ,Object>> postCourse(@RequestHeader("Authorization") String token,
 | 
			
		||||
                                             @RequestBody Course course)
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        if (authServ.isNotIn(new Role[]{Role.Secretary,Role.Admin},token))
 | 
			
		||||
            return new UnauthorizedResponse<>(null);
 | 
			
		||||
 | 
			
		||||
        return new ResponseEntity<>(courseServ.save(course), HttpStatus.CREATED);
 | 
			
		||||
        Course createdCourse = courseServ.save(course);
 | 
			
		||||
        if (createdCourse == null)
 | 
			
		||||
            return new ResponseEntity<>(null,HttpStatus.BAD_REQUEST);
 | 
			
		||||
 | 
			
		||||
        return new ResponseEntity<>(ProtectionService.courseWithoutPassword(createdCourse), HttpStatus.CREATED);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -87,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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -30,13 +30,13 @@ public class CurriculumController {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @GetMapping("/curriculum/{id}")
 | 
			
		||||
    public ResponseEntity<Curriculum> findById(@PathVariable long id){
 | 
			
		||||
    public ResponseEntity<Map<String,Object>> findById(@PathVariable long id){
 | 
			
		||||
        Curriculum foundCurriculum = curriculumServ.findById(id);
 | 
			
		||||
 | 
			
		||||
        if (foundCurriculum == null)
 | 
			
		||||
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
 | 
			
		||||
 | 
			
		||||
        return new ResponseEntity<>(foundCurriculum, HttpStatus.OK);
 | 
			
		||||
        return new ResponseEntity<>(curriculumCourseServ.getDepthCurriculum(foundCurriculum), HttpStatus.OK);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @GetMapping("/curriculums")
 | 
			
		||||
@ -52,4 +52,19 @@ 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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -17,6 +17,8 @@ public class CourseService {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Course save(Course course){
 | 
			
		||||
        if (course.getOwner().getRole() != Role.Teacher)
 | 
			
		||||
            return null;
 | 
			
		||||
        return courseRepo.save(course);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -24,6 +26,11 @@ 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);
 | 
			
		||||
 | 
			
		||||
@ -62,4 +69,5 @@ public class CourseService {
 | 
			
		||||
        courseRepo.save(target);
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,10 +1,11 @@
 | 
			
		||||
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.Tables.Course;
 | 
			
		||||
import ovh.herisson.Clyde.Tables.Curriculum;
 | 
			
		||||
import ovh.herisson.Clyde.Tables.CurriculumCourse;
 | 
			
		||||
import ovh.herisson.Clyde.Repositories.CurriculumRepository;
 | 
			
		||||
import ovh.herisson.Clyde.Tables.*;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
@ -14,9 +15,14 @@ public class CurriculumCourseService {
 | 
			
		||||
 | 
			
		||||
    private final CurriculumCourseRepository curriculumCourseRepo;
 | 
			
		||||
 | 
			
		||||
    private final CourseRepository courseRepo;
 | 
			
		||||
 | 
			
		||||
    public CurriculumCourseService(CurriculumCourseRepository curriculumCourseRepository) {
 | 
			
		||||
    private final CurriculumRepository curriculumRepo;
 | 
			
		||||
 | 
			
		||||
    public CurriculumCourseService(CurriculumCourseRepository curriculumCourseRepository, CourseRepository courseRepo, CurriculumRepository curriculumRepo) {
 | 
			
		||||
        this.curriculumCourseRepo = curriculumCourseRepository;
 | 
			
		||||
        this.courseRepo = courseRepo;
 | 
			
		||||
        this.curriculumRepo = curriculumRepo;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void save(CurriculumCourse curriculumCourse){
 | 
			
		||||
@ -30,9 +36,11 @@ public class CurriculumCourseService {
 | 
			
		||||
            return null;
 | 
			
		||||
 | 
			
		||||
        HashMap<String ,Object> toReturn = new HashMap<>();
 | 
			
		||||
        ArrayList<Course> courses = new ArrayList<>();
 | 
			
		||||
        for (Course c: curriculumCourseRepo.findCoursesByCurriculum(curriculum)){
 | 
			
		||||
            courses.add(c);
 | 
			
		||||
        ArrayList<Map<String ,Object>> courses = new ArrayList<>();
 | 
			
		||||
        Iterable<Course> foundCourses = curriculumCourseRepo.findCoursesByCurriculum(curriculum);
 | 
			
		||||
 | 
			
		||||
        for (Course c: foundCourses){
 | 
			
		||||
            courses.add(ProtectionService.courseWithoutPassword(c));
 | 
			
		||||
        }
 | 
			
		||||
        toReturn.put("courses",courses);
 | 
			
		||||
        toReturn.put("curriculumId", curriculum.getCurriculumId());
 | 
			
		||||
@ -47,9 +55,39 @@ public class CurriculumCourseService {
 | 
			
		||||
 | 
			
		||||
        ArrayList<Map<String,Object>> toReturn = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
        for (Curriculum curriculum : curriculumCourseRepo.findDistinctCurriculums()){
 | 
			
		||||
        for (Curriculum curriculum : curriculumRepo.findAll()){
 | 
			
		||||
            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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user