diff --git a/skill-tree/src/main/java/com/RDS/skilltree/apis/SkillsApi.java b/skill-tree/src/main/java/com/RDS/skilltree/apis/SkillsApi.java index 021370c..4254b0c 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/apis/SkillsApi.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/apis/SkillsApi.java @@ -37,8 +37,9 @@ public ResponseEntity> getAll() { @GetMapping("/requests") @AuthorizedRoles({UserRoleEnum.SUPERUSER}) - public ResponseEntity getAllRequests(@RequestParam (value = "status", required = false) UserSkillStatusEnum status) { - if (status != null){ + public ResponseEntity getAllRequests( + @RequestParam(value = "status", required = false) UserSkillStatusEnum status) { + if (status != null) { return ResponseEntity.ok(skillService.getRequestsByStatus(status)); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/services/SkillServiceImplementation.java b/skill-tree/src/main/java/com/RDS/skilltree/services/SkillServiceImplementation.java index fd7cfab..80c69ad 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/services/SkillServiceImplementation.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/services/SkillServiceImplementation.java @@ -15,7 +15,6 @@ import com.RDS.skilltree.services.external.RdsService; import com.RDS.skilltree.utils.GenericResponse; import com.RDS.skilltree.viewmodels.*; - import java.util.HashMap; import java.util.List; import java.util.Map; @@ -46,17 +45,21 @@ public List getAll() { @Override public SkillRequestsDto getAllRequests() { List skillRequests = userSkillRepository.findAll(); - SkillRequestsWithUserDetailsViewModel skillRequestsWithUserDetails = toSkillRequestsViewModel(skillRequests); + SkillRequestsWithUserDetailsViewModel skillRequestsWithUserDetails = + toSkillRequestsViewModel(skillRequests); - return SkillRequestsDto.toDto(skillRequestsWithUserDetails.getSkillRequests(), skillRequestsWithUserDetails.getUsers()); + return SkillRequestsDto.toDto( + skillRequestsWithUserDetails.getSkillRequests(), skillRequestsWithUserDetails.getUsers()); } @Override public SkillRequestsDto getRequestsByStatus(UserSkillStatusEnum status) { List skillRequests = userSkillRepository.findByStatus(status); - SkillRequestsWithUserDetailsViewModel skillRequestsWithUserDetails = toSkillRequestsViewModel(skillRequests); + SkillRequestsWithUserDetailsViewModel skillRequestsWithUserDetails = + toSkillRequestsViewModel(skillRequests); - return SkillRequestsDto.toDto(skillRequestsWithUserDetails.getSkillRequests(), skillRequestsWithUserDetails.getUsers()); + return SkillRequestsDto.toDto( + skillRequestsWithUserDetails.getSkillRequests(), skillRequestsWithUserDetails.getUsers()); } private static UserViewModel getUserModalFromRdsDetails( @@ -105,47 +108,51 @@ public GenericResponse approveRejectSkillRequest( return new GenericResponse<>("Skill {}", action.toString().toLowerCase()); } - private SkillRequestsWithUserDetailsViewModel toSkillRequestsViewModel(List skills){ + private SkillRequestsWithUserDetailsViewModel toSkillRequestsViewModel(List skills) { // store all users data that are a part of this request Map userDetails = new HashMap<>(); - List skillRequests = skills.stream() - .map( - skill -> { - Integer skillId = skill.getSkill().getId(); - - String endorseId = skill.getUserId(); - - // Get all endorsement for a specific skill and user Id - List endorsements = - endorsementRepository.findByEndorseIdAndSkillId(endorseId, skillId); - - if (!userDetails.containsKey(endorseId)) { - RdsGetUserDetailsResDto endorseRdsDetails = - rdsService.getUserDetails(endorseId); - UserViewModel endorseDetails = - getUserModalFromRdsDetails(endorseId, endorseRdsDetails); - userDetails.put(endorseId, endorseDetails); - } - - endorsements.forEach( - endorsement -> { - String endorserId = endorsement.getEndorserId(); - - if (!userDetails.containsKey(endorserId)) { - RdsGetUserDetailsResDto endorserRdsDetails = - rdsService.getUserDetails(endorserId); - UserViewModel endorserDetails = - getUserModalFromRdsDetails(endorserId, endorserRdsDetails); - userDetails.put(endorserId, endorserDetails); - } - }); + List skillRequests = + skills.stream() + .map( + skill -> { + Integer skillId = skill.getSkill().getId(); + + String endorseId = skill.getUserId(); - return SkillRequestViewModel.toViewModel(skill, endorsements); - }) - .toList(); + // Get all endorsement for a specific skill and user Id + List endorsements = + endorsementRepository.findByEndorseIdAndSkillId(endorseId, skillId); + + if (!userDetails.containsKey(endorseId)) { + RdsGetUserDetailsResDto endorseRdsDetails = + rdsService.getUserDetails(endorseId); + UserViewModel endorseDetails = + getUserModalFromRdsDetails(endorseId, endorseRdsDetails); + userDetails.put(endorseId, endorseDetails); + } - return SkillRequestsWithUserDetailsViewModel.builder().skillRequests(skillRequests).users(userDetails.values().stream().toList()).build(); + endorsements.forEach( + endorsement -> { + String endorserId = endorsement.getEndorserId(); + + if (!userDetails.containsKey(endorserId)) { + RdsGetUserDetailsResDto endorserRdsDetails = + rdsService.getUserDetails(endorserId); + UserViewModel endorserDetails = + getUserModalFromRdsDetails(endorserId, endorserRdsDetails); + userDetails.put(endorserId, endorserDetails); + } + }); + + return SkillRequestViewModel.toViewModel(skill, endorsements); + }) + .toList(); + + return SkillRequestsWithUserDetailsViewModel.builder() + .skillRequests(skillRequests) + .users(userDetails.values().stream().toList()) + .build(); } private Skill toEntity(CreateSkillViewModel viewModel) { diff --git a/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillRequestViewModel.java b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillRequestViewModel.java index 226397e..8c54d54 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillRequestViewModel.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillRequestViewModel.java @@ -4,10 +4,9 @@ import com.RDS.skilltree.models.Endorsement; import com.RDS.skilltree.models.Skill; import com.RDS.skilltree.models.UserSkills; -import lombok.Data; - import java.util.List; import java.util.stream.Collectors; +import lombok.Data; @Data public class SkillRequestViewModel { @@ -18,7 +17,11 @@ public class SkillRequestViewModel { private List endorsements; public SkillRequestViewModel( - Integer id, String name, String endorseId, List endorsements, UserSkillStatusEnum status) { + Integer id, + String name, + String endorseId, + List endorsements, + UserSkillStatusEnum status) { this.skillId = id; this.skillName = name; this.endorseId = endorseId; @@ -38,6 +41,7 @@ public static SkillRequestViewModel toViewModel( userId, endorsements.stream() .map(MinimalEndorsementViewModel::toViewModel) - .collect(Collectors.toList()), status); + .collect(Collectors.toList()), + status); } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillRequestsWithUserDetailsViewModel.java b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillRequestsWithUserDetailsViewModel.java index ccf2efb..c51de05 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillRequestsWithUserDetailsViewModel.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillRequestsWithUserDetailsViewModel.java @@ -1,13 +1,12 @@ package com.RDS.skilltree.viewmodels; +import java.util.List; import lombok.Builder; import lombok.Getter; -import java.util.List; - @Builder @Getter public class SkillRequestsWithUserDetailsViewModel { private List skillRequests; - private List users; + private List users; }