Skip to content

Commit

Permalink
Merge pull request #853 from bounswe/backend/return-membership-info
Browse files Browse the repository at this point in the history
Changed return type of group detail
  • Loading branch information
Ardakacd authored Nov 26, 2023
2 parents ec95e63 + 0a991fe commit 15cfa98
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -47,14 +48,14 @@ public ResponseEntity<List<GetGroupResponseDto>> getGroups(
}

@GetMapping("/get")
public ResponseEntity<GetGroupResponseDto> getGroup(
public ResponseEntity<GetGroupDetailResponseDto> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Tag> 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<MemberInfo> moderators = new ArrayList<>(); // userIds of the moderators

private List<MemberInfo> members = new ArrayList<>(); // userIds of the members

private List<MemberInfo> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down Expand Up @@ -33,6 +34,8 @@ public class GroupService {

private final UserRepository userRepository;

private final ProfileRepository profileRepository;

private final MongoTemplate mongoTemplate;

private final ModelMapper modelMapper;
Expand All @@ -43,14 +46,17 @@ public GroupService(
GameRepository gameRepository,
ForumRepository forumRepository,
TagRepository tagRepository,
UserRepository userRepository, MongoTemplate mongoTemplate,
UserRepository userRepository,
ProfileRepository profileRepository,
MongoTemplate mongoTemplate,
ModelMapper modelMapper
) {
this.groupRepository = groupRepository;
this.gameRepository = gameRepository;
this.forumRepository = forumRepository;
this.tagRepository = tagRepository;
this.userRepository = userRepository;
this.profileRepository = profileRepository;
this.mongoTemplate = mongoTemplate;
this.modelMapper = modelMapper;

Expand All @@ -65,7 +71,17 @@ protected void configure() {
modelMapper.addMappings(new PropertyMap<Group, GetGroupResponseDto>() {
@Override
protected void configure() {
skip().setTags(null); // Exclude id from mapping
skip().setTags(null);
}
});

modelMapper.addMappings(new PropertyMap<Group, GetGroupDetailResponseDto>() {
@Override
protected void configure() {
skip().setTags(null);
skip().setModerators(null);
skip().setMembers(null);
skip().setBannedMembers(null);
}
});
}
Expand Down Expand Up @@ -99,6 +115,9 @@ public List<GetGroupResponseDto> 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) {
Expand Down Expand Up @@ -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<User> loggedInUser = userRepository.findByEmailAndIsDeletedFalse(email);
String loggedInUserId = loggedInUser.map(User::getId).orElse(null);
Expand All @@ -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> 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<GetGroupDetailResponseDto.MemberInfo> mapMemberIdsToMemberInfos(List<String> memberIds){
List<GetGroupDetailResponseDto.MemberInfo> memberInfos = new ArrayList<>();

for(String memberId : memberIds){

Optional<Profile> profileOfMember = profileRepository.findByUserIdAndIsDeletedFalse(memberId);
Optional<User> 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<Group> sameTitle = groupRepository.findByTitleAndIsDeletedFalse(request.getTitle());
Expand Down

0 comments on commit 15cfa98

Please sign in to comment.