Skip to content

Commit

Permalink
S3.1US1 (#99)
Browse files Browse the repository at this point in the history
* 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
4 people authored Apr 19, 2023
1 parent 9791946 commit 9c7717e
Show file tree
Hide file tree
Showing 18 changed files with 218 additions and 59 deletions.
32 changes: 32 additions & 0 deletions src/main/java/com/provedcode/kudos/controller/KudosController.java
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 src/main/java/com/provedcode/kudos/model/entity/Kudos.java
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;
}
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 src/main/java/com/provedcode/kudos/repository/KudosRepository.java
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 src/main/java/com/provedcode/kudos/service/KudosService.java
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);
}
}
13 changes: 8 additions & 5 deletions src/main/java/com/provedcode/talent/model/entity/Talent.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.provedcode.talent.model.entity;

import com.provedcode.kudos.model.entity.Kudos;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotEmpty;
import lombok.*;
Expand Down Expand Up @@ -36,14 +37,16 @@ public class Talent {
private String image;
@OneToOne(mappedBy = "talent", cascade = CascadeType.ALL, orphanRemoval = true)
private TalentDescription talentDescription;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "talent", cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(mappedBy = "talent", cascade = CascadeType.ALL, orphanRemoval = true)
private List<TalentLink> talentLinks = new ArrayList<>();
@OneToMany(fetch = FetchType.EAGER, mappedBy = "talent", cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(mappedBy = "talent", cascade = CascadeType.ALL, orphanRemoval = true)
private List<TalentTalents> talentTalents = new ArrayList<>();
@OneToMany(fetch = FetchType.EAGER, mappedBy = "talent", cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(mappedBy = "talent", cascade = CascadeType.ALL, orphanRemoval = true)
private List<TalentContact> talentContacts = new ArrayList<>();
@OneToMany(fetch = FetchType.EAGER, mappedBy = "talent", cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(mappedBy = "talent", cascade = CascadeType.ALL, orphanRemoval = true)
private List<TalentAttachedFile> talentAttachedFiles = new ArrayList<>();
@OneToMany(fetch = FetchType.EAGER, mappedBy = "talent", cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(mappedBy = "talent", cascade = CascadeType.ALL, orphanRemoval = true)
private List<TalentProof> talentProofs = new ArrayList<>();
@OneToMany(mappedBy = "talent", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Kudos> cudoses = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ public class TalentAttachedFile {
@Column(name = "id", nullable = false)
private Long id;
@NotNull
@Column(name = "talent_id", nullable = false)
private Long talentId;
@ManyToOne
@JoinColumn(name = "talent_id", updatable = false)
private Talent talent;
@URL
@Column(name = "attached_file", length = 100)
private String attachedFile;
@NotNull
@ManyToOne
@JoinColumn(name = "talent_id", insertable = false, updatable = false)
private Talent talent;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ public class TalentContact {
@Column(name = "id", nullable = false)
private Long id;
@NotNull
@Column(name = "talent_id", nullable = false)
private Long talentId;
@Column(name = "contact")
private String contact;
@NotNull
@ManyToOne
@JoinColumn(name = "talent_id", insertable = false, updatable = false)
@JoinColumn(name = "talent_id", updatable = false)
private Talent talent;
@Column(name = "contact")
private String contact;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@ public class TalentDescription {
@Column(nullable = false)
private Long id;
@NotNull
@Column(name = "talent_id", nullable = false)
private Long talentId;
@OneToOne(orphanRemoval = true)
@JoinColumn(name = "talent_id", updatable = false)
private Talent talent;
@Column(name = "BIO")
private String bio;
@Column(name = "addition_info")
private String additionalInfo;
@NotNull
@OneToOne(orphanRemoval = true)
@JoinColumn(name = "talent_id", insertable = false, updatable = false)
private Talent talent;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ public class TalentLink {
@Column(nullable = false)
private Long id;
@NotNull
@Column(name = "talent_id", nullable = false)
private Long talentId;
@ManyToOne
@JoinColumn(name = "talent_id", updatable = false)
private Talent talent;
@URL
@Column(name = "link")
private String link;
@NotNull
@ManyToOne
@JoinColumn(name = "talent_id", insertable = false, updatable = false)
private Talent talent;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.provedcode.talent.model.entity;

import com.provedcode.kudos.model.entity.Kudos;
import com.provedcode.talent.model.ProofStatus;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotEmpty;
Expand All @@ -9,6 +10,7 @@
import org.hibernate.validator.constraints.URL;

import java.time.LocalDateTime;
import java.util.List;

@Accessors(chain = true)
@NoArgsConstructor
Expand All @@ -24,8 +26,9 @@ public class TalentProof {
@Column(name = "id", nullable = false)
private Long id;
@NotNull
@Column(name = "talent_id", nullable = false)
private Long talentId;
@ManyToOne
@JoinColumn(name = "talent_id", updatable = false)
private Talent talent;
@NotEmpty
@URL
@Column(name = "link", length = 100)
Expand All @@ -36,8 +39,6 @@ public class TalentProof {
@Column(length = 20)
private ProofStatus status;
private LocalDateTime created;
@NotNull
@ManyToOne
@JoinColumn(name = "talent_id", insertable = false, updatable = false)
private Talent talent;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "proof", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Kudos> kudos;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ public class TalentTalents {
@Column(name = "id", nullable = false)
private Long id;
@NotNull
@Column(name = "talent_id", nullable = false)
private Long talentId;
@Column(name = "talent_name")
private String talentName;
@NotNull
@ManyToOne
@JoinColumn(name = "talent_id", insertable = false, updatable = false)
@JoinColumn(name = "talent_id", updatable = false)
private Talent talent;
@Column(name = "talent_name")
private String talentName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public TalentProof getTalentProof(long proofId, Authentication authentication) {
UserInfo userInfo = userInfoRepository.findByLogin(authentication.getName()).orElseThrow(
() -> new ResponseStatusException(NOT_FOUND, "user with this token not found"));

if (talentProof.getTalentId().equals(userInfo.getTalentId()) ||
if (talentProof.getTalent().getId().equals(userInfo.getTalent().getId()) ||
talentProof.getStatus().equals(ProofStatus.PUBLISHED)) {
return talentProof;
} else {
Expand Down Expand Up @@ -157,7 +157,6 @@ public ResponseEntity<?> addProof(AddProof addProof, long talentId, Authenticati

TalentProof talentProof = TalentProof.builder()
.talent(talent.get())
.talentId(talentId)
.link(addProof.link())
.text(addProof.text())
.status(ProofStatus.DRAFT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public Talent editTalent(long id, EditTalent editTalent, Authentication authenti
.setBio(editTalent.bio() != null ? editTalent.bio() : editableTalentDescription.getBio());
} else {
editableTalentDescription = TalentDescription.builder()
.talentId(idEditableTalent)
.additionalInfo(editTalent.additionalInfo())
.bio(editTalent.bio())
.talent(editableTalent)
Expand All @@ -99,7 +98,6 @@ public Talent editTalent(long id, EditTalent editTalent, Authentication authenti
if (editTalent.talents() != null) {
editableTalentTalents.clear();
editableTalentTalents.addAll(editTalent.talents().stream().map(s -> TalentTalents.builder()
.talentId(idEditableTalent)
.talent(editableTalent)
.talentName(s)
.build()).toList());
Expand All @@ -108,7 +106,6 @@ public Talent editTalent(long id, EditTalent editTalent, Authentication authenti
if (editTalent.links() != null) {
editableTalentLinks.clear();
editableTalentLinks.addAll(editTalent.links().stream().map(l -> TalentLink.builder()
.talentId(idEditableTalent)
.talent(editableTalent)
.link(l)
.build()).toList());
Expand All @@ -117,8 +114,6 @@ public Talent editTalent(long id, EditTalent editTalent, Authentication authenti
if (editTalent.contacts() != null) {
editableTalentContacts.clear();
editableTalentContacts.addAll(editTalent.contacts().stream().map(s -> TalentContact.builder()
.talentId(
idEditableTalent)
.talent(editableTalent)
.contact(s)
.build()).toList());
Expand All @@ -127,8 +122,6 @@ public Talent editTalent(long id, EditTalent editTalent, Authentication authenti
if (editTalent.attachedFiles() != null) {
editableTalentAttachedFile.clear();
editableTalentAttachedFile.addAll(editTalent.attachedFiles().stream().map(s -> TalentAttachedFile.builder()
.talentId(
idEditableTalent)
.talent(editableTalent)
.attachedFile(
s)
Expand Down
Loading

0 comments on commit 9c7717e

Please sign in to comment.