link backend Post Patch Delete Lesson

This commit is contained in:
2024-04-16 22:03:48 +02:00
parent 9112004326
commit a2be04bfb3
17 changed files with 282 additions and 79 deletions

View File

@ -76,7 +76,7 @@ public class CourseController {
{
if (authServ.isNotIn(new Role[]{Role.Secretary,Role.Admin},token))
return new UnauthorizedResponse<>(null);
System.out.println(course.getOwner().getRegNo());
Course createdCourse = courseServ.save(course);
if (createdCourse == null)
return new ResponseEntity<>(null,HttpStatus.BAD_REQUEST);

View File

@ -52,11 +52,14 @@ public class LessonController {
@PostMapping("/lesson")
public ResponseEntity<HashMap<String, Object>> postLesson(@RequestHeader("Authorization")String token,
@RequestBody Lesson lesson){
public ResponseEntity<HashMap<String, Object>> postLesson(@RequestHeader("Authorization") String token,
@RequestBody Map<String, Object> lessonInfos){
if(authServ.isNotIn(new Role[]{Role.Secretary,Role.Admin},token))
return new UnauthorizedResponse<>(null);
Lesson createdLesson = lessonServ.save(lesson);
Lesson lesson = lessonServ.createLesson(lessonInfos);
Lesson createdLesson = lessonServ.save(lesson);
if(createdLesson==null)
return new ResponseEntity<>(null,HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(ProtectionService.lessonWithoutPassword(createdLesson), HttpStatus.OK);
@ -68,8 +71,22 @@ public class LessonController {
@PathVariable long id){
if(authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token))
return new UnauthorizedResponse<>(null);
if(!lessonServ.modifyData(id, updates, authServ.getUserFromToken(token).getRole()))
if(!lessonServ.modifyData(id, updates)){
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>(HttpStatus.OK);
}
@DeleteMapping("lesson/{id}")
public ResponseEntity<String> deleteLesson(@RequestHeader("Authorization") String token,
@PathVariable Long id){
if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token))
return new UnauthorizedResponse<>(null);
Lesson toDelete = lessonServ.findById(id);
if(toDelete == null)
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
lessonServ.delete(toDelete);
return new ResponseEntity<>(HttpStatus.OK);
}
}

View File

@ -107,7 +107,7 @@ public class MockController {
//Schedule part
Lesson lesson_0_progra1 = new Lesson(progra1, "Mon Apr 01 2024 08:15", "Mon Apr 01 2024 10:15","rgb(0,50,100)","A0B2","Course");
Lesson lesson_0_progra1 = new Lesson(progra1, "Mon Apr 22 2024 08:15", "Mon Apr 22 2024 10:15","rgb(0,50,100)","A0B2","Course");
Lesson lesson_0_chemistry1 = new Lesson(chemistry1, "Wed Mar 27 2024 08:15", "Wed Mar 27 2024 09:15","rgb(100,50,0)","A0B2","TP");
Lesson lesson_0_psycho1 = new Lesson(psycho1, "Sun Mar 24 2024 10:30 ","Sun Mar 24 2024 12:30 ","rgb(100,50,100)", "A0B2","TD");
Lesson lesson_1_progra1 = new Lesson(progra1, "Mon Apr 02 2024 13:30", "Mon Apr 02 2024 15:30","rgb(0,50,100)","A0B2","TP");

View File

@ -1,5 +1,6 @@
package ovh.herisson.Clyde.EndPoints;
import ch.qos.logback.core.net.SyslogOutputStream;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@ -9,10 +10,7 @@ import ovh.herisson.Clyde.Services.ScheduleLessonService;
import ovh.herisson.Clyde.Services.ScheduleService;
import ovh.herisson.Clyde.Services.UserCurriculumService;
import ovh.herisson.Clyde.Services.CurriculumService;
import ovh.herisson.Clyde.Tables.Curriculum;
import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.Schedule;
import ovh.herisson.Clyde.Tables.ScheduleLesson;
import ovh.herisson.Clyde.Tables.*;
import ovh.herisson.Clyde.Services.LessonService;
import java.util.Map;
@ -94,4 +92,16 @@ public class ScheduleController {
return new ResponseEntity<>(HttpStatus.OK);
}
@DeleteMapping("/schedule/lesson/{id}")
public ResponseEntity<String> deleteLessonFromSchedule(@RequestHeader("Authorization") String token,
@RequestBody Long lessonId,
@PathVariable Long id)
{
if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token))
return new UnauthorizedResponse<>(null);
if (!scheduleLessonServ.delete(lessonId))
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(HttpStatus.OK);
}
}

View File

@ -1,5 +1,7 @@
package ovh.herisson.Clyde.Repositories;
import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import ovh.herisson.Clyde.Tables.Curriculum;
@ -20,4 +22,10 @@ public interface ScheduleLessonRepository extends CrudRepository<ScheduleLesson,
@Query("select distinct sl.lesson from ScheduleLesson sl where sl.schedule = ?1")
Iterable<Lesson> findLessonBySchedule(Schedule schedule);
@Modifying
@Transactional
@Query("delete from ScheduleLesson sl where sl.lesson =?1")
void delete(Lesson lesson);
}

View File

@ -8,6 +8,7 @@ import ovh.herisson.Clyde.Tables.Lesson;
import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.User;
import java.lang.reflect.GenericSignatureFormatError;
import java.util.ArrayList;
import java.util.Map;
@ -38,15 +39,11 @@ public class LessonService {
return toReturn;
}
public boolean modifyData(long id, Map<String ,Object> updates, Role role){
Lesson target = lessonRepo.findById(id);
public Lesson createLesson(Map<String,Object> lessonInfos) {
Lesson target = new Lesson();
if(target == null || role != Role.Secretary)
return false;
for (Map.Entry<String , Object> entry: updates.entrySet()){
switch (entry.getKey()){
for (Map.Entry<String, Object> entry : lessonInfos.entrySet()) {
switch (entry.getKey()) {
case "lessonStart":
target.setLessonStart((String) entry.getValue());
break;
@ -61,6 +58,40 @@ public class LessonService {
case "lessonType":
target.setLessonType((String) entry.getValue());
break;
case "courseId":
target.setCourse(courseRepo.findById((int) entry.getValue()));
}
}
return target;
}
public boolean modifyData(long id, Map<String ,Object> updates){
Lesson target = lessonRepo.findById(id);
System.out.println(target);
if(target == null)
return false;
System.out.println("test");
System.out.println(updates.entrySet());
for (Map.Entry<String , Object> entry: updates.entrySet()){
System.out.println(entry);
switch (entry.getKey()){
case "lessonStart":
target.setLessonStart((String) entry.getValue());
break;
case "lessonEnd":
target.setLessonEnd((String) entry.getValue());
break;
case "local":
target.setLocal((String) entry.getValue());
break;
case "lessonType":
target.setLessonType((String) entry.getValue());
break;
}
}
lessonRepo.save(target);

View File

@ -30,6 +30,14 @@ public class ScheduleLessonService {
return true;
}
public boolean delete(long lessonId){
if(lessonId == 0)
return false;
scheduleLessonRepo.delete(lessonRepo.findById(lessonId));
return true;
}
public Schedule getScheduleByCurriculum(Curriculum curriculum){
return scheduleLessonRepo.findScheduleByCurriculum(curriculum);
}

View File

@ -8,7 +8,7 @@ import org.hibernate.annotations.OnDeleteAction;
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int courseID;
private int courseId;
private int credits;
private String title;
@ -26,8 +26,11 @@ public class Course {
public Course() {}
public int getCourseID() {
return courseID;
return courseId;
}
public void setCourseID(int courseId){
this.courseId = courseId;
}
public int getCredits() {
return credits;

View File

@ -1,6 +1,8 @@
package ovh.herisson.Clyde.Tables;
import jakarta.persistence.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
@Entity
@ -10,7 +12,8 @@ public class Lesson {
private int lessonID;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name= "Course")
@OnDelete(action = OnDeleteAction.SET_NULL)
@JoinColumn(name = "Course")
private Course course;
private String lessonStart;

View File

@ -13,6 +13,7 @@ public class Schedule {
@OneToOne
@JoinColumn(name = "Curriculum")
@OnDelete(action = OnDeleteAction.SET_NULL)
private Curriculum curriculum;
public Schedule(Curriculum curriculum){

View File

@ -14,10 +14,12 @@ public class ScheduleLesson{
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "Schedule")
@OnDelete(action = OnDeleteAction.SET_NULL)
private Schedule schedule;
@ManyToOne(fetch = FetchType.EAGER)
@OnDelete(action = OnDeleteAction.SET_NULL)
@JoinColumn(name = "Lesson")
private Lesson lesson;