22
33import com .provedcode .config .PageProperties ;
44import com .provedcode .talent .model .ProofStatus ;
5+ import com .provedcode .talent .model .dto .AddProofDTO ;
56import com .provedcode .talent .model .dto .FullProofDTO ;
67import com .provedcode .talent .model .dto .ProofDTO ;
78import com .provedcode .talent .model .entity .Talent ;
89import com .provedcode .talent .model .entity .TalentProof ;
910import com .provedcode .talent .repo .TalentProofRepository ;
1011import com .provedcode .talent .repo .TalentRepository ;
12+ import com .provedcode .user .model .dto .SessionInfoDTO ;
1113import com .provedcode .user .model .entity .UserInfo ;
1214import com .provedcode .user .repo .UserInfoRepository ;
15+ import com .provedcode .utill .ValidateTalentForCompliance ;
1316import lombok .AllArgsConstructor ;
1417import lombok .extern .slf4j .Slf4j ;
1518import org .springframework .data .domain .Page ;
1619import org .springframework .data .domain .PageRequest ;
1720import org .springframework .data .domain .Sort ;
1821import org .springframework .http .HttpStatus ;
22+ import org .springframework .http .ResponseEntity ;
1923import org .springframework .security .core .Authentication ;
2024import org .springframework .stereotype .Service ;
25+ import org .springframework .transaction .annotation .Transactional ;
2126import org .springframework .web .server .ResponseStatusException ;
27+ import org .springframework .web .servlet .support .ServletUriComponentsBuilder ;
2228
29+ import java .net .URI ;
30+ import java .time .LocalDateTime ;
2331import java .util .Optional ;
2432
2533import static org .springframework .http .HttpStatus .BAD_REQUEST ;
34+ import static org .springframework .http .HttpStatus .NOT_IMPLEMENTED ;
2635
2736@ Service
2837@ AllArgsConstructor
@@ -32,27 +41,89 @@ public class TalentProofService {
3241 TalentRepository talentRepository ;
3342 UserInfoRepository userInfoRepository ;
3443 PageProperties pageProperties ;
44+ ValidateTalentForCompliance validateTalentForCompliance ;
3545
36- public Page <TalentProof > getAllProofsPage (Optional <Integer > page , Optional <Integer > size ) {
46+ public Page <TalentProof > getAllProofsPage (Optional <Integer > page , Optional <Integer > size ,
47+ Optional <String > orderBy ) {
3748 if (page .orElse (pageProperties .defaultPageNum ()) < 0 ) {
3849 throw new ResponseStatusException (BAD_REQUEST , "'page' query parameter must be greater than or equal to 0" );
3950 }
4051 if (size .orElse (pageProperties .defaultPageSize ()) <= 0 ) {
4152 throw new ResponseStatusException (BAD_REQUEST , "'size' query parameter must be greater than or equal to 1" );
4253 }
54+
55+ if (orderBy .isPresent ()) {
56+ if (!orderBy .get ().equalsIgnoreCase (Sort .Direction .ASC .name ()) &&
57+ !orderBy .get ().equalsIgnoreCase (Sort .Direction .DESC .name ())) {
58+ throw new ResponseStatusException (BAD_REQUEST , "'orderBy' query parameter must be ASC or DESC" );
59+ }
60+ Sort sort =
61+ orderBy .get ().equalsIgnoreCase (Sort .Direction .ASC .name ()) ? Sort .by (pageProperties .defaultSortBy ())
62+ .ascending ()
63+ : Sort .by (pageProperties .defaultSortBy ())
64+ .descending ();
65+ return talentProofRepository .findByStatus (ProofStatus .PUBLISHED ,
66+ PageRequest .of (page .orElse (
67+ pageProperties .defaultPageNum ()),
68+ size .orElse (
69+ pageProperties .defaultPageSize ()), sort ));
70+ }
4371 return talentProofRepository .findByStatus (ProofStatus .PUBLISHED ,
44- PageRequest .of (page .orElse (
45- pageProperties .defaultPageNum ()),
46- size .orElse (
47- pageProperties .defaultPageSize ())));
72+ PageRequest .of (page .orElse (
73+ pageProperties .defaultPageNum ()),
74+ size .orElse (
75+ pageProperties .defaultPageSize ())));
76+ }
77+
78+ @ Transactional
79+ public SessionInfoDTO deleteProofById (long talentId , long proofId , Authentication authentication ) {
80+
81+ Optional <Talent > talent = talentRepository .findById (talentId );
82+ Optional <TalentProof > talentProof = talentProofRepository .findById (proofId );
83+ Optional <UserInfo > userInfo = userInfoRepository .findByLogin (authentication .getName ());
84+ validateTalentForCompliance .userVerification (talent , talentProof , userInfo , talentId , proofId );
85+ talentProofRepository .delete (talentProof .orElseThrow (() -> new ResponseStatusException (NOT_IMPLEMENTED )));
86+ return new SessionInfoDTO ("deleted" , "null" );
87+ }
88+
89+ public ResponseEntity <?> addProof (AddProofDTO addProofDTO , long talentId , Authentication authentication ) {
90+
91+ Optional <Talent > talent = talentRepository .findById (talentId );
92+ Optional <UserInfo > userInfo = userInfoRepository .findByLogin (authentication .getName ());
93+
94+ validateTalentForCompliance .userVerification (talent , userInfo , talentId );
95+
96+ TalentProof talentProof = TalentProof .builder ()
97+ .talent (talent .get ())
98+ .talentId (talentId )
99+ .link (addProofDTO .link ())
100+ .text (addProofDTO .text ())
101+ .status (ProofStatus .DRAFT )
102+ .created (LocalDateTime .now ())
103+ .build ();
104+
105+ talentProofRepository .save (talentProof );
106+
107+ URI location = ServletUriComponentsBuilder
108+ .fromCurrentRequest ()
109+ .path ("/{id}" )
110+ .buildAndExpand (talentProof .getId ())
111+ .toUri ();
112+
113+ return ResponseEntity .created (location ).build ();
48114 }
49115
50116 public FullProofDTO getTalentProofs (Long talentId , Optional <Integer > page , Optional <Integer > size ,
51- Optional <String > direction , Authentication authentication , String ... sortProperties ) {
117+ Optional <String > direction , Authentication authentication ,
118+ String ... sortProperties ) {
52119 Talent talent = talentRepository .findById (talentId )
53- .orElseThrow (() -> new ResponseStatusException (HttpStatus .NOT_FOUND , "Talent with id = %s not found" .formatted (talentId )));
120+ .orElseThrow (() -> new ResponseStatusException (HttpStatus .NOT_FOUND ,
121+ "Talent with id = %s not found" .formatted (
122+ talentId )));
54123 UserInfo userInfo = userInfoRepository .findByLogin (authentication .getName ())
55- .orElseThrow (() -> new ResponseStatusException (HttpStatus .NOT_FOUND , "Talent with id = %s not found" .formatted (talentId )));
124+ .orElseThrow (() -> new ResponseStatusException (HttpStatus .NOT_FOUND ,
125+ "Talent with id = %s not found" .formatted (
126+ talentId )));
56127 Page <TalentProof > proofs ;
57128 PageRequest pageRequest ;
58129 String sortDirection = direction .orElseGet (Sort .DEFAULT_DIRECTION ::name );
@@ -63,7 +134,8 @@ public FullProofDTO getTalentProofs(Long talentId, Optional<Integer> page, Optio
63134 if (size .orElse (pageProperties .defaultPageSize ()) <= 0 ) {
64135 throw new ResponseStatusException (BAD_REQUEST , "'size' query parameter must be greater than or equal to 1" );
65136 }
66- if (!sortDirection .equalsIgnoreCase (Sort .Direction .ASC .name ()) && !sortDirection .equalsIgnoreCase (Sort .Direction .DESC .name ())) {
137+ if (!sortDirection .equalsIgnoreCase (Sort .Direction .ASC .name ()) &&
138+ !sortDirection .equalsIgnoreCase (Sort .Direction .DESC .name ())) {
67139 throw new ResponseStatusException (BAD_REQUEST , "'direction' query param must be equals ASC or DESC" );
68140 }
69141
@@ -84,17 +156,17 @@ public FullProofDTO getTalentProofs(Long talentId, Optional<Integer> page, Optio
84156 }
85157
86158 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 ();
159+ .id (talent .getId ())
160+ .image (talent .getImage ())
161+ .firstName (talent .getFirstName ())
162+ .lastName (talent .getLastName ())
163+ .specialization (talent .getSpecialization ())
164+ .proofs (proofs .map (i -> ProofDTO .builder ()
165+ .id (i .getId ())
166+ .created (i .getCreated ().toString ())
167+ .link (i .getLink ())
168+ .text (i .getText ())
169+ .status (i .getStatus ()).build ()))
170+ .build ();
99171 }
100- }
172+ }
0 commit comments