From 3057599485c99c6f87d405161088661836284a18 Mon Sep 17 00:00:00 2001 From: Sophie Methe Date: Sat, 17 Aug 2024 12:26:15 +0200 Subject: [PATCH] Refactor Course API to save group registration settings (#1675) --- .../22_group_selection_field_added.sql | 8 ++++++ .../ii/fbs/controller/CourseController.scala | 28 +++++++++++++++++++ .../scala/de/thm/ii/fbs/model/Course.scala | 4 ++- .../services/persistence/CourseService.scala | 12 ++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 modules/fbs-core/api/src/main/resources/migrations/22_group_selection_field_added.sql diff --git a/modules/fbs-core/api/src/main/resources/migrations/22_group_selection_field_added.sql b/modules/fbs-core/api/src/main/resources/migrations/22_group_selection_field_added.sql new file mode 100644 index 000000000..ab3523c6e --- /dev/null +++ b/modules/fbs-core/api/src/main/resources/migrations/22_group_selection_field_added.sql @@ -0,0 +1,8 @@ +BEGIN; + +alter table `fbs`.`course` + ADD `group_selection` BOOLEAN NULL DEFAULT NULL; + +INSERT INTO migration (number) VALUES (22); + +COMMIT; \ No newline at end of file diff --git a/modules/fbs-core/api/src/main/scala/de/thm/ii/fbs/controller/CourseController.scala b/modules/fbs-core/api/src/main/scala/de/thm/ii/fbs/controller/CourseController.scala index a54dcfc31..9316381c3 100644 --- a/modules/fbs-core/api/src/main/scala/de/thm/ii/fbs/controller/CourseController.scala +++ b/modules/fbs-core/api/src/main/scala/de/thm/ii/fbs/controller/CourseController.scala @@ -164,4 +164,32 @@ class CourseController { case _ => throw new ForbiddenException() } } + + + /** + * Update only the group selection of a course + * + * @param cid Course id + * @param req http request + * @param res http response + * @param body Request Body + */ + @PutMapping(value = Array("/{cid}/groupSelection")) + def updateGroupSelection(@PathVariable("cid") cid: Integer, req: HttpServletRequest, res: HttpServletResponse, + @RequestBody body: JsonNode): Unit = { + val user = authService.authorize(req, res) + val someCourseRole = courseRegistrationService.getParticipants(cid).find(_.user.id == user.id).map(_.role) + + (user.globalRole, someCourseRole) match { + case (GlobalRole.ADMIN | GlobalRole.MODERATOR, _) | (_, Some(CourseRole.DOCENT)) => + ( + body.retrive("groupSelection").asBool(), + ) match { + case (Some(groupSelection)) => + courseService.updateGroupSelection(cid, groupSelection) + case _ => throw new BadRequestException("Malformed Request Body") + } + case _ => throw new ForbiddenException() + } + } } diff --git a/modules/fbs-core/api/src/main/scala/de/thm/ii/fbs/model/Course.scala b/modules/fbs-core/api/src/main/scala/de/thm/ii/fbs/model/Course.scala index ad6c8f37e..b2a7cf3f6 100644 --- a/modules/fbs-core/api/src/main/scala/de/thm/ii/fbs/model/Course.scala +++ b/modules/fbs-core/api/src/main/scala/de/thm/ii/fbs/model/Course.scala @@ -7,5 +7,7 @@ package de.thm.ii.fbs.model * @param description The description of this course * @param visible The visibility of the course, false = invisible * @param id The id of the course, if 0, then none was assigned. + * @param groupSelection Whether registration for groups is possible */ -case class Course(name: String, description: String = "", visible: Boolean = true, id: Int = 0, semesterId: Option[Int] = None) +case class Course(name: String, description: String = "", visible: Boolean = true, id: Int = 0, semesterId: Option[Int] = None, + groupSelection: Option[Boolean] = None) diff --git a/modules/fbs-core/api/src/main/scala/de/thm/ii/fbs/services/persistence/CourseService.scala b/modules/fbs-core/api/src/main/scala/de/thm/ii/fbs/services/persistence/CourseService.scala index c37955b29..1cbc9dca9 100644 --- a/modules/fbs-core/api/src/main/scala/de/thm/ii/fbs/services/persistence/CourseService.scala +++ b/modules/fbs-core/api/src/main/scala/de/thm/ii/fbs/services/persistence/CourseService.scala @@ -100,4 +100,16 @@ class CourseService { Some(tmp) } } + + /** + * Update only the group selection of a course + * + * @param cid The course id + * @param groupSelection The new group selection status + * @return True if successful + */ + def updateGroupSelection(cid: Int, groupSelection: Boolean): Boolean = { + 1 == DB.update("UPDATE course SET group_selection = ? WHERE course_id = ?", + groupSelection, cid) + } }