-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor: * remove Eager it Talent entity * Task#1-2 (#93) * Created Cudos entity and did some DB refactor * Created 'GET(/proofs/{proof-id}/kudos)' endpoint in KudosController * Created method 'getAmountKudosProof' in KudosService * Created KudosRepository and add 'countByProof_Id' method * Task#3 As a talent I want to estimate proofs of other talents by givi… (#96) * Task#3 As a talent I want to estimate proofs of other talents by giving "kudos" * bug fix * S3.1 us1.task 4 (#97) * Added don`t working service * @manytoone relationSHIT * Code refactor and bugfix * Transactional --------- Co-authored-by: Ren <[email protected]> Co-authored-by: Ren <[email protected]> Co-authored-by: Denis973 <[email protected]>
- Loading branch information
1 parent
9791946
commit 9c7717e
Showing
18 changed files
with
218 additions
and
59 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/main/java/com/provedcode/kudos/controller/KudosController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.provedcode.kudos.controller; | ||
|
||
import com.provedcode.kudos.service.KudosService; | ||
import com.provedcode.kudos.model.response.KudosAmount; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@AllArgsConstructor | ||
@RequestMapping("/api/v3/talent") | ||
public class KudosController { | ||
KudosService kudosService; | ||
|
||
@GetMapping("/proofs/{proof-id}/kudos") | ||
KudosAmount getKudosProof(@PathVariable("proof-id") long id) { | ||
return kudosService.getAmountKudosProof(id); | ||
} | ||
|
||
@PreAuthorize("hasRole('TALENT')") | ||
@PostMapping("/proofs/{proof-id}/kudos") | ||
void addKudosToProof(@PathVariable("proof-id") long id, Authentication authentication) { | ||
kudosService.addKudosToProof(id, authentication); | ||
} | ||
|
||
@PreAuthorize("hasRole('TALENT')") | ||
@DeleteMapping("/proofs/{proof-id}/kudos") | ||
void deleteKudosFromProof(@PathVariable("proof-id") long id, Authentication authentication) { | ||
kudosService.deleteKudosFromProof(id, authentication); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/provedcode/kudos/model/entity/Kudos.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.provedcode.kudos.model.entity; | ||
|
||
import com.provedcode.talent.model.entity.Talent; | ||
import com.provedcode.talent.model.entity.TalentProof; | ||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
@Entity | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Table(name = "kudos") | ||
public class Kudos { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "id", nullable = false) | ||
private Long id; | ||
@ManyToOne(cascade = CascadeType.ALL) | ||
@JoinColumn(name = "talent_id") | ||
private Talent talent; | ||
@ManyToOne | ||
@JoinColumn(name = "proof_id") | ||
private TalentProof proof; | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/provedcode/kudos/model/response/KudosAmount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.provedcode.kudos.model.response; | ||
|
||
public record KudosAmount( | ||
long amount | ||
) { | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/provedcode/kudos/repository/KudosRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.provedcode.kudos.repository; | ||
|
||
import com.provedcode.kudos.model.entity.Kudos; | ||
import com.provedcode.talent.model.entity.Talent; | ||
import com.provedcode.talent.model.entity.TalentProof; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface KudosRepository extends JpaRepository<Kudos, Long> { | ||
long countByProof_Id(Long id); | ||
|
||
boolean existsByTalent(Talent talent); | ||
} |
89 changes: 89 additions & 0 deletions
89
src/main/java/com/provedcode/kudos/service/KudosService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.provedcode.kudos.service; | ||
|
||
import com.provedcode.kudos.model.entity.Kudos; | ||
import com.provedcode.kudos.model.response.KudosAmount; | ||
import com.provedcode.kudos.repository.KudosRepository; | ||
import com.provedcode.talent.model.ProofStatus; | ||
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 org.springframework.http.HttpStatus; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
import java.util.List; | ||
|
||
import static org.springframework.http.HttpStatus.*; | ||
|
||
@Service | ||
@AllArgsConstructor | ||
@Transactional | ||
public class KudosService { | ||
KudosRepository kudosRepository; | ||
TalentRepository talentRepository; | ||
TalentProofRepository talentProofRepository; | ||
UserInfoRepository userInfoRepository; | ||
|
||
@Transactional(readOnly = true) | ||
public KudosAmount getAmountKudosProof(long id) { | ||
TalentProof talentProof = talentProofRepository.findById(id) | ||
.orElseThrow(() -> new ResponseStatusException(NOT_FOUND, "proof with id = %s not found".formatted(id))); | ||
|
||
if (!talentProof.getStatus().equals(ProofStatus.PUBLISHED)) { | ||
throw new ResponseStatusException(FORBIDDEN); | ||
} | ||
|
||
long count = kudosRepository.countByProof_Id(id); | ||
return new KudosAmount(count); | ||
} | ||
|
||
public void addKudosToProof(long id, Authentication authentication) { | ||
UserInfo userInfo = userInfoRepository.findByLogin(authentication.getName()) | ||
.orElseThrow(() -> new ResponseStatusException(NOT_FOUND, "Talent with id = %s not found".formatted(id))); | ||
|
||
Talent talent = userInfo.getTalent(); | ||
|
||
TalentProof talentProof = talentProofRepository.findById(id) | ||
.orElseThrow(() -> new ResponseStatusException(NOT_FOUND, "Proof with id = %s not found".formatted(id))); | ||
|
||
if (talent.getId().equals(talentProof.getTalent().getId())) { | ||
throw new ResponseStatusException(FORBIDDEN, "Talent can’t give `kudos` to himself"); | ||
} | ||
if (kudosRepository.existsByTalent(talent)) { | ||
throw new ResponseStatusException(CONFLICT, "Talent can give only one `kudos` for one proof"); | ||
} | ||
if (!talentProof.getStatus().equals(ProofStatus.PUBLISHED)) { | ||
throw new ResponseStatusException(FORBIDDEN); | ||
} | ||
|
||
kudosRepository.save(Kudos.builder() | ||
.proof(talentProof) | ||
.talent(talent) | ||
.build()); | ||
} | ||
|
||
public void deleteKudosFromProof(long id, Authentication authentication) { | ||
UserInfo userInfo = userInfoRepository.findByLogin(authentication.getName()) | ||
.orElseThrow(() -> new ResponseStatusException(NOT_FOUND, "Talent with id = %s not found".formatted(id))); | ||
Talent talent = userInfo.getTalent(); | ||
|
||
TalentProof talentProof = talentProofRepository.findById(id) | ||
.orElseThrow(() -> new ResponseStatusException(NOT_FOUND, "Proof with id = %s not found".formatted(id))); | ||
|
||
if (!kudosRepository.existsByTalent(talent)) { | ||
throw new ResponseStatusException(CONFLICT, "kudos don`t exist"); | ||
} | ||
|
||
Kudos kudos = talent.getCudoses().stream().filter(i -> i.getProof().getId().equals(id)).findFirst().orElseThrow(); | ||
talentProof.getKudos().remove(kudos); | ||
talent.getCudoses().remove(kudos); | ||
|
||
talentRepository.save(talent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.