Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add status filter to get all skill requests api #152

Merged
merged 3 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Down Expand Up @@ -47,50 +44,22 @@ public List<SkillViewModel> getAll() {

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

// 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 SkillRequestsDto.toDto(
skillRequestsWithUserDetails.getSkillRequests(), skillRequestsWithUserDetails.getUsers());
}

return SkillRequestViewModel.toViewModel(skill, endorsements);
})
.toList();
@Override
public SkillRequestsDto getRequestsByStatus(UserSkillStatusEnum status) {
List<UserSkills> 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(
Expand Down Expand Up @@ -139,6 +108,53 @@ public GenericResponse<String> approveRejectSkillRequest(
return new GenericResponse<>("Skill {}", action.toString().toLowerCase());
}

private SkillRequestsWithUserDetailsViewModel toSkillRequestsViewModel(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(
iamitprakash marked this conversation as resolved.
Show resolved Hide resolved
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;
}
Loading