Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 7 additions & 7 deletions src/main/java/com/provedcode/kudos/service/KudosService.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ public KudosAmountWithSponsor getProofKudos(long proofId, Authentication authent
.collect(Collectors.toMap(
Kudos::getAmount,
proof -> proof.getSponsor() != null
? sponsorMapper.toDto(proof.getSponsor())
: SponsorDTO.builder().build(),
(prev, next) -> next,
HashMap::new));
? sponsorMapper.toDto(proof.getSponsor())
: SponsorDTO.builder().build(),
(prev, next) -> next,
HashMap::new));
skillsMap.put(skill, kudosFromSponsor);
});
return KudosAmountWithSponsor.builder()
Expand Down Expand Up @@ -127,9 +127,9 @@ public void addKudosToProof(long proofId, SetAmountKudos amountOfKudoses, Authen
if (sponsor.getAmountKudos() < obtainedAmount) {
throw new ResponseStatusException(FORBIDDEN, "The sponsor cannot give more kudos than he has");
}
if (obtainedAmount % talentProof.getProofSkills().size() != 0) {
throw new ResponseStatusException(BAD_REQUEST,
"Sponsor cannot add amount of kudos that multiple of amount of skills");
Long modula = obtainedAmount % talentProof.getProofSkills().size();
if (modula != 0) {
obtainedAmount -= modula;
}
sponsor.setAmountKudos(sponsor.getAmountKudos() - obtainedAmount);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.provedcode.talent.model.dto.FullTalentDTO;
import com.provedcode.talent.model.dto.ShortTalentDTO;
import com.provedcode.talent.model.dto.SkillIdDTO;
import com.provedcode.talent.model.dto.StatisticsDTO;
import com.provedcode.talent.model.request.EditTalent;
import com.provedcode.talent.service.TalentService;
import com.provedcode.user.model.dto.SessionInfoDTO;
Expand Down Expand Up @@ -90,4 +91,10 @@ Page<ShortTalentDTO> getFilteredBySkillsTalents(@RequestParam(value = "page", de
return talentService.getFilteredBySkillsTalentsPage(page, size, filterBy).map(talentMapper::talentToShortTalentDTO);
}

@PreAuthorize("hasRole('TALENT')")
@GetMapping("v5/talents/{talent-id}/statistics")
StatisticsDTO getStatisticsForTalent(@PathVariable("talent-id") long talentId,
Authentication authentication) {
return talentService.getStatisticsForTalent(talentId, authentication);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.provedcode.talent.mapper.TalentProofMapper;
import com.provedcode.talent.model.dto.FullProofDTO;
import com.provedcode.talent.model.dto.ProofDTO;
import com.provedcode.talent.model.dto.StatusDTO;
import com.provedcode.talent.model.request.AddProof;
import com.provedcode.talent.service.TalentProofService;
import com.provedcode.util.annotations.doc.controller.proof.*;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/provedcode/talent/model/dto/StatisticsDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.provedcode.talent.model.dto;

import lombok.Builder;

import java.util.Map;

@Builder
public record StatisticsDTO(
Long allKudosOnTalent,
Map<String, Long> skillWithLargestNumberOfKudos,
Map<ProofDTO, Long> proofWithLargestNumberOfKudos
) {
}
77 changes: 73 additions & 4 deletions src/main/java/com/provedcode/talent/service/TalentService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.provedcode.talent.service;

import com.provedcode.config.PageProperties;
import com.provedcode.kudos.model.entity.Kudos;
import com.provedcode.talent.mapper.TalentProofMapper;
import com.provedcode.talent.model.dto.ProofDTO;
import com.provedcode.talent.model.dto.SkillDTO;
import com.provedcode.talent.model.dto.SkillIdDTO;
import com.provedcode.talent.model.dto.StatisticsDTO;
import com.provedcode.talent.model.entity.*;
import com.provedcode.talent.model.request.EditTalent;
import com.provedcode.talent.repo.SkillsRepository;
Expand All @@ -24,10 +29,7 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

import static org.springframework.http.HttpStatus.*;
Expand All @@ -44,6 +46,7 @@ public class TalentService {
PageProperties pageProperties;
ValidateTalentForCompliance validateTalentForCompliance;
SkillsRepository skillsRepository;
TalentProofMapper talentProofMapper;

@Transactional(readOnly = true)
public Page<Talent> getTalentsPage(Integer page, Integer size) {
Expand Down Expand Up @@ -214,4 +217,70 @@ public Page<Talent> getFilteredBySkillsTalentsPage(@PositiveOrZero Integer page,
talentRepository.findBySkills_SkillsInIgnoreCase(PageRequest.of(page, size), Arrays.stream(filterBy).map(String::toUpperCase).toList())
: talentRepository.findAll(PageRequest.of(page, size));
}

public StatisticsDTO getStatisticsForTalent(long talentId, Authentication authentication) {
Optional<Talent> talent = talentRepository.findById(talentId);
Optional<UserInfo> userInfo = userInfoRepository.findByLogin(authentication.getName());
validateTalentForCompliance.userVerification(talent, userInfo, talentId);
Talent talentObject = talent.get();
return StatisticsDTO.builder()
.allKudosOnTalent(getAllKudosOnTalent(talentObject))
.skillWithLargestNumberOfKudos(getSkillWithLargestNumberOfKudos(talentObject))
.proofWithLargestNumberOfKudos(getProofWithLargestNumberOfKudos(talentObject))
.build();
}

public Long getAllKudosOnTalent(Talent talent) {
return talent.getTalentProofs()
.stream()
.flatMap(x -> x.getProofSkills().stream())
.flatMap(y -> y.getKudos().stream())
.mapToLong(q -> q.getAmount())
.sum();
}

public Map<String, Long> getSkillWithLargestNumberOfKudos(Talent talent) {
Map<String, Long> numberKudosOnSkill = new HashMap<>();
for (TalentProof talentProof : talent.getTalentProofs()) {
for (ProofSkill proofSkill : talentProof.getProofSkills()) {
for (Kudos kudos : proofSkill.getKudos()) {
if (numberKudosOnSkill.containsKey(proofSkill.getSkill().getSkill())) {
Long amountKudosOnSkill = numberKudosOnSkill.get(proofSkill.getSkill().getSkill());
numberKudosOnSkill.remove(proofSkill.getSkill().getSkill());
numberKudosOnSkill.put(proofSkill.getSkill().getSkill(), amountKudosOnSkill + kudos.getAmount());
} else {
numberKudosOnSkill.put(proofSkill.getSkill().getSkill(), kudos.getAmount());
}
}
}
}
Long max = Collections.max(numberKudosOnSkill.values());
Map<String, Long> result = new HashMap<>();
for (Map.Entry<String, Long> entry : numberKudosOnSkill.entrySet()) {
if (entry.getValue().equals(max)) {
result.put(entry.getKey(), entry.getValue());
}
}

return result;
}
Comment on lines +242 to +266
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use lambdas for best performanse and view


public Map<ProofDTO, Long> getProofWithLargestNumberOfKudos(Talent talent) {
Map<ProofDTO, Long> result = new HashMap<>();
Long maxForResult = 0L;
for (TalentProof talentProof : talent.getTalentProofs()) {
Long amountKudosOnSkills = 0L;
for (ProofSkill proofSkill : talentProof.getProofSkills()) {
for (Kudos kudos : proofSkill.getKudos()) {
amountKudosOnSkills += kudos.getAmount();
}
}
if (amountKudosOnSkills > maxForResult) {
maxForResult = amountKudosOnSkills;
result.clear();
result.put(talentProofMapper.toProofDTO(talentProof), amountKudosOnSkills);
}
}
Comment on lines +271 to +283
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same.

return result;
}
}