From 75b24840f80b0a93956e53f6bf03e529cfcb04d1 Mon Sep 17 00:00:00 2001 From: Yash Raj <56453897+yesyash@users.noreply.github.com> Date: Sat, 31 Aug 2024 00:55:05 +0530 Subject: [PATCH] add status filter to get all skill requests api (#152) * add status filter to get all skill requests api * fix formatting * fix method name --- .../com/RDS/skilltree/apis/SkillsApi.java | 8 +- .../RDS/skilltree/services/SkillService.java | 2 + .../services/SkillServiceImplementation.java | 125 ++++++++++-------- .../viewmodels/SkillRequestViewModel.java | 13 +- ...SkillRequestsWithUserDetailsViewModel.java | 12 ++ 5 files changed, 103 insertions(+), 57 deletions(-) create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillRequestsWithUserDetailsViewModel.java 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 28ba482..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 @@ -5,6 +5,7 @@ import com.RDS.skilltree.dtos.SkillRequestActionRequestDto; import com.RDS.skilltree.dtos.SkillRequestsDto; import com.RDS.skilltree.enums.UserRoleEnum; +import com.RDS.skilltree.enums.UserSkillStatusEnum; import com.RDS.skilltree.services.EndorsementService; import com.RDS.skilltree.services.SkillService; import com.RDS.skilltree.utils.GenericResponse; @@ -36,7 +37,12 @@ public ResponseEntity> getAll() { @GetMapping("/requests") @AuthorizedRoles({UserRoleEnum.SUPERUSER}) - public ResponseEntity getAllRequests() { + public ResponseEntity getAllRequests( + @RequestParam(value = "status", required = false) UserSkillStatusEnum status) { + if (status != null) { + return ResponseEntity.ok(skillService.getRequestsByStatus(status)); + } + return ResponseEntity.ok(skillService.getAllRequests()); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/services/SkillService.java b/skill-tree/src/main/java/com/RDS/skilltree/services/SkillService.java index 06439ee..ce87d11 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/services/SkillService.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/services/SkillService.java @@ -14,6 +14,8 @@ public interface SkillService { SkillRequestsDto getAllRequests(); + SkillRequestsDto getRequestsByStatus(UserSkillStatusEnum status); + GenericResponse approveRejectSkillRequest( Integer skillId, String endorseId, UserSkillStatusEnum action); } 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 ee900f5..270cfe4 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 @@ -14,10 +14,7 @@ import com.RDS.skilltree.repositories.UserSkillRepository; import com.RDS.skilltree.services.external.RdsService; import com.RDS.skilltree.utils.GenericResponse; -import com.RDS.skilltree.viewmodels.CreateSkillViewModel; -import com.RDS.skilltree.viewmodels.SkillRequestViewModel; -import com.RDS.skilltree.viewmodels.SkillViewModel; -import com.RDS.skilltree.viewmodels.UserViewModel; +import com.RDS.skilltree.viewmodels.*; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -38,6 +35,16 @@ public class SkillServiceImplementation implements SkillService { private final UserSkillRepository userSkillRepository; private final EndorsementRepository endorsementRepository; + private static UserViewModel getUserModalFromRdsDetails( + String id, RdsGetUserDetailsResDto rdsDetails) { + String firstName = + rdsDetails.getUser().getFirst_name() != null ? rdsDetails.getUser().getFirst_name() : ""; + String lastName = + rdsDetails.getUser().getLast_name() != null ? rdsDetails.getUser().getLast_name() : ""; + + return UserViewModel.builder().id(id).name(firstName + ' ' + lastName).build(); + } + @Override public List getAll() { return skillRepository.findAll().stream() @@ -47,60 +54,22 @@ public List getAll() { @Override public SkillRequestsDto getAllRequests() { - List pendingSkills = userSkillRepository.findByStatus(UserSkillStatusEnum.PENDING); - - // store all users data that are a part of this request - Map userDetails = new HashMap<>(); - - // make a list of all pending skill requests with their endorsement details - List skillRequests = - pendingSkills.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); - } - }); - - return SkillRequestViewModel.toViewModel(skill, endorsements); - }) - .toList(); + List skillRequests = userSkillRepository.findAll(); + SkillRequestsWithUserDetailsViewModel skillRequestsWithUserDetails = + toSkillRequestsWithUserDetailsViewModel(skillRequests); - return SkillRequestsDto.toDto(skillRequests, userDetails.values().stream().toList()); + return SkillRequestsDto.toDto( + skillRequestsWithUserDetails.getSkillRequests(), skillRequestsWithUserDetails.getUsers()); } - private static UserViewModel getUserModalFromRdsDetails( - String id, RdsGetUserDetailsResDto rdsDetails) { - String firstName = - rdsDetails.getUser().getFirst_name() != null ? rdsDetails.getUser().getFirst_name() : ""; - String lastName = - rdsDetails.getUser().getLast_name() != null ? rdsDetails.getUser().getLast_name() : ""; + @Override + public SkillRequestsDto getRequestsByStatus(UserSkillStatusEnum status) { + List skillRequests = userSkillRepository.findByStatus(status); + SkillRequestsWithUserDetailsViewModel skillRequestsWithUserDetails = + toSkillRequestsWithUserDetailsViewModel(skillRequests); - return UserViewModel.builder().id(id).name(firstName + ' ' + lastName).build(); + return SkillRequestsDto.toDto( + skillRequestsWithUserDetails.getSkillRequests(), skillRequestsWithUserDetails.getUsers()); } @Override @@ -139,6 +108,54 @@ public GenericResponse approveRejectSkillRequest( return new GenericResponse<>("Skill {}", action.toString().toLowerCase()); } + private SkillRequestsWithUserDetailsViewModel toSkillRequestsWithUserDetailsViewModel( + 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); + } + }); + + return SkillRequestViewModel.toViewModel(skill, endorsements); + }) + .toList(); + + return SkillRequestsWithUserDetailsViewModel.builder() + .skillRequests(skillRequests) + .users(userDetails.values().stream().toList()) + .build(); + } + private Skill toEntity(CreateSkillViewModel viewModel) { Skill entity = new Skill(); BeanUtils.copyProperties(viewModel, entity); 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 87695b9..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 @@ -1,5 +1,6 @@ package com.RDS.skilltree.viewmodels; +import com.RDS.skilltree.enums.UserSkillStatusEnum; import com.RDS.skilltree.models.Endorsement; import com.RDS.skilltree.models.Skill; import com.RDS.skilltree.models.UserSkills; @@ -12,20 +13,27 @@ public class SkillRequestViewModel { private Integer skillId; private String skillName; private String endorseId; + private UserSkillStatusEnum status; private List endorsements; public SkillRequestViewModel( - Integer id, String name, String endorseId, List endorsements) { + Integer id, + String name, + String endorseId, + List endorsements, + UserSkillStatusEnum status) { this.skillId = id; this.skillName = name; this.endorseId = endorseId; this.endorsements = endorsements; + this.status = status; } public static SkillRequestViewModel toViewModel( UserSkills userSkills, List endorsements) { Skill skill = userSkills.getSkill(); String userId = userSkills.getUserId(); + UserSkillStatusEnum status = userSkills.getStatus(); return new SkillRequestViewModel( skill.getId(), @@ -33,6 +41,7 @@ public static SkillRequestViewModel toViewModel( userId, endorsements.stream() .map(MinimalEndorsementViewModel::toViewModel) - .collect(Collectors.toList())); + .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 new file mode 100644 index 0000000..c51de05 --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillRequestsWithUserDetailsViewModel.java @@ -0,0 +1,12 @@ +package com.RDS.skilltree.viewmodels; + +import java.util.List; +import lombok.Builder; +import lombok.Getter; + +@Builder +@Getter +public class SkillRequestsWithUserDetailsViewModel { + private List skillRequests; + private List users; +}