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
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.provedcode.talent.controller;

import com.provedcode.talent.model.dto.ProofSkillsDTO;
import com.provedcode.talent.model.dto.SkillsOnProofDTO;
import com.provedcode.talent.service.TalentSkillsService;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
Expand All @@ -25,4 +27,19 @@ void addSkillOnProof(@PathVariable("talent-id") long talentId,
Authentication authentication) {
talentSkillsService.addSkillsOnProof(talentId, proofId, skills, authentication);
}

@GetMapping("/{talent-id}/proofs/{proof-id}/skills")
SkillsOnProofDTO getAllSkillsOnProof(@PathVariable("talent-id") long talentId,
@PathVariable("proof-id") long proofId,
Authentication authentication) {
return talentSkillsService.getAllSkillsOnProof(talentId, proofId, authentication);
}
@PreAuthorize("hasRole('TALENT')")
@DeleteMapping("/{talent-id}/proofs/{proof-id}/skills/{skill-id}")
void deleteSkillOnProof(@PathVariable("talent-id") long talentId,
@PathVariable("proof-id") long proofId,
@PathVariable("skill-id") long skillId,
Authentication authentication) {
talentSkillsService.deleteSkillOnProof(talentId, proofId, skillId, authentication);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.provedcode.talent.model.dto;

import com.provedcode.talent.model.entity.Skills;
import lombok.Builder;

import java.util.Set;

@Builder
public record SkillsOnProofDTO(
Set<Skills> skills
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.provedcode.talent.model.ProofStatus;
import com.provedcode.talent.model.dto.ProofSkillsDTO;
import com.provedcode.talent.model.dto.SkillsOnProofDTO;
import com.provedcode.talent.model.entity.Skills;
import com.provedcode.talent.model.entity.Talent;
import com.provedcode.talent.model.entity.TalentProof;
import com.provedcode.talent.repo.SkillsRepository;
import com.provedcode.talent.repo.TalentProofRepository;
Expand All @@ -16,7 +18,6 @@
import org.springframework.web.server.ResponseStatusException;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;

Expand Down Expand Up @@ -60,5 +61,57 @@ public void addSkillsOnProof(long talentId, long proofId, ProofSkillsDTO skills,
talentProofRepository.save(talentProof);
}

@Transactional(readOnly = true)
public SkillsOnProofDTO getAllSkillsOnProof(long talentId, long proofId, Authentication authentication) {
TalentProof talentProof = talentProofRepository.findById(proofId)
.orElseThrow(() -> new ResponseStatusException(NOT_FOUND,
"proof with id = %s not found".formatted(proofId)));
Talent talent = talentRepository.findById(talentId)
.orElseThrow(() -> new ResponseStatusException(NOT_FOUND,
"talent with id = %s not found".formatted(talentId)));
if (!talent.getId().equals(talentProof.getTalent().getId())) {
throw new ResponseStatusException(BAD_REQUEST,
"talentId with id = %s and proofId with id = %s do not match".formatted(talentId, proofId));
}

if (talentProof.getStatus().equals(ProofStatus.PUBLISHED)) {
return SkillsOnProofDTO.builder().skills(talentProof.getSkills()).build();
} else if (authentication != null) {
UserInfo userInfo = userInfoRepository.findByLogin(authentication.getName())
.orElseThrow(() -> new ResponseStatusException(NOT_FOUND));
if (userInfo.getTalent().getId().equals(talentProof.getTalent().getId())) {
return SkillsOnProofDTO.builder().skills(talentProof.getSkills()).build();
} else {
throw new ResponseStatusException(FORBIDDEN, "you can't see proofs in DRAFT and HIDDEN status");
}
} else {
throw new ResponseStatusException(FORBIDDEN, "you can't see proofs in DRAFT and HIDDEN status");
}
}

public void deleteSkillOnProof(long talentId, long proofId, long skillId, Authentication authentication) {
Talent talent = talentRepository.findById(talentId)
.orElseThrow(() -> new ResponseStatusException(NOT_FOUND,
"talent with id = %s not found".formatted(talentId)));
UserInfo userInfo = userInfoRepository.findByLogin(authentication.getName())
.orElseThrow(() -> new ResponseStatusException(NOT_FOUND));
TalentProof talentProof = talentProofRepository.findById(proofId)
.orElseThrow(() -> new ResponseStatusException(NOT_FOUND, "proof with id = %s not found".formatted(proofId)));
if (!talentProof.getStatus().equals(ProofStatus.DRAFT)) {
throw new ResponseStatusException(CONFLICT, "proof status must be DRAFT");
}
if (!talent.getId().equals(talentProof.getTalent().getId())) {
throw new ResponseStatusException(BAD_REQUEST,
"talentId with id = %s and proofId with id = %s do not match".formatted(talentId, proofId));
}
isValidUserEditTalent.accept(talentId, userInfo);
Skills skills = skillsRepository.findById(skillId)
.orElseThrow(() -> new ResponseStatusException(NOT_FOUND,
"skill with id = %s not found".formatted(skillId)));
if (!talentProof.getSkills().contains(skills)) {
throw new ResponseStatusException(NOT_FOUND,
"you dont have skill with id = %s on proof with id = %s".formatted(skillId, proofId));
}
talentProof.getSkills().remove(skills);
}
}
19 changes: 19 additions & 0 deletions src/main/resources/db/changelog/changeset/V4/data-V4.1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--liquibase formatted sql
--changeset dennis:3
-- Skill
INSERT INTO talent_skill (proof_id, skill_id)
VALUES (1, 1);
INSERT INTO talent_skill (proof_id, skill_id)
VALUES (1, 2);
INSERT INTO talent_skill (proof_id, skill_id)
VALUES (1, 3);
INSERT INTO talent_skill (proof_id, skill_id)
VALUES (1, 4);
INSERT INTO talent_skill (proof_id, skill_id)
VALUES (4, 1);
INSERT INTO talent_skill (proof_id, skill_id)
VALUES (5, 2);
INSERT INTO talent_skill (proof_id, skill_id)
VALUES (6, 3);


2 changes: 2 additions & 0 deletions src/main/resources/db/changelog/db.changelog-master.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ databaseChangeLog:
file: db/changelog/changeset/V4/schema-V4.sql
- include:
file: db/changelog/changeset/V4/data-V4.sql
- include:
file: db/changelog/changeset/V4/data-V4.1.sql