Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
yesyash committed Aug 26, 2024
1 parent 86a6869 commit 162ab3c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ public ResponseEntity<List<SkillViewModel>> getAll() {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,17 +45,21 @@ public List<SkillViewModel> getAll() {
@Override
public SkillRequestsDto getAllRequests() {
List<UserSkills> 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<UserSkills> 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(
Expand Down Expand Up @@ -105,47 +108,51 @@ public GenericResponse<String> approveRejectSkillRequest(
return new GenericResponse<>("Skill {}", action.toString().toLowerCase());
}

private SkillRequestsWithUserDetailsViewModel toSkillRequestsViewModel(List<UserSkills> skills){
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(
endorsement -> {
String endorserId = endorsement.getEndorserId();

if (!userDetails.containsKey(endorserId)) {
RdsGetUserDetailsResDto endorserRdsDetails =
rdsService.getUserDetails(endorserId);
UserViewModel endorserDetails =
getUserModalFromRdsDetails(endorserId, endorserRdsDetails);
userDetails.put(endorserId, endorserDetails);
}
});
List<SkillRequestViewModel> 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<Endorsement> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -18,7 +17,11 @@ public class SkillRequestViewModel {
private List<MinimalEndorsementViewModel> endorsements;

public SkillRequestViewModel(
Integer id, String name, String endorseId, List<MinimalEndorsementViewModel> endorsements, UserSkillStatusEnum status) {
Integer id,
String name,
String endorseId,
List<MinimalEndorsementViewModel> endorsements,
UserSkillStatusEnum status) {
this.skillId = id;
this.skillName = name;
this.endorseId = endorseId;
Expand All @@ -38,6 +41,7 @@ public static SkillRequestViewModel toViewModel(
userId,
endorsements.stream()
.map(MinimalEndorsementViewModel::toViewModel)
.collect(Collectors.toList()), status);
.collect(Collectors.toList()),
status);
}
}
Original file line number Diff line number Diff line change
@@ -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<SkillRequestViewModel> skillRequests;
private List<UserViewModel> users;
private List<UserViewModel> users;
}

0 comments on commit 162ab3c

Please sign in to comment.