diff --git a/app/backend/src/main/java/com/app/gamereview/controller/GroupController.java b/app/backend/src/main/java/com/app/gamereview/controller/GroupController.java index a95312f5..24f5746c 100644 --- a/app/backend/src/main/java/com/app/gamereview/controller/GroupController.java +++ b/app/backend/src/main/java/com/app/gamereview/controller/GroupController.java @@ -1,6 +1,7 @@ package com.app.gamereview.controller; import com.app.gamereview.dto.request.group.*; +import com.app.gamereview.dto.response.group.GetGroupDetailResponseDto; import com.app.gamereview.dto.response.group.GetGroupResponseDto; import com.app.gamereview.dto.response.tag.AddGroupTagResponseDto; import com.app.gamereview.model.Group; @@ -47,14 +48,14 @@ public ResponseEntity> getGroups( } @GetMapping("/get") - public ResponseEntity getGroup( + public ResponseEntity getGroup( @RequestParam String id, @RequestHeader(name = HttpHeaders.AUTHORIZATION, required = false) String Authorization) { String email; if (JwtUtil.validateToken(Authorization)) email = JwtUtil.extractSubject(Authorization); else email = ""; - GetGroupResponseDto group = groupService.getGroupById(id, email); + GetGroupDetailResponseDto group = groupService.getGroupById(id, email); return ResponseEntity.ok(group); } diff --git a/app/backend/src/main/java/com/app/gamereview/dto/response/group/GetGroupDetailResponseDto.java b/app/backend/src/main/java/com/app/gamereview/dto/response/group/GetGroupDetailResponseDto.java new file mode 100644 index 00000000..f7c20b85 --- /dev/null +++ b/app/backend/src/main/java/com/app/gamereview/dto/response/group/GetGroupDetailResponseDto.java @@ -0,0 +1,54 @@ +package com.app.gamereview.dto.response.group; + +import com.app.gamereview.enums.MembershipPolicy; +import com.app.gamereview.model.Tag; +import lombok.Getter; +import lombok.Setter; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + + +@Getter +@Setter +public class GetGroupDetailResponseDto { + private String id; + + private String title; + + private String description; + + private MembershipPolicy membershipPolicy; + + private List tags = new ArrayList<>(); // list of tags + + private String gameId; // id of related game + + private String forumId; // id of the forum of the group + + private int quota; + + private List moderators = new ArrayList<>(); // userIds of the moderators + + private List members = new ArrayList<>(); // userIds of the members + + private List bannedMembers = new ArrayList<>(); // userIds of the banned members + + private Boolean avatarOnly; + + private LocalDateTime createdAt; + + private Boolean userJoined; + + public static class MemberInfo { + public String id; + + public String username; + + public String photoUrl; + } + public void populateTag(Tag tag){ + this.tags.add(tag); + } +} diff --git a/app/backend/src/main/java/com/app/gamereview/service/GroupService.java b/app/backend/src/main/java/com/app/gamereview/service/GroupService.java index 692d06e9..4579275a 100644 --- a/app/backend/src/main/java/com/app/gamereview/service/GroupService.java +++ b/app/backend/src/main/java/com/app/gamereview/service/GroupService.java @@ -1,6 +1,7 @@ package com.app.gamereview.service; import com.app.gamereview.dto.request.group.*; +import com.app.gamereview.dto.response.group.GetGroupDetailResponseDto; import com.app.gamereview.dto.response.group.GetGroupResponseDto; import com.app.gamereview.dto.response.tag.AddGroupTagResponseDto; import com.app.gamereview.enums.*; @@ -33,6 +34,8 @@ public class GroupService { private final UserRepository userRepository; + private final ProfileRepository profileRepository; + private final MongoTemplate mongoTemplate; private final ModelMapper modelMapper; @@ -43,7 +46,9 @@ public GroupService( GameRepository gameRepository, ForumRepository forumRepository, TagRepository tagRepository, - UserRepository userRepository, MongoTemplate mongoTemplate, + UserRepository userRepository, + ProfileRepository profileRepository, + MongoTemplate mongoTemplate, ModelMapper modelMapper ) { this.groupRepository = groupRepository; @@ -51,6 +56,7 @@ public GroupService( this.forumRepository = forumRepository; this.tagRepository = tagRepository; this.userRepository = userRepository; + this.profileRepository = profileRepository; this.mongoTemplate = mongoTemplate; this.modelMapper = modelMapper; @@ -65,7 +71,17 @@ protected void configure() { modelMapper.addMappings(new PropertyMap() { @Override protected void configure() { - skip().setTags(null); // Exclude id from mapping + skip().setTags(null); + } + }); + + modelMapper.addMappings(new PropertyMap() { + @Override + protected void configure() { + skip().setTags(null); + skip().setModerators(null); + skip().setMembers(null); + skip().setBannedMembers(null); } }); } @@ -99,6 +115,9 @@ public List getAllGroups(GetAllGroupsFilterRequestDto filte } query.addCriteria(Criteria.where("gameId").is(game.get().getId())); } + if (!filter.getWithDeleted()) { + query.addCriteria(Criteria.where("isDeleted").in(filter.getWithDeleted())); + } if (filter.getSortBy() != null) { Sort.Direction sortDirection = Sort.Direction.DESC; if (filter.getSortDirection() != null) { @@ -132,7 +151,7 @@ else if (filter.getSortBy().equals(SortType.QUOTA.name())) { return responseDtos; } - public GetGroupResponseDto getGroupById(String groupId, String email){ + public GetGroupDetailResponseDto getGroupById(String groupId, String email){ Optional loggedInUser = userRepository.findByEmailAndIsDeletedFalse(email); String loggedInUserId = loggedInUser.map(User::getId).orElse(null); @@ -144,17 +163,43 @@ public GetGroupResponseDto getGroupById(String groupId, String email){ } Group group = isGroupExists.get(); - GetGroupResponseDto dto = modelMapper.map(group, GetGroupResponseDto.class); + GetGroupDetailResponseDto dto = modelMapper.map(group, GetGroupDetailResponseDto.class); + for(String tagId : group.getTags()){ Optional tag = tagRepository.findById(tagId); tag.ifPresent(dto::populateTag); } + dto.setMembers(mapMemberIdsToMemberInfos(group.getMembers())); + dto.setModerators(mapMemberIdsToMemberInfos(group.getModerators())); + dto.setBannedMembers(mapMemberIdsToMemberInfos(group.getBannedMembers())); + dto.setUserJoined(group.getMembers().contains(loggedInUserId)); return dto; } + public List mapMemberIdsToMemberInfos(List memberIds){ + List memberInfos = new ArrayList<>(); + + for(String memberId : memberIds){ + + Optional profileOfMember = profileRepository.findByUserIdAndIsDeletedFalse(memberId); + Optional user = userRepository.findByIdAndIsDeletedFalse(memberId); + + if(user.isEmpty() || profileOfMember.isEmpty()) + continue; + + GetGroupDetailResponseDto.MemberInfo memberInfo = new GetGroupDetailResponseDto.MemberInfo(); + memberInfo.id = memberId; + memberInfo.username = user.get().getUsername(); + memberInfo.photoUrl = profileOfMember.get().getProfilePhoto(); + + memberInfos.add(memberInfo); + } + return memberInfos; + } + public Group createGroup(CreateGroupRequestDto request, User user){ Optional sameTitle = groupRepository.findByTitleAndIsDeletedFalse(request.getTitle());