Skip to content

Commit

Permalink
Merge pull request #1076 from bounswe/backend/group-recommendations-i…
Browse files Browse the repository at this point in the history
…mprovement

Backend/group recommendations improvement
  • Loading branch information
canuzdrn authored Dec 23, 2023
2 parents 1f0e3c7 + a62f4ad commit 838c038
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/group")
Expand Down Expand Up @@ -184,11 +185,16 @@ public ResponseEntity<List<GroupApplicationResponseDto>> listApplications(@Reque
return ResponseEntity.ok(applications);
}

@AuthorizationRequired
@GetMapping("/get-recommendations")
public ResponseEntity<List<Group>> getRecommendedGroups(@RequestHeader String Authorization, HttpServletRequest request) {
User user = (User) request.getAttribute("authenticatedUser");
List<Group> groups = groupService.getRecommendedGroups(user);
public ResponseEntity<List<Group>> getRecommendedGroups(@RequestHeader(name = HttpHeaders.AUTHORIZATION,
required = false) String Authorization) {
String email = "";
if(Authorization == null){
return ResponseEntity.ok(groupService.getRecommendedGroups(email));
}
if (JwtUtil.validateToken(Authorization))
email = JwtUtil.extractSubject(Authorization);
List<Group> groups = groupService.getRecommendedGroups(email);
return ResponseEntity.ok(groups);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -639,29 +639,65 @@ private GroupApplicationResponseDto contertToDto(GroupApplication application) {
return dto;
}

public List<Group> getRecommendedGroups(User user) {
Optional<Profile> findProfile = profileRepository.findByUserIdAndIsDeletedFalse(user.getId());
public List<Group> getRecommendedGroups(String email) {
Optional<User> optUser = userRepository.findByEmailAndIsDeletedFalse(email);

if(optUser.isEmpty()){
Query query = new Query();
query.addCriteria(Criteria.where("isDeleted").is(false));
query.limit(10);
return mongoTemplate.find(query, Group.class);
}
User user = optUser.get();

Optional<Profile> findProfile = profileRepository.findByUserIdAndIsDeletedFalse(user.getId());
if (findProfile.isEmpty()) {
throw new ResourceNotFoundException("Profile of the user is not found, unexpected error has occurred");
}

List<Group> recommendations = new ArrayList<>();
List<Group> memberGroups = groupRepository.findUserGroups(user.getId());
List<String> userGames = findProfile.get().getGames();
if (memberGroups.isEmpty()) {
return Collections.emptyList();
if (userGames.isEmpty()){
Query query = new Query();
query.addCriteria(Criteria.where("isDeleted").is(false));
query.limit(10);
return mongoTemplate.find(query, Group.class);
}else{
List<Group> recommendationGameGroups = new ArrayList<>();
for(String gameId : userGames){
Query query = new Query();
query.addCriteria(Criteria.where("isDeleted").is(false));
query.addCriteria(Criteria.where("gameId").is(gameId));
query.limit(2);
List<Group> gameGroups = mongoTemplate.find(query, Group.class);
recommendationGameGroups.addAll(gameGroups);
}
return recommendationGameGroups;
}
}
TreeSet<RecommendGroupDto> recommendedGroups = new TreeSet<>(Comparator.reverseOrder());
for (Group group : memberGroups) {
String groupId = group.getId();
recommendedGroups.addAll(recommendationByGroupId(groupId));
}

List<Group> recommendations = new ArrayList<>();

if(!userGames.isEmpty()){
List<Group> recommendedGameGroups = new ArrayList<>();
for(String gameId : userGames){
Query query = new Query();
query.addCriteria(Criteria.where("isDeleted").is(false));
query.addCriteria(Criteria.where("gameId").is(gameId));
query.limit(2);
List<Group> gameGroups = mongoTemplate.find(query, Group.class);
recommendedGameGroups.addAll(gameGroups);
}
recommendations.addAll(recommendedGameGroups);
}
for (RecommendGroupDto groupDto : recommendedGroups) {
recommendations.add(groupDto.getGroup());
}


return recommendations;
}

Expand Down Expand Up @@ -696,7 +732,7 @@ public TreeSet<RecommendGroupDto> recommendationByGroupId(String groupId) {
}
}

Query allGroupsQuery = new Query(); // all games except the base game
Query allGroupsQuery = new Query();
allGroupsQuery.addCriteria(Criteria.where("isDeleted").is(false));
allGroupsQuery.addCriteria(Criteria.where("_id").nin(idList));

Expand Down

0 comments on commit 838c038

Please sign in to comment.