Compare commits
No commits in common. "005e9503d6eeae8de93364c8d35caf773d974404" and "767a83d1d69db58a53dabd857b3d5be416127c81" have entirely different histories.
005e9503d6
...
767a83d1d6
@ -1,80 +0,0 @@
|
|||||||
package ovh.herisson.Clyde.EndPoints;
|
|
||||||
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
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.CourseService;
|
|
||||||
import ovh.herisson.Clyde.Services.TeacherCourseService;
|
|
||||||
import ovh.herisson.Clyde.Tables.Course;
|
|
||||||
import ovh.herisson.Clyde.Tables.Role;
|
|
||||||
import ovh.herisson.Clyde.Tables.TeacherCourse;
|
|
||||||
import ovh.herisson.Clyde.Tables.User;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@CrossOrigin(originPatterns = "*", allowCredentials = "true")
|
|
||||||
public class CourseController {
|
|
||||||
|
|
||||||
private final CourseService courseServ;
|
|
||||||
|
|
||||||
private final TeacherCourseService teacherCourseServ;
|
|
||||||
|
|
||||||
private final AuthenticatorService authServ;
|
|
||||||
|
|
||||||
public CourseController(CourseService courseServ, TeacherCourseService teacherCourseServ, AuthenticatorService authServ) {
|
|
||||||
this.courseServ = courseServ;
|
|
||||||
this.teacherCourseServ = teacherCourseServ;
|
|
||||||
this.authServ = authServ;
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/course/{id}")
|
|
||||||
public ResponseEntity<Course> getCourse(@RequestHeader("Authorization") String token, @PathVariable long id){
|
|
||||||
if (authServ.getUserFromToken(token) == null)
|
|
||||||
return new UnauthorizedResponse<>(null);
|
|
||||||
|
|
||||||
return new ResponseEntity<>(courseServ.findById(id), HttpStatus.OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@PostMapping("/course")
|
|
||||||
public ResponseEntity<Course> postCourse(@RequestHeader("Authorization") String token, @RequestBody Course course){
|
|
||||||
if (authServ.isNotSecretaryOrAdmin(token))
|
|
||||||
return new UnauthorizedResponse<>(null);
|
|
||||||
|
|
||||||
return new ResponseEntity<>(courseServ.save(course), HttpStatus.CREATED);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@PatchMapping("/course/{id}")
|
|
||||||
public ResponseEntity<Course> patchCourse(@RequestHeader("Authorization") String token,
|
|
||||||
@RequestBody Map<String,Object> updates,
|
|
||||||
@PathVariable long id)
|
|
||||||
{
|
|
||||||
|
|
||||||
if (authServ.IsNotIn(new Role[]{Role.Admin,Role.Teacher,Role.Secretary}, token)){
|
|
||||||
return new UnauthorizedResponse<>(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ResponseEntity<>(courseServ.modifyData(id, updates, authServ.getUserFromToken(token).getRole()), HttpStatus.OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping("/course/{id}")
|
|
||||||
public ResponseEntity<String> postTeachers(@RequestHeader("Authorization") String token,
|
|
||||||
@RequestBody Iterable<Long> teacherIds,
|
|
||||||
@PathVariable Long id)
|
|
||||||
{
|
|
||||||
if (authServ.IsNotIn(new Role[]{Role.Admin,Role.Secretary}, token))
|
|
||||||
return new UnauthorizedResponse<>(null);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
teacherCourseServ.saveAll(teacherIds,courseServ.findById(id));
|
|
||||||
return new ResponseEntity<>(HttpStatus.OK);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -15,7 +15,6 @@ import ovh.herisson.Clyde.Tables.User;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@CrossOrigin(originPatterns = "*", allowCredentials = "true")
|
|
||||||
public class CurriculumController {
|
public class CurriculumController {
|
||||||
|
|
||||||
|
|
||||||
@ -44,17 +43,4 @@ public class CurriculumController {
|
|||||||
public ResponseEntity<Iterable<CurriculumCourse>> findAll(){
|
public ResponseEntity<Iterable<CurriculumCourse>> findAll(){
|
||||||
return new ResponseEntity<>(curriculumCourseServ.findAll(),HttpStatus.OK);
|
return new ResponseEntity<>(curriculumCourseServ.findAll(),HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**@PostMapping("/curriculum") //todo now
|
|
||||||
public ResponseEntity<String> postCurriculum(@RequestHeader("Authorization") String token,@RequestBody Curriculum curriculum){
|
|
||||||
|
|
||||||
if (!isSecretaryOrAdmin(token)){
|
|
||||||
return new UnauthorizedResponse<>("you're not allowed to post a Curriculum");
|
|
||||||
}
|
|
||||||
|
|
||||||
CurriculumServ.save(Curriculum);
|
|
||||||
|
|
||||||
return new ResponseEntity<>("created !",HttpStatus.CREATED);
|
|
||||||
}**/
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -47,6 +47,6 @@ public class LoginController {
|
|||||||
@PostMapping("/request/register")
|
@PostMapping("/request/register")
|
||||||
public ResponseEntity<String> register(@RequestBody InscriptionRequest inscriptionRequest){
|
public ResponseEntity<String> register(@RequestBody InscriptionRequest inscriptionRequest){
|
||||||
authServ.register(inscriptionRequest);
|
authServ.register(inscriptionRequest);
|
||||||
return new ResponseEntity<>("Is OK", HttpStatus.CREATED);
|
return new ResponseEntity<>("Is OK", HttpStatus.OK);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,10 +68,10 @@ public class MockController {
|
|||||||
curriculumService.save(psychologyBab1);
|
curriculumService.save(psychologyBab1);
|
||||||
|
|
||||||
|
|
||||||
Course progra1 = new Course(5,"Programmation et algorithimque 1",joke);
|
Course progra1 = new Course(5,"Programmation et algorithimque 1","TODO DELETE");
|
||||||
Course chemistry1 = new Course(12, "Thermochimie",joke);
|
Course chemistry1 = new Course(12, "Thermochimie","TODO DELETE");
|
||||||
Course psycho1 = new Course(21, "rien faire t'as cru c'est psycho",joke);
|
Course psycho1 = new Course(21, "rien faire t'as cru c'est psycho", "TODO DELETE");
|
||||||
Course commun = new Course(2, "cours commun",joke);
|
Course commun = new Course(2, "cours commun","TODO DELETE");
|
||||||
|
|
||||||
courseService.save(progra1);
|
courseService.save(progra1);
|
||||||
courseService.save(chemistry1);
|
courseService.save(chemistry1);
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
package ovh.herisson.Clyde.Repositories;
|
|
||||||
|
|
||||||
|
|
||||||
import org.springframework.data.repository.CrudRepository;
|
|
||||||
import ovh.herisson.Clyde.Tables.TeacherCourse;
|
|
||||||
|
|
||||||
public interface TeacherCourseRepository extends CrudRepository<TeacherCourse, Long> {
|
|
||||||
}
|
|
@ -50,18 +50,5 @@ public class AuthenticatorService {
|
|||||||
return poster.getRole() != Role.Secretary || poster.getRole() != Role.Admin;
|
return poster.getRole() != Role.Secretary || poster.getRole() != Role.Admin;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean IsNotIn(Role[] roles, String token){
|
|
||||||
if (token == null)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
User poster = getUserFromToken(token);
|
|
||||||
if (poster == null) return true;
|
|
||||||
|
|
||||||
for (Role r:roles){
|
|
||||||
if (poster.getRole() == r)
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,10 +3,6 @@ package ovh.herisson.Clyde.Services;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import ovh.herisson.Clyde.Repositories.CourseRepository;
|
import ovh.herisson.Clyde.Repositories.CourseRepository;
|
||||||
import ovh.herisson.Clyde.Tables.Course;
|
import ovh.herisson.Clyde.Tables.Course;
|
||||||
import ovh.herisson.Clyde.Tables.Role;
|
|
||||||
import ovh.herisson.Clyde.Tables.User;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class CourseService {
|
public class CourseService {
|
||||||
@ -17,39 +13,11 @@ public class CourseService {
|
|||||||
this.courseRepo = courseRepo;
|
this.courseRepo = courseRepo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Course save(Course course){
|
public void save(Course course){
|
||||||
return courseRepo.save(course);
|
courseRepo.save(course);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Course findById(long id){
|
public Course findById(long id){
|
||||||
return courseRepo.findById(id);
|
return courseRepo.findById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Course modifyData(long id, Map<String, Object> updates, Role role) {
|
|
||||||
Course target = courseRepo.findById(id);
|
|
||||||
|
|
||||||
if (role == Role.Teacher){
|
|
||||||
for (Map.Entry<String, Object> entry : updates.entrySet()){
|
|
||||||
if (entry.getKey().equals("title")){
|
|
||||||
target.setTitle((String) entry.getValue());
|
|
||||||
return courseRepo.save(target);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Map.Entry<String ,Object> entry: updates.entrySet()){
|
|
||||||
switch (entry.getKey()){
|
|
||||||
case "title":
|
|
||||||
target.setTitle((String) entry.getValue());
|
|
||||||
break;
|
|
||||||
case "credits":
|
|
||||||
target.setCredits((Integer) entry.getValue());
|
|
||||||
break;
|
|
||||||
case "owner":
|
|
||||||
target.setOwner((User) entry.getValue()); //todo check if is a teacher !
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return courseRepo.save(target);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,39 +0,0 @@
|
|||||||
package ovh.herisson.Clyde.Services;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Controller;
|
|
||||||
import ovh.herisson.Clyde.Repositories.TeacherCourseRepository;
|
|
||||||
import ovh.herisson.Clyde.Repositories.UserRepository;
|
|
||||||
import ovh.herisson.Clyde.Tables.Course;
|
|
||||||
import ovh.herisson.Clyde.Tables.TeacherCourse;
|
|
||||||
import ovh.herisson.Clyde.Tables.User;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
@Controller
|
|
||||||
public class TeacherCourseService {
|
|
||||||
private final TeacherCourseRepository teacherCourseRepo;
|
|
||||||
|
|
||||||
private final UserRepository userRepo;
|
|
||||||
|
|
||||||
public TeacherCourseService(TeacherCourseRepository teacherCourseRepo, UserRepository userRepo) {
|
|
||||||
this.teacherCourseRepo = teacherCourseRepo;
|
|
||||||
this.userRepo = userRepo;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean saveAll(Iterable<Long> teacherIds, Course course){
|
|
||||||
|
|
||||||
ArrayList<Long> addedIds = new ArrayList<>();
|
|
||||||
for (Long teacherId : teacherIds){
|
|
||||||
User teacher = userRepo.findById((long) teacherId);
|
|
||||||
if ( teacher== null){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!addedIds.contains(teacherId))
|
|
||||||
{
|
|
||||||
teacherCourseRepo.save(new TeacherCourse(teacher,course));
|
|
||||||
addedIds.add(teacherId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,6 +1,9 @@
|
|||||||
package ovh.herisson.Clyde.Tables;
|
package ovh.herisson.Clyde.Tables;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.GenerationType;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class Course {
|
public class Course {
|
||||||
@ -9,15 +12,12 @@ public class Course {
|
|||||||
private int courseID;
|
private int courseID;
|
||||||
private int credits;
|
private int credits;
|
||||||
private String title;
|
private String title;
|
||||||
|
private String faculty;
|
||||||
|
|
||||||
@ManyToOne(fetch = FetchType.EAGER)
|
public Course(int credits, String title, String faculty){
|
||||||
@JoinColumn(name = "Users")
|
|
||||||
private User owner;
|
|
||||||
|
|
||||||
public Course(int credits, String title, User owner){
|
|
||||||
this.credits = credits;
|
this.credits = credits;
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.owner = owner;
|
this.faculty = faculty;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Course() {}
|
public Course() {}
|
||||||
@ -34,6 +34,14 @@ public class Course {
|
|||||||
this.credits = credits;
|
this.credits = credits;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getFaculty() {
|
||||||
|
return faculty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFaculty(String faculty){
|
||||||
|
this.faculty = faculty;
|
||||||
|
}
|
||||||
|
|
||||||
public String getTitle() {
|
public String getTitle() {
|
||||||
return title;
|
return title;
|
||||||
}
|
}
|
||||||
@ -41,12 +49,4 @@ public class Course {
|
|||||||
public void setTitle(String title){
|
public void setTitle(String title){
|
||||||
this.title = title;
|
this.title = title;
|
||||||
}
|
}
|
||||||
|
|
||||||
public User getOwner() {
|
|
||||||
return owner;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setOwner(User owner) {
|
|
||||||
this.owner = owner;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -3,26 +3,30 @@ package ovh.herisson.Clyde.Tables;
|
|||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class TeacherCourse {
|
public class TeacherGivenCourse {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
@ManyToOne(fetch = FetchType.EAGER)
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
@JoinColumn(name = "Users")
|
@JoinColumn(name = "Users")
|
||||||
private User user;
|
private User user;
|
||||||
|
|
||||||
|
|
||||||
@ManyToOne(fetch = FetchType.EAGER)
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
@JoinColumn(name = "Course")
|
@JoinColumn(name = "Course")
|
||||||
private Course course;
|
private Course course;
|
||||||
|
|
||||||
public TeacherCourse(User user, Course course){
|
//This flag helps make the difference between an assistant or a Teacher (who owns the course)
|
||||||
|
private boolean owned;
|
||||||
|
|
||||||
|
public TeacherGivenCourse(User user, Course course, boolean owned){
|
||||||
this.user = user;
|
this.user = user;
|
||||||
this.course = course;
|
this.course = course;
|
||||||
|
this.owned = owned;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TeacherCourse() {}
|
public TeacherGivenCourse() {}
|
||||||
|
|
||||||
public int getId() {
|
public int getId() {
|
||||||
return id;
|
return id;
|
||||||
@ -44,4 +48,11 @@ public class TeacherCourse {
|
|||||||
this.course = course;
|
this.course = course;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isOwned() {
|
||||||
|
return owned;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOwned(boolean owned) {
|
||||||
|
this.owned = owned;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user