Skip to content

Commit

Permalink
fix duplicate data issue when getting all skill requests
Browse files Browse the repository at this point in the history
  • Loading branch information
yesyash committed Jul 20, 2024
1 parent 66f824b commit 7045fad
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.RDS.skilltree.repositories;

import com.RDS.skilltree.models.Endorsement;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface EndorsementRepository extends JpaRepository<Endorsement, Integer> {
List<Endorsement> findBySkillId(Integer skillId);

List<Endorsement> findByEndorseIdAndSkillId(String endorseId, Integer skillId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
Expand All @@ -37,38 +40,41 @@ public List<SkillViewModel> getAll() {

@Override
public SkillRequestsDto getAllRequests() {
List<UserSkillsModel> pendingSkills =
userSkillRepository.findByStatus(UserSkillStatusEnum.PENDING);
Set<UserViewModel> uniqueUsers = new HashSet<>();

List<SkillRequestViewModel> skillRequests =
pendingSkills.stream()
.map(
pendingSkill -> {
List<Endorsement> endorsements =
endorsementRepository.findBySkillId(pendingSkill.getSkill().getId());

String endorseRdsUserId = pendingSkill.getUser().getRdsUserId();
RdsUserViewModel rdsDetails = rdsService.getUserDetails(endorseRdsUserId);
UserViewModel endorseDetails = getUserModalFromRdsDetails(pendingSkill.getUser().getId(), rdsDetails);

uniqueUsers.add(endorseDetails);

endorsements.forEach(
endorsement -> {
UserModel endorser = endorsement.getEndorser();

RdsUserViewModel rdsUserDetails = rdsService.getUserDetails(endorser.getRdsUserId());
UserViewModel endorserDetails = getUserModalFromRdsDetails(endorser.getId(), rdsUserDetails);

uniqueUsers.add(endorserDetails);
});

return SkillRequestViewModel.toViewModel(pendingSkill, endorsements);
})
.toList();

return SkillRequestsDto.toDto(skillRequests, new ArrayList<>(uniqueUsers));
List<UserSkillsModel> pendingSkills = userSkillRepository.findByStatus(UserSkillStatusEnum.PENDING);

// store all users data that are a part of this request cycle.
Map<String, UserViewModel> userDetails = new HashMap<>();

// make a list of all pending skill requests with their endorsement details
List<SkillRequestViewModel> skillRequests = pendingSkills.stream().map(skill -> {
Integer skillId = skill.getSkill().getId();
String endorseId = skill.getUser().getId();
String endorseRdsUserId = skill.getUser().getRdsUserId();

// Get all endorsement for a specific skill and user Id
List<Endorsement> endorsements = endorsementRepository.findByEndorseIdAndSkillId(endorseId, skillId);

if (!userDetails.containsKey(endorseId)) {
RdsUserViewModel endorseRdsDetails = rdsService.getUserDetails(endorseRdsUserId);
UserViewModel endorseDetails = getUserModalFromRdsDetails(endorseId, endorseRdsDetails);
userDetails.put(endorseId, endorseDetails);
}

endorsements.forEach(endorsement -> {
String endorserId = endorsement.getEndorser().getId();
String endorserRdsUserId = endorsement.getEndorser().getRdsUserId();

if (!userDetails.containsKey(endorserId)) {
RdsUserViewModel endorserRdsDetails = rdsService.getUserDetails(endorserRdsUserId);
UserViewModel endorserDetails = getUserModalFromRdsDetails(endorseId, endorserRdsDetails);
userDetails.put(endorserId, endorserDetails);
}
});

return SkillRequestViewModel.toViewModel(skill, endorsements);
}).toList();

return SkillRequestsDto.toDto(skillRequests, userDetails.values().stream().toList());
}

private static UserViewModel getUserModalFromRdsDetails(String id, RdsUserViewModel rdsDetails) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
import com.RDS.skilltree.User.UserSkillsModel;
import com.RDS.skilltree.models.Endorsement;
import com.RDS.skilltree.models.Skill;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.stream.Collectors;

@Data
@NoArgsConstructor
public class SkillRequestViewModel {
private Integer id;
private String name;
private Integer skillId;
private String skillName;
private String endorseId;
private List<MinimalEndorsementViewModel> endorsements;

Expand All @@ -24,8 +25,8 @@ public static SkillRequestViewModel toViewModel(
Skill skill = userSkillsModel.getSkill();
UserModel user = userSkillsModel.getUser();

skillRequestViewModel.setId(skill.getId());
skillRequestViewModel.setName(skill.getName());
skillRequestViewModel.setSkillId(skill.getId());
skillRequestViewModel.setSkillName(skill.getName());
skillRequestViewModel.setEndorseId(user.getId());
skillRequestViewModel.setEndorsements(
endorsements.stream()
Expand Down

0 comments on commit 7045fad

Please sign in to comment.