1
0
forked from PGL/Clyde

moved portective method to Static ProtectiveService

This commit is contained in:
2024-03-17 03:06:19 +01:00
parent d855bbe911
commit f7df234312
5 changed files with 59 additions and 48 deletions

View File

@@ -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,21 +41,27 @@ 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<Course>> getAllCourses(@RequestHeader("Authorization") String token){
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<>();
return new ResponseEntity<>(courseServ.findAll(),HttpStatus.OK);
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)
{
@@ -64,7 +72,7 @@ public class CourseController {
if (createdCourse == null)
return new ResponseEntity<>(null,HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(createdCourse, HttpStatus.CREATED);
return new ResponseEntity<>(ProtectionService.courseWithoutPassword(createdCourse), HttpStatus.CREATED);
}
@@ -100,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;
}
}