diff --git a/src/main/java/com/provedcode/talent/controller/TalentSkillsController.java b/src/main/java/com/provedcode/talent/controller/TalentSkillsController.java index 35d9acb..bc0a57e 100644 --- a/src/main/java/com/provedcode/talent/controller/TalentSkillsController.java +++ b/src/main/java/com/provedcode/talent/controller/TalentSkillsController.java @@ -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.*; @@ -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); + } } diff --git a/src/main/java/com/provedcode/talent/model/dto/SkillsOnProofDTO.java b/src/main/java/com/provedcode/talent/model/dto/SkillsOnProofDTO.java new file mode 100644 index 0000000..f70ff5e --- /dev/null +++ b/src/main/java/com/provedcode/talent/model/dto/SkillsOnProofDTO.java @@ -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 +) { +} diff --git a/src/main/java/com/provedcode/talent/service/TalentSkillsService.java b/src/main/java/com/provedcode/talent/service/TalentSkillsService.java index 7fdd351..0fed22f 100644 --- a/src/main/java/com/provedcode/talent/service/TalentSkillsService.java +++ b/src/main/java/com/provedcode/talent/service/TalentSkillsService.java @@ -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; @@ -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; @@ -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); + } } diff --git a/src/main/resources/db/changelog/changeset/V4/data-V4.1.sql b/src/main/resources/db/changelog/changeset/V4/data-V4.1.sql new file mode 100644 index 0000000..9261f0d --- /dev/null +++ b/src/main/resources/db/changelog/changeset/V4/data-V4.1.sql @@ -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); + + diff --git a/src/main/resources/db/changelog/db.changelog-master.yaml b/src/main/resources/db/changelog/db.changelog-master.yaml index 8c5dc7c..102ca3c 100644 --- a/src/main/resources/db/changelog/db.changelog-master.yaml +++ b/src/main/resources/db/changelog/db.changelog-master.yaml @@ -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