Skip to content

Commit

Permalink
Correct lint errors (#1632)
Browse files Browse the repository at this point in the history
  • Loading branch information
scmet committed May 12, 2024
1 parent 70e5cc0 commit eb98f88
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import de.thm.ii.fbs.util.JsonWrapper._

import javax.servlet.http.{HttpServletRequest, HttpServletResponse}
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.dao.DataIntegrityViolationException
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation._


/**
* Controller to manage rest api calls for a group resource.
*/
Expand All @@ -23,9 +21,9 @@ import org.springframework.web.bind.annotation._
@RequestMapping (path = Array("/api/v1/courses/{cid}/groups"), produces = Array(MediaType.APPLICATION_JSON_VALUE))
class GroupController{
@Autowired
private val groupService : GroupService = null
private val groupService: GroupService = null
@Autowired
private val authService : AuthService = null
private val authService: AuthService = null
@Autowired
private val courseRegistrationService: CourseRegistrationService = null

Expand All @@ -40,12 +38,13 @@ class GroupController{
*/
@GetMapping(value = Array(""))
@ResponseBody
def getAll(@PathVariable ("cid") cid : Integer, @RequestParam(value = "visible", required = false) ignoreHidden: Boolean,req: HttpServletRequest, res: HttpServletResponse): List[Group] = {
def getAll(@PathVariable ("cid") cid: Integer, @RequestParam(value = "visible", required = false)
ignoreHidden: Boolean, req: HttpServletRequest, res: HttpServletResponse): List[Group] = {
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)) =>
val groupList = groupService.getAll(cid, false)
case (GlobalRole.ADMIN | GlobalRole.MODERATOR, _) | (_, Some(CourseRole.DOCENT)) =>
val groupList = groupService.getAll(cid, ignoreHidden = false)
groupList
case _ => throw new ForbiddenException()
}
Expand All @@ -62,17 +61,17 @@ class GroupController{
*/
@PostMapping(value = Array(""), consumes = Array(MediaType.APPLICATION_JSON_VALUE))
@ResponseBody
def create(@PathVariable ("cid") cid : Int, req: HttpServletRequest, res: HttpServletResponse, @RequestBody body: JsonNode): Group = {
def create(@PathVariable ("cid") cid: Int, req: HttpServletRequest, res: HttpServletResponse, @RequestBody body: JsonNode): Group = {
val user = authService.authorize(req, res)
val someCourseRole = courseRegistrationService.getParticipants(cid).find(_.user.id == user.id).map(_.role)
if (!(user.globalRole == GlobalRole.ADMIN || user.globalRole == GlobalRole.MODERATOR || someCourseRole == Some(CourseRole.DOCENT))) {
if (!(user.globalRole == GlobalRole.ADMIN || user.globalRole == GlobalRole.MODERATOR || someCourseRole.contains(CourseRole.DOCENT))) {
throw new ForbiddenException()
}
val name = Option(body.get("name")).map(_.asText())
val membership = Option(body.get("membership")).map(_.asInt())
val visible = Option(body.get("visible")).map(_.asBoolean())
(name, membership, visible) match {
case (Some(name), Some (membership), Some(visible))
(name, membership, visible) match {
case (Some(name), Some (membership), Some(visible))
=> groupService.create(Group(0, cid, name, membership, visible))
case _ => throw new BadRequestException("Malformed Request Body")
}
Expand All @@ -89,12 +88,12 @@ class GroupController{
*/
@GetMapping(value = Array("/{gid}"))
@ResponseBody
def getOne(@PathVariable ("cid") cid : Integer, @PathVariable("gid") gid: Integer, req: HttpServletRequest, res: HttpServletResponse): Group = {
def getOne(@PathVariable ("cid") cid: Integer, @PathVariable("gid") gid: Integer, req: HttpServletRequest, res: HttpServletResponse): Group = {
val user = authService.authorize(req, res)
val someCourseRole = courseRegistrationService.getParticipants(cid).find(_.user.id == user.id).map(_.role)

groupService.get(cid, gid) match {
case Some(group) => if (!(user.globalRole == GlobalRole.ADMIN || user.globalRole == GlobalRole.MODERATOR || someCourseRole == Some(CourseRole.DOCENT))) {
case Some(group) => if (!(user.globalRole == GlobalRole.ADMIN || user.globalRole == GlobalRole.MODERATOR || someCourseRole.contains(CourseRole.DOCENT))) {
throw new ForbiddenException()
} else {
group
Expand All @@ -113,18 +112,18 @@ class GroupController{
* @param body Request Body
*/
@PutMapping(value = Array("/{gid}"))
def update(@PathVariable ("cid") cid : Integer, @PathVariable("gid") gid: Integer, req: HttpServletRequest, res: HttpServletResponse,
def update(@PathVariable ("cid") cid: Integer, @PathVariable("gid") gid: 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("name").asText(),
body.retrive("membership").asInt(),
body.retrive("visible").asBool()
) match {
case (Some(name), Some (membership), visible)
case (Some(name), Some (membership), visible)
=> groupService.update(cid, gid, Group(gid, cid, name, membership, visible.getOrElse(true)))
case _ => throw new BadRequestException("Malformed Request Body")
}
Expand All @@ -135,13 +134,13 @@ class GroupController{
/**
* Delete course
*
* @param cid Course id
* @param cid Course id
* @param gid Group id
* @param req http request
* @param res http response
*/
@DeleteMapping(value = Array("/{gid}"))
def delete(@PathVariable ("cid") cid : Integer, @PathVariable("gid") gid: Integer, req: HttpServletRequest, res: HttpServletResponse): Unit = {
def delete(@PathVariable ("cid") cid: Integer, @PathVariable("gid") gid: Integer, req: HttpServletRequest, res: HttpServletResponse): Unit = {
val user = authService.authorize(req, res)
val someCourseRole = courseRegistrationService.getParticipants(cid).find(_.user.id == user.id).map(_.role)

Expand All @@ -151,4 +150,4 @@ class GroupController{
case _ => throw new ForbiddenException()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package de.thm.ii.fbs.controller

import com.fasterxml.jackson.databind.JsonNode
import de.thm.ii.fbs.controller.exception.{BadRequestException, ForbiddenException, ResourceNotFoundException, MembershipExceededException}
import de.thm.ii.fbs.model.{Group, CourseRole, GlobalRole, User, Participant}
import de.thm.ii.fbs.controller.exception.{ForbiddenException, MembershipExceededException, ResourceNotFoundException}
import de.thm.ii.fbs.model.{CourseRole, GlobalRole, Group, Participant}
import de.thm.ii.fbs.services.persistence._
import de.thm.ii.fbs.services.security.AuthService
import de.thm.ii.fbs.util.JsonWrapper._

import javax.servlet.http.{HttpServletRequest, HttpServletResponse}
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.dao.DataIntegrityViolationException
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation._

Expand All @@ -32,13 +29,12 @@ class GroupRegistrationController {

/**
* Add a user to a group within a course
*
*
* @param cid Course id
* @param gid Group id
* @param uid User id
* @param req http request
* @param res http response
* @param body Content
*/
@PutMapping(value = Array("/courses/{cid}/groups/{gid}/users/{uid}"), consumes = Array(MediaType.APPLICATION_JSON_VALUE))
def addUserToGroup(@PathVariable("cid") cid: Int, @PathVariable("gid") gid: Int, @PathVariable("uid") uid: Int,
Expand All @@ -48,16 +44,16 @@ class GroupRegistrationController {
val hasCoursePrivileges = courseRegistrationService.getCoursePrivileges(user.id).getOrElse(cid, CourseRole.STUDENT) == CourseRole.DOCENT
if (hasGlobalPrivileges || hasCoursePrivileges || user.id == uid) {
//Check if the group is full
val currentMembership = groupRegistrationService.getGroupMembership(cid,gid)
val currentMembership = groupRegistrationService.getGroupMembership(cid, gid)
val group = groupService.get(cid, gid)
group match {
case Some(group) => val maxMembership: Int = group.membership
if (currentMembership < maxMembership) {
groupRegistrationService.addUserToGroup(uid, cid, gid)
} else {
throw new MembershipExceededException()
throw new MembershipExceededException()
}
case _ => throw new ResourceNotFoundException()
case _ => throw new ResourceNotFoundException()
}
} else {
throw new ForbiddenException()
Expand All @@ -66,15 +62,16 @@ class GroupRegistrationController {

/**
* Remove a user from a group
*
*
* @param uid User id
* @param cid Course id
* @param gid Group id
* @param req http request
* @param res http response
*/
@DeleteMapping(value = Array("/courses/{cid}/groups/{gid}/users/{uid}"))
def removeUserFromGroup(@PathVariable("uid") uid: Int, @PathVariable("cid") cid: Int, @PathVariable("gid") gid: Int, req: HttpServletRequest, res: HttpServletResponse): Unit = {
def removeUserFromGroup(@PathVariable("uid") uid: Int, @PathVariable("cid") cid: Int, @PathVariable("gid") gid: Int,
req: HttpServletRequest, res: HttpServletResponse): Unit = {
val user = authService.authorize(req, res)
val hasGlobalPrivileges = user.hasRole(GlobalRole.ADMIN, GlobalRole.MODERATOR)
val hasCoursePrivileges = courseRegistrationService.getCoursePrivileges(user.id).getOrElse(cid, CourseRole.STUDENT) == CourseRole.DOCENT
Expand All @@ -87,7 +84,7 @@ class GroupRegistrationController {

/**
* Remove all users from a group
*
*
* @param cid Course id
* @param gid Group id
* @param req http request
Expand All @@ -107,7 +104,7 @@ class GroupRegistrationController {

/**
* Retrieve all groups of a specific user
*
*
* @param uid User id
* @param req http request
* @param res http response
Expand All @@ -131,7 +128,7 @@ class GroupRegistrationController {
* @param gid Group id
* @param req http request
* @param res http response
* @return List of course participants
* @return List of course participants
*/
@GetMapping(value = Array("/courses/{cid}/groups/{gid}/participants"))
@ResponseBody
Expand All @@ -146,8 +143,3 @@ class GroupRegistrationController {
}
}
}





Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ import org.springframework.web.bind.annotation.ResponseStatus
* @param message A human readable String
*/
@ResponseStatus(value = HttpStatus.CONFLICT)
class MembershipExceededException(message: String = "Die Gruppe ist voll.") extends RuntimeException(message)
class MembershipExceededException(message: String = "Die Gruppe ist voll.") extends RuntimeException(message)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package de.thm.ii.fbs.model
* @param courseId course to which the group belongs
* @param name Name of the group
* @param membership The max number of members
* @param visible The visibility of the group, false = invisible
* @param visible The visibility of the group, false = invisible
*/

case class Group(id: Int, courseId: Int, name: String, membership: Int, visible: Boolean = true)
case class Group(id: Int, courseId: Int, name: String, membership: Int, visible: Boolean = true)
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,34 @@ class GroupRegistrationService {

/**
* Add a user to a group
*
*
* @param uid User id
* @param cid Course id
* @param gid Group id
* @return True if successful
*/
def addUserToGroup(uid:Int, cid: Int, gid: Int): Boolean =
1 == DB.update("INSERT INTO user_group (user_id, course_id, group_id) VALUES (?,?,?);",uid, cid, gid)
def addUserToGroup(uid: Int, cid: Int, gid: Int): Boolean =
1 == DB.update("INSERT INTO user_group (user_id, course_id, group_id) VALUES (?,?,?);", uid, cid, gid)

/**
* Remove a user from a group
*
* @param uid User id
* @param cid Course id
* @param gid Group id
* @return True if sucessfully deregistered
* @param gid Group id
* @return True if successfully deregistered
*/
def removeUserFromGroup(uid: Int, cid: Int, gid: Int): Boolean =
1 == DB.update("DELETE FROM user_group WHERE user_id = ? AND course_id = ? AND group_id = ?",uid, cid, gid)
1 == DB.update("DELETE FROM user_group WHERE user_id = ? AND course_id = ? AND group_id = ?", uid, cid, gid)

/**
* Remove all users from a group
*
* @param cid Course id
* @param gid Group id
* @return True if sucessfully deregistered
* @param gid Group id
* @return True if successfully deregistered
*/
def removeAllUsersFromGroup(cid: Int, gid: Int): Boolean =
def removeAllUsersFromGroup(cid: Int, gid: Int): Boolean =
1 == DB.update("DELETE FROM user_group WHERE course_id = ? AND group_id = ?", cid, gid)

/**
Expand All @@ -57,7 +57,8 @@ class GroupRegistrationService {
* @return List of groups
*/
def getUserGroups(uid: Int, ignoreHidden: Boolean = true): List[Group] = DB.query(
"SELECT g.group_id, g.course_id, g.name, g.membership, g.visible FROM `group` g JOIN user_group ug ON g.group_id = ug.group_id WHERE ug.user_id = ? ORDER BY g.course_id ASC"
"SELECT g.group_id, g.course_id, g.name, g.membership, g.visible " +
"FROM `group` g JOIN user_group ug ON g.group_id = ug.group_id WHERE ug.user_id = ? ORDER BY g.course_id ASC"
+ (if (ignoreHidden) " AND g.visible = 1" else ""),
(res, _) => parseResult(res), uid)

Expand All @@ -66,7 +67,7 @@ class GroupRegistrationService {
*
* @param cid Course id
* @param gid Group id
* @return List of members
* @return List of members
*/
def getMembers(cid: Int, gid: Int): List[Participant] = DB.query(
"SELECT u.user_id, u.prename, u.surname, u.email, u.username, u.alias, u.global_role, uc.course_role " +
Expand All @@ -78,25 +79,25 @@ class GroupRegistrationService {

/**
* Gets current number of members of a group
*
*
* @param cid Course id
* @param gid Group id
* @return Number of members
* @return Number of members
*/
def getGroupMembership(cid: Int, gid: Int) : Int = {
val groupMembershipRowMapper: RowMapper[Int] = (rs: ResultSet, rowNum: Int) => rs.getInt(1)
def getGroupMembership(cid: Int, gid: Int): Int = {
val groupMembershipRowMapper: RowMapper[Int] = (rs: ResultSet, _) => rs.getInt(1)
val sql = "SELECT COUNT(*) FROM user_group WHERE course_id = ? AND group_id = ?"
return jdbc.queryForObject(sql, groupMembershipRowMapper, cid, gid);
}
jdbc.queryForObject(sql, groupMembershipRowMapper, cid, gid)
}

private def parseResult(res: ResultSet): Group = Group(
id = res.getInt("group_id"),
courseId = res.getInt("course_id"),
name = res.getString("name"),
membership = res.getInt("membership"),
visible = res.getBoolean("visible"),
)

private def parseUserResult(res: ResultSet): User = new User(
prename = res.getString("prename"),
surname = res.getString("surname"),
Expand All @@ -107,6 +108,3 @@ class GroupRegistrationService {
id = res.getInt("user_id")
)
}



Loading

0 comments on commit eb98f88

Please sign in to comment.