From 86a6869f0e228f67d70950674d0a9269218f228b Mon Sep 17 00:00:00 2001 From: yash raj Date: Tue, 27 Aug 2024 02:37:38 +0530 Subject: [PATCH 1/3] add status filter to get all skill requests api --- .../com/RDS/skilltree/apis/SkillsApi.java | 7 +- .../RDS/skilltree/services/SkillService.java | 2 + .../services/SkillServiceImplementation.java | 99 ++++++++++--------- .../viewmodels/SkillRequestViewModel.java | 11 ++- ...SkillRequestsWithUserDetailsViewModel.java | 13 +++ 5 files changed, 83 insertions(+), 49 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..021370c 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,11 @@ 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..fd7cfab 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,8 @@ 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; @@ -47,50 +45,18 @@ public List getAll() { @Override public SkillRequestsDto getAllRequests() { - List pendingSkills = userSkillRepository.findByStatus(UserSkillStatusEnum.PENDING); + List skillRequests = userSkillRepository.findAll(); + SkillRequestsWithUserDetailsViewModel skillRequestsWithUserDetails = toSkillRequestsViewModel(skillRequests); - // 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 SkillRequestsDto.toDto(skillRequestsWithUserDetails.getSkillRequests(), skillRequestsWithUserDetails.getUsers()); + } - return SkillRequestViewModel.toViewModel(skill, endorsements); - }) - .toList(); + @Override + public SkillRequestsDto getRequestsByStatus(UserSkillStatusEnum status) { + List skillRequests = userSkillRepository.findByStatus(status); + SkillRequestsWithUserDetailsViewModel skillRequestsWithUserDetails = toSkillRequestsViewModel(skillRequests); - return SkillRequestsDto.toDto(skillRequests, userDetails.values().stream().toList()); + return SkillRequestsDto.toDto(skillRequestsWithUserDetails.getSkillRequests(), skillRequestsWithUserDetails.getUsers()); } private static UserViewModel getUserModalFromRdsDetails( @@ -139,6 +105,49 @@ public GenericResponse approveRejectSkillRequest( return new GenericResponse<>("Skill {}", action.toString().toLowerCase()); } + 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); + } + }); + + 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..226397e 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,31 +1,36 @@ 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; +import lombok.Data; + import java.util.List; import java.util.stream.Collectors; -import lombok.Data; @Data 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 +38,6 @@ 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..ccf2efb --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillRequestsWithUserDetailsViewModel.java @@ -0,0 +1,13 @@ +package com.RDS.skilltree.viewmodels; + +import lombok.Builder; +import lombok.Getter; + +import java.util.List; + +@Builder +@Getter +public class SkillRequestsWithUserDetailsViewModel { + private List skillRequests; + private List users; +} From 162ab3c1d33c3c5facbd8207bd03519dffe3260c Mon Sep 17 00:00:00 2001 From: yash raj Date: Tue, 27 Aug 2024 02:42:45 +0530 Subject: [PATCH 2/3] fix formatting --- .../com/RDS/skilltree/apis/SkillsApi.java | 5 +- .../services/SkillServiceImplementation.java | 89 ++++++++++--------- .../viewmodels/SkillRequestViewModel.java | 12 ++- ...SkillRequestsWithUserDetailsViewModel.java | 5 +- 4 files changed, 61 insertions(+), 50 deletions(-) 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; } From ffb33f6bb0a6ecccbf1f2c76ff40cc6bef3f5369 Mon Sep 17 00:00:00 2001 From: yash raj Date: Tue, 27 Aug 2024 02:47:58 +0530 Subject: [PATCH 3/3] fix method name --- .../services/SkillServiceImplementation.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) 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 80c69ad..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 @@ -35,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() @@ -46,7 +56,7 @@ public List getAll() { public SkillRequestsDto getAllRequests() { List skillRequests = userSkillRepository.findAll(); SkillRequestsWithUserDetailsViewModel skillRequestsWithUserDetails = - toSkillRequestsViewModel(skillRequests); + toSkillRequestsWithUserDetailsViewModel(skillRequests); return SkillRequestsDto.toDto( skillRequestsWithUserDetails.getSkillRequests(), skillRequestsWithUserDetails.getUsers()); @@ -56,22 +66,12 @@ public SkillRequestsDto getAllRequests() { public SkillRequestsDto getRequestsByStatus(UserSkillStatusEnum status) { List skillRequests = userSkillRepository.findByStatus(status); SkillRequestsWithUserDetailsViewModel skillRequestsWithUserDetails = - toSkillRequestsViewModel(skillRequests); + toSkillRequestsWithUserDetailsViewModel(skillRequests); 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() : ""; - - return UserViewModel.builder().id(id).name(firstName + ' ' + lastName).build(); - } - @Override public SkillViewModel create(CreateSkillViewModel skill) { if (skillRepository.existsByName(skill.getName())) { @@ -108,7 +108,8 @@ public GenericResponse approveRejectSkillRequest( return new GenericResponse<>("Skill {}", action.toString().toLowerCase()); } - private SkillRequestsWithUserDetailsViewModel toSkillRequestsViewModel(List skills) { + private SkillRequestsWithUserDetailsViewModel toSkillRequestsWithUserDetailsViewModel( + List skills) { // store all users data that are a part of this request Map userDetails = new HashMap<>();