Skip to content
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

Merged
merged 6 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +30,9 @@ public class ClusterController {
@Autowired
GroupRepository groupRepository;

@Autowired
GroupMemberController groupMemberController;

@Autowired
private ClusterUtil clusterUtil;
@Autowired
Expand Down Expand Up @@ -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");
}
Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Contributor

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


if(clusterFillJson.getClusterGroupMembers().keySet().size() > clusterJson.groupCount()){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("provided more groups than are allowed in the cluster");
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ik zou dus voor elke groep checken in de clusterFillJson checken of

  1. de groep een valid groep is & gekoppeld aan die cluster
  2. er niet meer members zijn dan de maxSize
  3. alle users valid users zijn die in de course zitten

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) {
Expand Down
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;
}

}
Loading