22
33import com .provedcode .config .PageProperties ;
44import 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 ;
58import com .provedcode .talent .model .entity .TalentProof ;
69import 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 ;
713import lombok .AllArgsConstructor ;
14+ import lombok .extern .slf4j .Slf4j ;
815import org .springframework .data .domain .Page ;
916import 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 ;
1020import org .springframework .stereotype .Service ;
1121import org .springframework .web .server .ResponseStatusException ;
1222
1626
1727@ Service
1828@ AllArgsConstructor
29+ @ Slf4j
1930public 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