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

Backend/group recommendations improvement #1076

Merged
merged 2 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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