Max/Backend/UserDelete #144

Merged
tonitch merged 7 commits from Max/Backend/UserDelete into master 2024-03-18 13:45:05 +01:00
9 changed files with 74 additions and 4 deletions
Showing only changes of commit 93c161be4c - Show all commits

View File

@ -11,7 +11,6 @@ import ovh.herisson.Clyde.Services.TeacherCourseService;
import ovh.herisson.Clyde.Tables.Course; import ovh.herisson.Clyde.Tables.Course;
import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.User; import ovh.herisson.Clyde.Tables.User;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -119,4 +118,20 @@ public class CourseController {
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }
@DeleteMapping("course/{id}")
public ResponseEntity<String> deleteUser(@RequestHeader("Authorization") String token, @PathVariable Long id){
if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary}, token))
return new UnauthorizedResponse<>(null);
Course toDelete = courseServ.findById(id);
if (toDelete == null)
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
courseServ.delete(courseServ.findById(id));
return new ResponseEntity<>(HttpStatus.OK);
}
} }

View File

@ -67,4 +67,18 @@ public class CurriculumController {
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }
@DeleteMapping("/curriculum/{id}")
public ResponseEntity<String > deleteCurriculum(@RequestHeader("Authorization") String token, @PathVariable Long id){
if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary}, token))
return new UnauthorizedResponse<>(null);
Curriculum toDelete = curriculumServ.findById(id);
if (toDelete == null)
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
curriculumServ.delete(toDelete);
return new ResponseEntity<>(HttpStatus.OK);
}
} }

View File

@ -10,8 +10,6 @@ import ovh.herisson.Clyde.Services.ProtectionService;
import ovh.herisson.Clyde.Tables.InscriptionRequest; import ovh.herisson.Clyde.Tables.InscriptionRequest;
import ovh.herisson.Clyde.Tables.RequestState; import ovh.herisson.Clyde.Tables.RequestState;
import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.Role;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
@RestController @RestController
@ -68,4 +66,18 @@ public class InscriptionController {
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }
@DeleteMapping("/request/register/{id}")
public ResponseEntity<String > deleteRequest(@RequestHeader("Authorization") String token, @PathVariable Long id){
if (authServ.isNotIn(new Role[]{Role.Admin,Role.InscriptionService}, token))
return new UnauthorizedResponse<>(null);
InscriptionRequest toDelete = inscriptionServ.getById(id);
if (toDelete == null)
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
inscriptionServ.delete(toDelete);
return new ResponseEntity<>(HttpStatus.OK);
}
} }

View File

@ -127,7 +127,12 @@ public class UserController {
if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token)) if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token))
return new UnauthorizedResponse<>(null); return new UnauthorizedResponse<>(null);
userService.delete(userService.getUserById(id)); User toDelete = userService.getUserById(id);
if (toDelete == null)
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
userService.delete(toDelete);
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }
} }

View File

@ -77,4 +77,7 @@ public class CourseService {
return true; return true;
} }
public void delete(Course course) {
courseRepo.delete(course);
}
} }

View File

@ -19,4 +19,7 @@ public class CurriculumService {
return curriculumRepo.findById(id); return curriculumRepo.findById(id);
} }
public void delete(Curriculum curriculum) {
curriculumRepo.delete(curriculum);
}
} }

View File

@ -87,4 +87,8 @@ public class InscriptionService {
save(inscrRequest); save(inscrRequest);
return true; return true;
} }
public void delete(InscriptionRequest toDelete) {
inscriptionRepo.delete(toDelete);
}
} }

View File

@ -15,6 +15,10 @@ public class ProtectionService {
* @return all the user data without the password * @return all the user data without the password
*/ */
public static HashMap<String,Object> userWithoutPassword(User user){ public static HashMap<String,Object> userWithoutPassword(User user){
if (user ==null)
return null;
HashMap<String,Object> toReturn = new HashMap<>(); HashMap<String,Object> toReturn = new HashMap<>();
toReturn.put("regNo",user.getRegNo()); toReturn.put("regNo",user.getRegNo());
@ -42,6 +46,9 @@ public class ProtectionService {
public static HashMap<String,Object> courseWithoutPassword(Course course){ public static HashMap<String,Object> courseWithoutPassword(Course course){
if (course == null)
return null;
HashMap<String ,Object> toReturn = new HashMap<>(); HashMap<String ,Object> toReturn = new HashMap<>();
toReturn.put("courseId",course.getCourseID()); toReturn.put("courseId",course.getCourseID());
@ -64,6 +71,10 @@ public class ProtectionService {
public static Map<String, Object> requestWithoutPassword(InscriptionRequest inscriptionRequest) { public static Map<String, Object> requestWithoutPassword(InscriptionRequest inscriptionRequest) {
if (inscriptionRequest == null)
return null;
Map<String, Object> toReturn = new HashMap<>(); Map<String, Object> toReturn = new HashMap<>();
toReturn.put("id", inscriptionRequest.getId()); toReturn.put("id", inscriptionRequest.getId());

View File

@ -1,6 +1,8 @@
package ovh.herisson.Clyde.Tables; package ovh.herisson.Clyde.Tables;
import jakarta.persistence.*; import jakarta.persistence.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
@Entity @Entity
public class Course { public class Course {
@ -11,6 +13,7 @@ public class Course {
private String title; private String title;
@ManyToOne(fetch = FetchType.EAGER) @ManyToOne(fetch = FetchType.EAGER)
@OnDelete(action = OnDeleteAction.SET_NULL)
@JoinColumn(name = "Users") @JoinColumn(name = "Users")
private User owner; private User owner;