From 1e2efac5bcb440e8570cb15ec84851b12e570db3 Mon Sep 17 00:00:00 2001 From: LeoMoulin Date: Sat, 20 Apr 2024 23:29:22 +0200 Subject: [PATCH] Implement an endpoint to get all the courses of an user --- .../Clyde/EndPoints/CourseController.java | 45 ++++++++++++++++--- .../UserCurriculumRepository.java | 2 + .../Clyde/Services/UserCurriculumService.java | 4 +- .../src/Apps/Inscription/AboutStudent.vue | 4 ++ frontend/src/rest/courses.js | 8 ++++ 5 files changed, 57 insertions(+), 6 deletions(-) diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java index 474e2cb..3dd81fd 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java @@ -3,14 +3,15 @@ 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.Repositories.CurriculumCourseRepository; 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.Services.*; import ovh.herisson.Clyde.Tables.Course; import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.User; +import ovh.herisson.Clyde.Tables.UserCurriculum; + +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -24,10 +25,20 @@ public class CourseController { private final AuthenticatorService authServ; - public CourseController(CourseService courseServ, TeacherCourseService teacherCourseServ, AuthenticatorService authServ) { + private final UserService userService; + + private final UserCurriculumService userCurriculumService; + + private final CurriculumCourseRepository curriculumCourseRepository; + private final CurriculumCourseService curriculumCourseService; + public CourseController(CourseService courseServ, TeacherCourseService teacherCourseServ, AuthenticatorService authServ, UserService userService, UserCurriculumService userCurriculumService, CurriculumCourseRepository curriculumCourseRepository, CurriculumCourseService curriculumCourseService) { this.courseServ = courseServ; this.teacherCourseServ = teacherCourseServ; this.authServ = authServ; + this.userService = userService; + this.userCurriculumService = userCurriculumService; + this.curriculumCourseRepository = curriculumCourseRepository; + this.curriculumCourseService = curriculumCourseService; } @GetMapping("/course/{id}") @@ -136,4 +147,28 @@ public class CourseController { return new ResponseEntity<>(HttpStatus.OK); } + + //Get all the courses followed by an user + @GetMapping("/usercourses/{userId}") + public ResponseEntity> getAllUserCourses(@PathVariable long userId){ + User u = userService.getUserById(userId); + + //We get all the actual curriculums of the user + ArrayList userCurricula = userCurriculumService.findByStudentAndActual(u, true); + ArrayList toReturn = new ArrayList<>(); + + //We iterate through all the curriculums and we extract the courses + for (int i = 0; i < userCurricula.size(); i++){ + curriculumCourseRepository.findCoursesByCurriculum(userCurricula.get(i).getCurriculum()).forEach((item) -> { + //We need this to eliminate clones because a course can belong to several curriculums + if(!toReturn.contains(item)){ + toReturn.add(item); + } + }); + } + + return new ResponseEntity<>(toReturn, HttpStatus.OK); + } + + } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserCurriculumRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserCurriculumRepository.java index 75922fa..0e119f1 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserCurriculumRepository.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserCurriculumRepository.java @@ -16,4 +16,6 @@ public interface UserCurriculumRepository extends CrudRepository findByUserOrderByCurriculum(User student); UserCurriculum findByUserAndCurriculumAndActual(User user, Curriculum curriculum, boolean actual); + ArrayList findByUserAndActual(User user, boolean actual); + } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/UserCurriculumService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/UserCurriculumService.java index 2861a6e..4dbdea3 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/UserCurriculumService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/UserCurriculumService.java @@ -49,5 +49,7 @@ public class UserCurriculumService { return toReturn; } - + public ArrayList findByStudentAndActual(User u, boolean actual){ + return userCurriculumRepository.findByUserAndActual(u, actual); + } } diff --git a/frontend/src/Apps/Inscription/AboutStudent.vue b/frontend/src/Apps/Inscription/AboutStudent.vue index daa3170..c2d1f96 100644 --- a/frontend/src/Apps/Inscription/AboutStudent.vue +++ b/frontend/src/Apps/Inscription/AboutStudent.vue @@ -5,6 +5,7 @@ import {ref} from "vue"; import ExternalCurriculumList from "@/Apps/Inscription/ExternalCurriculumList.vue"; import {getExternalCurriculumByUser} from "@/rest/externalCurriculum.js"; + import {getUserActualCourses} from "@/rest/courses.js"; const props = defineProps(['target']) const user = await getUser(props.target) @@ -12,6 +13,9 @@ const externalcurrlist = await getExternalCurriculumByUser(user.regNo) const extercurrlist = ref(false) + const courselist = await getUserActualCourses(user.regNo) + console.log(courselist) + const watchingUser = await getSelf() function getPP(){ if(user.profilePictureUrl === null){ diff --git a/frontend/src/rest/courses.js b/frontend/src/rest/courses.js index 2359a7b..e482e6b 100644 --- a/frontend/src/rest/courses.js +++ b/frontend/src/rest/courses.js @@ -71,3 +71,11 @@ export async function getCourses(role){ export async function alterCourse(id, changes){ return restPatch("/course/" + id, changes); } + +/** + * Return a list containing all the actual courses of a user + */ + +export async function getUserActualCourses(userId){ + return restGet("/usercourses/"+userId) +} \ No newline at end of file