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,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;

Expand All @@ -24,4 +24,15 @@ Page<ProofDTO> getAllProofs(@RequestParam(value = "page") Optional<Integer> page
@RequestParam(value = "size") Optional<Integer> 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<Integer> page,
@RequestParam(value = "size") Optional<Integer> size,
@RequestParam(value = "direction") Optional<String> direction,
@RequestParam(value = "sort", defaultValue = "created") String... sort) {
return talentProofService.getTalentProofs(talentId, page, size, direction, authentication, sort);
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/provedcode/talent/model/dto/FullProofDTO.java
Original file line number Diff line number Diff line change
@@ -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<ProofDTO> proofs
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<TalentProof, Long> {
Page<TalentProof> findByTalentIdAndStatus(Long talentId, ProofStatus status, Pageable pageable);
Page<TalentProof> findByTalentId(Long talentId, Pageable pageable);
Page<TalentProof> findByStatus(ProofStatus status, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -16,8 +26,11 @@

@Service
@AllArgsConstructor
@Slf4j
public class TalentProofService {
TalentProofRepository talentProofRepository;
TalentRepository talentRepository;
UserInfoRepository userInfoRepository;
PageProperties pageProperties;

public Page<TalentProof> getAllProofsPage(Optional<Integer> page, Optional<Integer> size) {
Expand All @@ -28,9 +41,60 @@ public Page<TalentProof> getAllProofsPage(Optional<Integer> page, Optional<Integ
throw new ResponseStatusException(BAD_REQUEST, "'size' query parameter must be greater than or equal to 1");
}
return talentProofRepository.findByStatus(ProofStatus.PUBLISHED,
PageRequest.of(page.orElse(
pageProperties.defaultPageNum()),
size.orElse(
pageProperties.defaultPageSize())));
PageRequest.of(page.orElse(
pageProperties.defaultPageNum()),
size.orElse(
pageProperties.defaultPageSize())));
}

public FullProofDTO getTalentProofs(Long talentId, Optional<Integer> page, Optional<Integer> size,
Optional<String> 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<TalentProof> 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();
}
}