Skip to content

Commit 9edb172

Browse files
authored
S2US3 (#65)
* Created FullProofDTO Added to controller and TalentProofService getTalentProofs method
1 parent 6954612 commit 9edb172

4 files changed

Lines changed: 110 additions & 8 deletions

File tree

src/main/java/com/provedcode/talent/controller/TalentProofController.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.provedcode.talent.controller;
22

33
import com.provedcode.talent.mapper.TalentProofMapper;
4+
import com.provedcode.talent.model.dto.FullProofDTO;
45
import com.provedcode.talent.model.dto.ProofDTO;
56
import com.provedcode.talent.service.TalentProofService;
67
import lombok.AllArgsConstructor;
78
import org.springframework.data.domain.Page;
8-
import org.springframework.web.bind.annotation.GetMapping;
9-
import org.springframework.web.bind.annotation.RequestMapping;
10-
import org.springframework.web.bind.annotation.RequestParam;
11-
import org.springframework.web.bind.annotation.RestController;
9+
import org.springframework.security.access.prepost.PreAuthorize;
10+
import org.springframework.security.core.Authentication;
11+
import org.springframework.web.bind.annotation.*;
1212

1313
import java.util.Optional;
1414

@@ -24,4 +24,15 @@ Page<ProofDTO> getAllProofs(@RequestParam(value = "page") Optional<Integer> page
2424
@RequestParam(value = "size") Optional<Integer> size) {
2525
return talentProofService.getAllProofsPage(page, size).map(talentProofMapper::toProofDTO);
2626
}
27+
28+
@GetMapping("/{talent-id}/proofs")
29+
@PreAuthorize("hasRole('TALENT')")
30+
FullProofDTO getTalentInformationWithProofs(Authentication authentication,
31+
@PathVariable("talent-id") Long talentId,
32+
@RequestParam(value = "page") Optional<Integer> page,
33+
@RequestParam(value = "size") Optional<Integer> size,
34+
@RequestParam(value = "direction") Optional<String> direction,
35+
@RequestParam(value = "sort", defaultValue = "created") String... sort) {
36+
return talentProofService.getTalentProofs(talentId, page, size, direction, authentication, sort);
37+
}
2738
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.provedcode.talent.model.dto;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import jakarta.validation.constraints.NotEmpty;
5+
import lombok.Builder;
6+
import org.springframework.data.domain.Page;
7+
8+
9+
@Builder
10+
public record FullProofDTO (
11+
Long id,
12+
@NotEmpty
13+
@JsonProperty("first_name")
14+
String firstName,
15+
@NotEmpty
16+
@JsonProperty("last_name")
17+
String lastName,
18+
String image,
19+
@NotEmpty
20+
String specialization,
21+
Page<ProofDTO> proofs
22+
) {
23+
}

src/main/java/com/provedcode/talent/repo/TalentProofRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
import org.springframework.data.domain.Pageable;
77
import org.springframework.data.jpa.repository.JpaRepository;
88

9+
import java.util.Optional;
10+
911
public interface TalentProofRepository extends JpaRepository<TalentProof, Long> {
12+
Page<TalentProof> findByTalentIdAndStatus(Long talentId, ProofStatus status, Pageable pageable);
13+
Page<TalentProof> findByTalentId(Long talentId, Pageable pageable);
1014
Page<TalentProof> findByStatus(ProofStatus status, Pageable pageable);
1115
}

src/main/java/com/provedcode/talent/service/TalentProofService.java

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@
22

33
import com.provedcode.config.PageProperties;
44
import com.provedcode.talent.model.ProofStatus;
5+
import com.provedcode.talent.model.dto.FullProofDTO;
6+
import com.provedcode.talent.model.dto.ProofDTO;
7+
import com.provedcode.talent.model.entity.Talent;
58
import com.provedcode.talent.model.entity.TalentProof;
69
import com.provedcode.talent.repo.TalentProofRepository;
10+
import com.provedcode.talent.repo.TalentRepository;
11+
import com.provedcode.user.model.entity.UserInfo;
12+
import com.provedcode.user.repo.UserInfoRepository;
713
import lombok.AllArgsConstructor;
14+
import lombok.extern.slf4j.Slf4j;
815
import org.springframework.data.domain.Page;
916
import org.springframework.data.domain.PageRequest;
17+
import org.springframework.data.domain.Sort;
18+
import org.springframework.http.HttpStatus;
19+
import org.springframework.security.core.Authentication;
1020
import org.springframework.stereotype.Service;
1121
import org.springframework.web.server.ResponseStatusException;
1222

@@ -16,8 +26,11 @@
1626

1727
@Service
1828
@AllArgsConstructor
29+
@Slf4j
1930
public class TalentProofService {
2031
TalentProofRepository talentProofRepository;
32+
TalentRepository talentRepository;
33+
UserInfoRepository userInfoRepository;
2134
PageProperties pageProperties;
2235

2336
public Page<TalentProof> getAllProofsPage(Optional<Integer> page, Optional<Integer> size) {
@@ -28,9 +41,60 @@ public Page<TalentProof> getAllProofsPage(Optional<Integer> page, Optional<Integ
2841
throw new ResponseStatusException(BAD_REQUEST, "'size' query parameter must be greater than or equal to 1");
2942
}
3043
return talentProofRepository.findByStatus(ProofStatus.PUBLISHED,
31-
PageRequest.of(page.orElse(
32-
pageProperties.defaultPageNum()),
33-
size.orElse(
34-
pageProperties.defaultPageSize())));
44+
PageRequest.of(page.orElse(
45+
pageProperties.defaultPageNum()),
46+
size.orElse(
47+
pageProperties.defaultPageSize())));
48+
}
49+
50+
public FullProofDTO getTalentProofs(Long talentId, Optional<Integer> page, Optional<Integer> size,
51+
Optional<String> direction, Authentication authentication, String... sortProperties) {
52+
Talent talent = talentRepository.findById(talentId)
53+
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Talent with id = %s not found".formatted(talentId)));
54+
UserInfo userInfo = userInfoRepository.findByLogin(authentication.getName())
55+
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Talent with id = %s not found".formatted(talentId)));
56+
Page<TalentProof> proofs;
57+
PageRequest pageRequest;
58+
String sortDirection = direction.orElseGet(Sort.DEFAULT_DIRECTION::name);
59+
60+
if (page.orElse(pageProperties.defaultPageNum()) < 0) {
61+
throw new ResponseStatusException(BAD_REQUEST, "'page' query parameter must be greater than or equal to 0");
62+
}
63+
if (size.orElse(pageProperties.defaultPageSize()) <= 0) {
64+
throw new ResponseStatusException(BAD_REQUEST, "'size' query parameter must be greater than or equal to 1");
65+
}
66+
if (!sortDirection.equalsIgnoreCase(Sort.Direction.ASC.name()) && !sortDirection.equalsIgnoreCase(Sort.Direction.DESC.name())) {
67+
throw new ResponseStatusException(BAD_REQUEST, "'direction' query param must be equals ASC or DESC");
68+
}
69+
70+
try {
71+
pageRequest = PageRequest.of(
72+
page.orElse(pageProperties.defaultPageNum()),
73+
size.orElse(pageProperties.defaultPageSize()),
74+
Sort.Direction.valueOf(sortDirection.toUpperCase()),
75+
sortProperties
76+
);
77+
if (!userInfo.getLogin().equals(authentication.getName())) {
78+
proofs = talentProofRepository.findByTalentIdAndStatus(talentId, ProofStatus.PUBLISHED, pageRequest);
79+
} else {
80+
proofs = talentProofRepository.findByTalentId(talentId, pageRequest);
81+
}
82+
} catch (RuntimeException exception) {
83+
throw new ResponseStatusException(BAD_REQUEST, exception.getMessage());
84+
}
85+
86+
return FullProofDTO.builder()
87+
.id(talent.getId())
88+
.image(talent.getImage())
89+
.firstName(talent.getFirstName())
90+
.lastName(talent.getLastName())
91+
.specialization(talent.getSpecialization())
92+
.proofs(proofs.map(i -> ProofDTO.builder()
93+
.id(i.getId())
94+
.created(i.getCreated().toString())
95+
.link(i.getLink())
96+
.text(i.getText())
97+
.status(i.getStatus()).build()))
98+
.build();
3599
}
36100
}

0 commit comments

Comments
 (0)