-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Een groepcluster vullen met 1 request #252
Changes from 2 commits
31729e4
fb6c364
f7ef3d2
441a73c
024b035
b5808f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
import com.ugent.pidgeon.postgre.models.types.UserRole; | ||
import com.ugent.pidgeon.postgre.repository.*; | ||
import com.ugent.pidgeon.util.*; | ||
import java.util.Collections; | ||
import java.util.logging.Logger; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
|
@@ -28,6 +30,9 @@ public class ClusterController { | |
@Autowired | ||
GroupRepository groupRepository; | ||
|
||
@Autowired | ||
GroupMemberController groupMemberController; | ||
|
||
@Autowired | ||
private ClusterUtil clusterUtil; | ||
@Autowired | ||
|
@@ -168,6 +173,68 @@ public ResponseEntity<?> doGroupClusterUpdate(GroupClusterEntity clusterEntity, | |
return ResponseEntity.ok(entityToJsonConverter.clusterEntityToClusterJson(clusterEntity)); | ||
} | ||
|
||
/** | ||
* Fills up the groups in a cluster by providing a map of groupids with lists of userids | ||
* | ||
* @param clusterid identifier of a cluster | ||
* @param auth authentication object of the requesting user | ||
* @param clusterFillJson ClusterFillJson object containing a map of all groups and their | ||
* members of that cluster | ||
* @return ResponseEntity<?> | ||
* @HttpMethod PUT | ||
* @ApiPath /api/clusters/{clusterid}/fill | ||
* @AllowedRoles student, teacher | ||
*/ | ||
@PutMapping(ApiRoutes.CLUSTER_BASE_PATH + "/{clusterid}/fill") | ||
@Roles({UserRole.teacher, UserRole.student}) | ||
public ResponseEntity<?> fillCluster(@PathVariable("clusterid") Long clusterid, Auth auth, @RequestBody ClusterFillJson clusterFillJson) { | ||
CheckResult<GroupClusterEntity> checkResult = clusterUtil.getGroupClusterEntityIfAdminAndNotIndividual(clusterid, auth.getUserEntity()); | ||
|
||
if (checkResult.getStatus() != HttpStatus.OK) { | ||
return ResponseEntity.status(checkResult.getStatus()).body(checkResult.getMessage()); | ||
} | ||
|
||
GroupClusterEntity groupCluster = checkResult.getData(); | ||
|
||
ResponseEntity<?> response = getCluster(groupCluster.getId(), auth); | ||
if(response.getStatusCode() != HttpStatus.OK){ | ||
return response; | ||
} | ||
|
||
GroupClusterJson clusterJson = (GroupClusterJson) response.getBody(); | ||
if(clusterJson == null){ | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Group cluster could not be found"); | ||
} | ||
|
||
if(clusterFillJson.getClusterGroupMembers().keySet().size() > clusterJson.groupCount()){ | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("provided more groups than are allowed in the cluster"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dit mag dus weg |
||
|
||
if(clusterFillJson.getClusterGroupMembers().values().stream().anyMatch(members -> members.length > clusterJson.capacity())){ | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("you made a group with too many members"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ik zou dus voor elke groep checken in de clusterFillJson checken of
Eenmaal dit voor alle groepen id request gecheckt is kunnen ze toegevoegd worden zoals je dit reeds doet |
||
} | ||
|
||
try { | ||
for(GroupJson groupJson: clusterJson.groups()){ | ||
commonDatabaseActions.removeGroup(groupJson.getGroupId()); | ||
} | ||
|
||
for(Long groupId: clusterFillJson.getClusterGroupMembers().keySet()){ | ||
Long[] users = clusterFillJson.getClusterGroupMembers().get(groupId); | ||
GroupEntity groupEntity = new GroupEntity("group " + groupId, clusterJson.clusterId()); | ||
groupRepository.save(groupEntity); | ||
for(Long userid: users){ | ||
groupMemberController.addMemberToGroup(groupId, userid, auth); | ||
} | ||
} | ||
return ResponseEntity.status(HttpStatus.OK).body("Filled group cluster successfully"); | ||
}catch (Exception e) { | ||
Logger.getGlobal().severe(e.getMessage()); | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Something went wrong"); | ||
} | ||
} | ||
|
||
|
||
@PatchMapping(ApiRoutes.CLUSTER_BASE_PATH + "/{clusterid}") | ||
@Roles({UserRole.teacher, UserRole.student}) | ||
public ResponseEntity<?> patchCluster(@PathVariable("clusterid") Long clusterid, Auth auth, @RequestBody GroupClusterUpdateJson clusterJson) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.ugent.pidgeon.model.json; | ||
|
||
import java.util.Map; | ||
|
||
public class ClusterFillJson { | ||
|
||
private Map<Long, Long[]> clusterGroupMembers; | ||
|
||
|
||
public ClusterFillJson(Map<Long, Long[]> clusterGroupMembers) { | ||
this.clusterGroupMembers = clusterGroupMembers; | ||
} | ||
|
||
public ClusterFillJson() { | ||
} | ||
|
||
|
||
public Map<Long, Long[]> getClusterGroupMembers() { | ||
return clusterGroupMembers; | ||
} | ||
|
||
public void setClusterGroupMembers(Map<Long, Long[]> clusterGroupMembers) { | ||
this.clusterGroupMembers = clusterGroupMembers; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dit lijkt me overbodig aangezien je de GroupClusterEntity al hebt
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ik doe dit omdat een GroupClusterEntity niet de groep-info bevat om alle groepen te verwijderen
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GroupRepository.findAllByClusterId(ClusterId)
zou ik hier dan voor gebruiken