Skip to content

Commit

Permalink
add status filter to get all skill requests api (#152)
Browse files Browse the repository at this point in the history
* add status filter to get all skill requests api

* fix formatting

* fix method name
  • Loading branch information
yesyash authored Aug 30, 2024
1 parent eb5f8af commit 75b2484
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -36,7 +37,12 @@ public ResponseEntity<List<SkillViewModel>> getAll() {

@GetMapping("/requests")
@AuthorizedRoles({UserRoleEnum.SUPERUSER})
public ResponseEntity<SkillRequestsDto> getAllRequests() {
public ResponseEntity<SkillRequestsDto> getAllRequests(
@RequestParam(value = "status", required = false) UserSkillStatusEnum status) {
if (status != null) {
return ResponseEntity.ok(skillService.getRequestsByStatus(status));
}

return ResponseEntity.ok(skillService.getAllRequests());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public interface SkillService {

SkillRequestsDto getAllRequests();

SkillRequestsDto getRequestsByStatus(UserSkillStatusEnum status);

GenericResponse<String> approveRejectSkillRequest(
Integer skillId, String endorseId, UserSkillStatusEnum action);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<SkillViewModel> getAll() {
return skillRepository.findAll().stream()
Expand All @@ -47,60 +54,22 @@ public List<SkillViewModel> getAll() {

@Override
public SkillRequestsDto getAllRequests() {
List<UserSkills> pendingSkills = userSkillRepository.findByStatus(UserSkillStatusEnum.PENDING);

// store all users data that are a part of this request
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.getUserId();

// Get all endorsement for a specific skill and user Id
List<Endorsement> 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<UserSkills> 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<UserSkills> 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
Expand Down Expand Up @@ -139,6 +108,54 @@ public GenericResponse<String> approveRejectSkillRequest(
return new GenericResponse<>("Skill {}", action.toString().toLowerCase());
}

private SkillRequestsWithUserDetailsViewModel toSkillRequestsWithUserDetailsViewModel(
List<UserSkills> skills) {
// store all users data that are a part of this request
Map<String, UserViewModel> userDetails = new HashMap<>();

List<SkillRequestViewModel> 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<Endorsement> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,27 +13,35 @@ public class SkillRequestViewModel {
private Integer skillId;
private String skillName;
private String endorseId;
private UserSkillStatusEnum status;
private List<MinimalEndorsementViewModel> endorsements;

public SkillRequestViewModel(
Integer id, String name, String endorseId, List<MinimalEndorsementViewModel> endorsements) {
Integer id,
String name,
String endorseId,
List<MinimalEndorsementViewModel> 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<Endorsement> endorsements) {
Skill skill = userSkills.getSkill();
String userId = userSkills.getUserId();
UserSkillStatusEnum status = userSkills.getStatus();

return new SkillRequestViewModel(
skill.getId(),
skill.getName(),
userId,
endorsements.stream()
.map(MinimalEndorsementViewModel::toViewModel)
.collect(Collectors.toList()));
.collect(Collectors.toList()),
status);
}
}
Original file line number Diff line number Diff line change
@@ -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<SkillRequestViewModel> skillRequests;
private List<UserViewModel> users;
}

0 comments on commit 75b2484

Please sign in to comment.