diff --git a/src/main/java/com/provedcode/talent/controller/TalentProofController.java b/src/main/java/com/provedcode/talent/controller/TalentProofController.java index f9a2101..b104448 100644 --- a/src/main/java/com/provedcode/talent/controller/TalentProofController.java +++ b/src/main/java/com/provedcode/talent/controller/TalentProofController.java @@ -1,14 +1,14 @@ package com.provedcode.talent.controller; import com.provedcode.talent.mapper.TalentProofMapper; +import com.provedcode.talent.model.dto.FullProofDTO; import com.provedcode.talent.model.dto.ProofDTO; import com.provedcode.talent.service.TalentProofService; import lombok.AllArgsConstructor; import org.springframework.data.domain.Page; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.core.Authentication; +import org.springframework.web.bind.annotation.*; import java.util.Optional; @@ -24,4 +24,15 @@ Page getAllProofs(@RequestParam(value = "page") Optional page @RequestParam(value = "size") Optional size) { return talentProofService.getAllProofsPage(page, size).map(talentProofMapper::toProofDTO); } + + @GetMapping("/{talent-id}/proofs") + @PreAuthorize("hasRole('TALENT')") + FullProofDTO getTalentInformationWithProofs(Authentication authentication, + @PathVariable("talent-id") Long talentId, + @RequestParam(value = "page") Optional page, + @RequestParam(value = "size") Optional size, + @RequestParam(value = "direction") Optional direction, + @RequestParam(value = "sort", defaultValue = "created") String... sort) { + return talentProofService.getTalentProofs(talentId, page, size, direction, authentication, sort); + } } diff --git a/src/main/java/com/provedcode/talent/model/dto/FullProofDTO.java b/src/main/java/com/provedcode/talent/model/dto/FullProofDTO.java new file mode 100644 index 0000000..ddb981d --- /dev/null +++ b/src/main/java/com/provedcode/talent/model/dto/FullProofDTO.java @@ -0,0 +1,23 @@ +package com.provedcode.talent.model.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.validation.constraints.NotEmpty; +import lombok.Builder; +import org.springframework.data.domain.Page; + + +@Builder +public record FullProofDTO ( + Long id, + @NotEmpty + @JsonProperty("first_name") + String firstName, + @NotEmpty + @JsonProperty("last_name") + String lastName, + String image, + @NotEmpty + String specialization, + Page proofs +) { +} diff --git a/src/main/java/com/provedcode/talent/repo/TalentProofRepository.java b/src/main/java/com/provedcode/talent/repo/TalentProofRepository.java index 8db70e6..8624c8d 100644 --- a/src/main/java/com/provedcode/talent/repo/TalentProofRepository.java +++ b/src/main/java/com/provedcode/talent/repo/TalentProofRepository.java @@ -6,6 +6,10 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.Optional; + public interface TalentProofRepository extends JpaRepository { + Page findByTalentIdAndStatus(Long talentId, ProofStatus status, Pageable pageable); + Page findByTalentId(Long talentId, Pageable pageable); Page findByStatus(ProofStatus status, Pageable pageable); } \ No newline at end of file diff --git a/src/main/java/com/provedcode/talent/service/TalentProofService.java b/src/main/java/com/provedcode/talent/service/TalentProofService.java index a4d817a..c81f4c0 100644 --- a/src/main/java/com/provedcode/talent/service/TalentProofService.java +++ b/src/main/java/com/provedcode/talent/service/TalentProofService.java @@ -2,11 +2,21 @@ import com.provedcode.config.PageProperties; import com.provedcode.talent.model.ProofStatus; +import com.provedcode.talent.model.dto.FullProofDTO; +import com.provedcode.talent.model.dto.ProofDTO; +import com.provedcode.talent.model.entity.Talent; import com.provedcode.talent.model.entity.TalentProof; import com.provedcode.talent.repo.TalentProofRepository; +import com.provedcode.talent.repo.TalentRepository; +import com.provedcode.user.model.entity.UserInfo; +import com.provedcode.user.repo.UserInfoRepository; import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; +import org.springframework.http.HttpStatus; +import org.springframework.security.core.Authentication; import org.springframework.stereotype.Service; import org.springframework.web.server.ResponseStatusException; @@ -16,8 +26,11 @@ @Service @AllArgsConstructor +@Slf4j public class TalentProofService { TalentProofRepository talentProofRepository; + TalentRepository talentRepository; + UserInfoRepository userInfoRepository; PageProperties pageProperties; public Page getAllProofsPage(Optional page, Optional size) { @@ -28,9 +41,60 @@ public Page getAllProofsPage(Optional page, Optional page, Optional size, + Optional direction, Authentication authentication, String... sortProperties) { + Talent talent = talentRepository.findById(talentId) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Talent with id = %s not found".formatted(talentId))); + UserInfo userInfo = userInfoRepository.findByLogin(authentication.getName()) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Talent with id = %s not found".formatted(talentId))); + Page proofs; + PageRequest pageRequest; + String sortDirection = direction.orElseGet(Sort.DEFAULT_DIRECTION::name); + + if (page.orElse(pageProperties.defaultPageNum()) < 0) { + throw new ResponseStatusException(BAD_REQUEST, "'page' query parameter must be greater than or equal to 0"); + } + if (size.orElse(pageProperties.defaultPageSize()) <= 0) { + throw new ResponseStatusException(BAD_REQUEST, "'size' query parameter must be greater than or equal to 1"); + } + if (!sortDirection.equalsIgnoreCase(Sort.Direction.ASC.name()) && !sortDirection.equalsIgnoreCase(Sort.Direction.DESC.name())) { + throw new ResponseStatusException(BAD_REQUEST, "'direction' query param must be equals ASC or DESC"); + } + + try { + pageRequest = PageRequest.of( + page.orElse(pageProperties.defaultPageNum()), + size.orElse(pageProperties.defaultPageSize()), + Sort.Direction.valueOf(sortDirection.toUpperCase()), + sortProperties + ); + if (!userInfo.getLogin().equals(authentication.getName())) { + proofs = talentProofRepository.findByTalentIdAndStatus(talentId, ProofStatus.PUBLISHED, pageRequest); + } else { + proofs = talentProofRepository.findByTalentId(talentId, pageRequest); + } + } catch (RuntimeException exception) { + throw new ResponseStatusException(BAD_REQUEST, exception.getMessage()); + } + + return FullProofDTO.builder() + .id(talent.getId()) + .image(talent.getImage()) + .firstName(talent.getFirstName()) + .lastName(talent.getLastName()) + .specialization(talent.getSpecialization()) + .proofs(proofs.map(i -> ProofDTO.builder() + .id(i.getId()) + .created(i.getCreated().toString()) + .link(i.getLink()) + .text(i.getText()) + .status(i.getStatus()).build())) + .build(); } }