Skip to content
Snippets Groups Projects
Commit 24383edf authored by Anders Montsko Austlid's avatar Anders Montsko Austlid
Browse files

Merge branch 'feature/group-controller-get-endpoint' into 'main'

Improved response layout for group get endpoint

Closes #211

See merge request idatt2106-v23-03/backend!171
parents a177e649 f4038714
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,8 @@ package ntnu.idatt2016.v233.SmartMat.controller.group;
import lombok.AllArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.dto.request.group.GroupConnectionRequest;
import ntnu.idatt2016.v233.SmartMat.dto.request.group.GroupRequest;
import ntnu.idatt2016.v233.SmartMat.dto.response.group.GroupDetailsResponse;
import ntnu.idatt2016.v233.SmartMat.dto.response.group.UserAuthorityInfo;
import ntnu.idatt2016.v233.SmartMat.dto.response.group.GroupResponse;
import ntnu.idatt2016.v233.SmartMat.entity.group.Group;
import ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupAsso;
......@@ -13,10 +15,7 @@ import ntnu.idatt2016.v233.SmartMat.service.user.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
/**
* Controller for groups API, providing endpoints for group management
......@@ -51,25 +50,28 @@ public class GroupController {
* @return a ResponseEntity containing the group if it exists, or a 404 if it doesn't
*/
@GetMapping("/{groupId}")
public ResponseEntity<?> getGroupById(@PathVariable("groupId") long groupId){
public ResponseEntity<?> getGroupById(@PathVariable("groupId") long groupId) {
Optional<Group> group = groupService.getGroupById(groupId);
if(group.isPresent()) {
if (group.isPresent()) {
List<UserGroupAsso> users = group.get().getUser();
Map<String, String> usernames = new HashMap<>();
for (UserGroupAsso user : users) {
String userAuthority = groupService.getUserGroupAsso(user.getUser().getUsername(), groupId)
.get().getGroupAuthority();
List<UserAuthorityInfo> userAuthorityInfoList = new ArrayList<>();
if(groupService.getUserGroupAsso(user.getUser().getUsername(), groupId).isPresent()) {
usernames.put(user.getUser().getUsername(), userAuthority);
for (UserGroupAsso user : users) {
Optional<UserGroupAsso> userGroupAsso = groupService.getUserGroupAsso(user.getUser().getUsername(), groupId);
if (userGroupAsso.isPresent()) {
String userAuthority = userGroupAsso.get().getGroupAuthority();
userAuthorityInfoList.add(new UserAuthorityInfo(user.getUser().getUsername(), userAuthority));
}
usernames.put(user.getUser().getUsername(), userAuthority);
}
return ResponseEntity.ok(usernames);
GroupDetailsResponse groupDetailsResponse = new GroupDetailsResponse(group.get(), userAuthorityInfoList);
return ResponseEntity.ok(groupDetailsResponse);
}
return ResponseEntity.badRequest().body("Group not found.");
}
/**
* Creates a new group
*
......
package ntnu.idatt2016.v233.SmartMat.dto.response.group;
import lombok.Data;
import ntnu.idatt2016.v233.SmartMat.entity.group.Group;
import java.util.List;
@Data
public class GroupDetailsResponse {
private long groupId;
private String linkCode;
private List<UserAuthorityInfo> userAuthorityInfoList;
public GroupDetailsResponse(Group group, List<UserAuthorityInfo> userAuthorityInfoList) {
this.groupId = group.getGroupId();
this.linkCode = group.getLinkCode();
this.userAuthorityInfoList = userAuthorityInfoList;
}
}
package ntnu.idatt2016.v233.SmartMat.dto.response.group;
public record UserAuthorityInfo(String username, String authority) {}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment