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,5 +1,6 @@
package com.provedcode.handlers;

import com.provedcode.handlers.dto.ErrorDTO;
import jakarta.validation.ConstraintViolationException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -65,7 +66,7 @@ private ResponseEntity<?> responseStatusExceptionHandler(ResponseStatusException
@ResponseStatus(HttpStatus.BAD_REQUEST)
private ResponseEntity<?> responseStatusExceptionHandler(ConstraintViolationException exception) {
ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, exception.getMessage(), exception.toString());
return new ResponseEntity<>(apiError, new HttpHeaders(), apiError.getStatus());
return new ResponseEntity<>(new ErrorDTO(apiError.getMessage()), new HttpHeaders(), apiError.getStatus());
}

// @ExceptionHandler({ Exception.class })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,11 @@ void deleteSkillFromTalent(@PathVariable("talent-id") long talentId,
talentService.deleteSkillFromTalent(talentId, skillId, authentication);
}

@GetMapping("v4/talents")
Page<ShortTalentDTO> getFilteredBySkillsTalents(@RequestParam(value = "page", defaultValue = "0") @PositiveOrZero Integer page,
@RequestParam(value = "size", defaultValue = "5") @Min(1) @Max(1000) Integer size,
@RequestParam(value = "filter-by", required = false) String... filterBy) {
return talentService.getFilteredBySkillsTalentsPage(page, size, filterBy).map(talentMapper::talentToShortTalentDTO);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ ProofDTO editProof(Authentication authentication,
@DeleteProofApiDoc
@DeleteMapping("/{talent-id}/proofs/{proof-id}")
@PreAuthorize("hasRole('TALENT')")
StatusDTO deleteProof(@PathVariable(value = "talent-id") long talentId,
void deleteProof(@PathVariable(value = "talent-id") long talentId,
@PathVariable(value = "proof-id") long proofId,
Authentication authentication) {
return talentProofService.deleteProofById(talentId, proofId, authentication);
talentProofService.deleteProofById(talentId, proofId, authentication);
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/provedcode/talent/repo/TalentRepository.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
package com.provedcode.talent.repo;

import com.provedcode.talent.model.entity.Talent;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface TalentRepository extends JpaRepository<Talent, Long> {
// @Query("select t from Talent t left join t.skills skills where upper(skills.skill) like upper(concat('%', ?2, '%'))")
// Page<Talent> findBySkills_SkillContainsIgnoreCase(Pageable pageable, String... skill);
// @Query("SELECT t FROM Talent t LEFT JOIN t.skills skills WHERE UPPER(skills.skill) LIKE UPPER(CONCAT('%', :skills, '%'))") // if by-symbol contains
// Page<Talent> findBySkills_SkillContainsIgnoreCase(Pageable pageable, @Param("skills") String... skills);
@Query("SELECT t FROM Talent t INNER JOIN t.skills s WHERE UPPER(s.skill) IN (:filter_by)") //if only contains all
Page<Talent> findBySkills_SkillsInIgnoreCase(Pageable pageable, @Param("filter_by") List<String> skills);
// @Query("select t from Talent t inner join t.skills skills where upper(skills.skill) like upper(?1)")
// Page<Talent> findBySkillsLikeIgnoreCase(String skill, Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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.entity.Skills;
import com.provedcode.talent.model.entity.Talent;
import com.provedcode.talent.model.entity.TalentProof;
import com.provedcode.talent.model.request.AddProof;
Expand All @@ -30,8 +31,8 @@
import java.net.URI;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;

import static org.springframework.http.HttpStatus.FORBIDDEN;
import static org.springframework.http.HttpStatus.NOT_FOUND;
Expand Down Expand Up @@ -159,14 +160,22 @@ public TalentProof editTalentProof(long talentId, long proofId, ProofDTO proof,
return talentProofRepository.save(oldProof);
}

public StatusDTO deleteProofById(long talentId, long proofId, Authentication authentication) {
public void deleteProofById(long talentId, long proofId, Authentication authentication) {
Optional<Talent> talent = talentRepository.findById(talentId);
Optional<TalentProof> talentProof = talentProofRepository.findById(proofId);
Optional<UserInfo> userInfo = userInfoRepository.findByLogin(authentication.getName());

validateTalentForCompliance.userAndProofVerification(talent, talentProof, userInfo, talentId, proofId);

talent.get().getTalentProofs().remove(talentProof.get());
return new StatusDTO("deleted");
Talent updatableTalent = talent.get();
List<Skills> talentSkills = updatableTalent.getTalentProofs().stream()
.flatMap(proof -> proof.getSkills().stream()).collect(Collectors.toCollection(ArrayList::new));
log.info("talent-skills = {}", talentSkills.stream().map(i -> i.getSkill()).toList());
List<Skills> skillsOnProof = talentProof.get().getSkills().stream().toList();
talentSkills.removeAll(skillsOnProof);
Set<Skills> newTalentSkills = new HashSet<>(talentSkills);
updatableTalent.setSkills(newTalentSkills);
log.info("new talent-skills = {}", newTalentSkills.stream().map(Skills::getSkill).toList());
updatableTalent.getTalentProofs().remove(talentProof.get());
}
}
14 changes: 13 additions & 1 deletion src/main/java/com/provedcode/talent/service/TalentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import com.provedcode.user.model.entity.UserInfo;
import com.provedcode.user.repo.AuthorityRepository;
import com.provedcode.user.repo.UserInfoRepository;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.PositiveOrZero;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
Expand All @@ -21,7 +24,7 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;

import java.util.HashSet;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -194,4 +197,13 @@ public void deleteSkillFromTalent(long talentId, long skillId, Authentication au
"Skill with id = %d not found".formatted(skillId));
}
}

@Transactional(readOnly = true)
public Page<Talent> getFilteredBySkillsTalentsPage(@PositiveOrZero Integer page,
@Min(1) @Max(1000) Integer size,
String... filterBy) {
return filterBy != null ?
talentRepository.findBySkills_SkillsInIgnoreCase(PageRequest.of(page, size), Arrays.stream(filterBy).map(String::toUpperCase).toList())
: talentRepository.findAll(PageRequest.of(page, size));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
description = "As a talent I want to have an opportunity to delete my personal proofs")
@ApiResponses(value = {
@ApiResponse(responseCode = "200",
description = "SUCCESS",
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = StatusDTO.class))),
description = "SUCCESS"),
@ApiResponse(responseCode = "404",
description = "NOT FOUND",
content = @Content),
Expand Down
6 changes: 1 addition & 5 deletions src/main/resources/db/changelog/changeset/V4/data-V4.1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,4 @@ INSERT INTO proof_skill (proof_id, skill_id)
VALUES (5, 2);
INSERT INTO proof_skill (proof_id, skill_id)
VALUES (6, 3);
-- Skill in talent
INSERT INTO talent_skill (talent_id, skill_id)
VALUES(1, 1);
INSERT INTO talent_skill (talent_id, skill_id)
VALUES(1, 2);

25 changes: 25 additions & 0 deletions src/main/resources/db/changelog/changeset/V4/data-V4.2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--liquibase formatted sql
--changeset mykhailo:3

-- Skill on proof


-- Skill on talent
INSERT INTO talent_skill (talent_id, skill_id)
VALUES(1, 1);
INSERT INTO talent_skill (talent_id, skill_id)
VALUES(1, 2);
INSERT INTO talent_skill (talent_id, skill_id)
VALUES(5, 1);
INSERT INTO talent_skill (talent_id, skill_id)
VALUES(5, 2);
INSERT INTO talent_skill (talent_id, skill_id)
VALUES(5, 3);

-- Skills on proof
INSERT INTO proof_skill (proof_id, skill_id)
VALUES (13, 1);
INSERT INTO proof_skill (proof_id, skill_id)
VALUES (13, 2);
INSERT INTO proof_skill (proof_id, skill_id)
VALUES (13, 3);
Loading