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
Expand Up @@ -12,9 +12,6 @@
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

Expand Down Expand Up @@ -58,4 +55,12 @@ FullProofDTO getTalentInformationWithProofs(Authentication authentication,
@RequestParam(value = "sort", defaultValue = "created") String... sort) {
return talentProofService.getTalentProofs(talentId, page, size, direction, authentication, sort);
}

@PreAuthorize("hasRole('TALENT')")
@GetMapping("/{talent-id}/proofs/{proof-id}")
ProofDTO getTalentProof(@PathVariable(value = "talent-id") long talentId,
@PathVariable(value = "proof-id") long proofId,
Authentication authentication) {
return talentProofService.getTalentProof(talentId, proofId, authentication);
}
}
3 changes: 0 additions & 3 deletions src/main/java/com/provedcode/talent/model/dto/ProofDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import com.provedcode.talent.model.ProofStatus;
import lombok.Builder;

import java.time.LocalDateTime;
import java.util.Date;

@Builder
public record ProofDTO(
long id,
Expand Down
106 changes: 65 additions & 41 deletions src/main/java/com/provedcode/talent/service/TalentProofService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.provedcode.user.model.dto.SessionInfoDTO;
import com.provedcode.user.model.entity.UserInfo;
import com.provedcode.user.repo.UserInfoRepository;
import com.provedcode.utill.ValidateTalentForCompliance;
import com.provedcode.talent.utill.ValidateTalentForCompliance;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
Expand All @@ -30,8 +30,7 @@
import java.time.LocalDateTime;
import java.util.Optional;

import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.NOT_IMPLEMENTED;
import static org.springframework.http.HttpStatus.*;

@Service
@AllArgsConstructor
Expand All @@ -54,25 +53,25 @@ public Page<TalentProof> getAllProofsPage(Optional<Integer> page, Optional<Integ

if (orderBy.isPresent()) {
if (!orderBy.get().equalsIgnoreCase(Sort.Direction.ASC.name()) &&
!orderBy.get().equalsIgnoreCase(Sort.Direction.DESC.name())) {
!orderBy.get().equalsIgnoreCase(Sort.Direction.DESC.name())) {
throw new ResponseStatusException(BAD_REQUEST, "'orderBy' query parameter must be ASC or DESC");
}
Sort sort =
orderBy.get().equalsIgnoreCase(Sort.Direction.ASC.name()) ? Sort.by(pageProperties.defaultSortBy())
.ascending()
: Sort.by(pageProperties.defaultSortBy())
.descending();
.ascending()
: Sort.by(pageProperties.defaultSortBy())
.descending();
return talentProofRepository.findByStatus(ProofStatus.PUBLISHED,
PageRequest.of(page.orElse(
pageProperties.defaultPageNum()),
size.orElse(
pageProperties.defaultPageSize()), sort));
PageRequest.of(page.orElse(
pageProperties.defaultPageNum()),
size.orElse(
pageProperties.defaultPageSize()), sort));
}
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())));
}

@Transactional
Expand All @@ -94,13 +93,13 @@ public ResponseEntity<?> addProof(AddProofDTO addProofDTO, long talentId, Authen
validateTalentForCompliance.userVerification(talent, userInfo, talentId);

TalentProof talentProof = TalentProof.builder()
.talent(talent.get())
.talentId(talentId)
.link(addProofDTO.link())
.text(addProofDTO.text())
.status(ProofStatus.DRAFT)
.created(LocalDateTime.now())
.build();
.talent(talent.get())
.talentId(talentId)
.link(addProofDTO.link())
.text(addProofDTO.text())
.status(ProofStatus.DRAFT)
.created(LocalDateTime.now())
.build();

talentProofRepository.save(talentProof);

Expand All @@ -117,13 +116,13 @@ public FullProofDTO getTalentProofs(Long talentId, Optional<Integer> page, Optio
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)));
.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)));
.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);
Expand All @@ -135,7 +134,7 @@ public FullProofDTO getTalentProofs(Long talentId, Optional<Integer> page, Optio
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())) {
!sortDirection.equalsIgnoreCase(Sort.Direction.DESC.name())) {
throw new ResponseStatusException(BAD_REQUEST, "'direction' query param must be equals ASC or DESC");
}

Expand All @@ -156,17 +155,42 @@ public FullProofDTO getTalentProofs(Long talentId, Optional<Integer> page, Optio
}

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();
.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();
}

public ProofDTO getTalentProof(long talentId, long proofId, Authentication authentication) {
Optional<TalentProof> talentProof = talentProofRepository.findById(proofId);
if (talentProof.isPresent()) {
if (talentProof.get().getTalentId() != talentId) {
throw new ResponseStatusException(BAD_REQUEST,
String.format("proof with id = %d not equal to talent id = %d", proofId, talentId));
}
} else {
throw new ResponseStatusException(NOT_FOUND, String.format("proof with id = %d not found", proofId));
}
Optional<UserInfo> userInfo = userInfoRepository.findByLogin(authentication.getName());
if (userInfo.get().getTalentId() == talentId ||
talentProof.get().getStatus().equals(ProofStatus.PUBLISHED)) {
return ProofDTO.builder()
.id(talentProof.get().getTalentId())
.link(talentProof.get().getLink())
.status(talentProof.get().getStatus())
.created(talentProof.get().getCreated().toString())
.text(talentProof.get().getText())
.build();
} else {
throw new ResponseStatusException(FORBIDDEN);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.provedcode.user.model.dto.SessionInfoDTO;
import com.provedcode.user.model.entity.UserInfo;
import com.provedcode.user.repo.UserInfoRepository;
import com.provedcode.utill.ValidateTalentForCompliance;
import com.provedcode.talent.utill.ValidateTalentForCompliance;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.provedcode.utill;
package com.provedcode.talent.utill;

import com.provedcode.talent.model.entity.Talent;
import com.provedcode.talent.model.entity.TalentProof;
Expand All @@ -19,7 +19,7 @@ public void userVerification(Optional<Talent> talent, Optional<UserInfo> userInf
throw new ResponseStatusException(NOT_FOUND, String.format("talent with id = %d not found", id));
}
if (userInfo.get().getTalent().getId() != id) {
throw new ResponseStatusException(FORBIDDEN, "you can`t add proof another user");
throw new ResponseStatusException(FORBIDDEN);
}
}

Expand Down