Skip to content

Commit

Permalink
Task#1-2 (#93)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
LordRenDS authored Apr 18, 2023
1 parent b8c2579 commit d9b77b8
Show file tree
Hide file tree
Showing 18 changed files with 123 additions and 54 deletions.
22 changes: 22 additions & 0 deletions src/main/java/com/provedcode/kudos/KudosController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.provedcode.kudos;

import com.provedcode.kudos.model.response.KudosAmount;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@AllArgsConstructor
@RequestMapping("/api/v3/talent")
public class KudosController {
KudosService kudosService;

@PreAuthorize("hasRole('TALENT')")
@GetMapping("/proofs/{proof-id}/kudos")
KudosAmount getKudosProof(@PathVariable("proof-id") long id) {
return kudosService.getAmountKudosProof(id);
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/provedcode/kudos/KudosRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.provedcode.kudos;

import com.provedcode.kudos.model.entity.Kudos;
import org.springframework.data.jpa.repository.JpaRepository;

public interface KudosRepository extends JpaRepository<Kudos, Long> {
long countByProof_Id(Long id);
}
16 changes: 16 additions & 0 deletions src/main/java/com/provedcode/kudos/KudosService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.provedcode.kudos;

import com.provedcode.kudos.model.response.KudosAmount;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class KudosService {
KudosRepository kudosRepository;

public KudosAmount getAmountKudosProof(long id) {
long count = kudosRepository.countByProof_Id(id);
return new KudosAmount(count);
}
}
26 changes: 26 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,26 @@
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.*;

@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;
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@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
) {
}
3 changes: 3 additions & 0 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 @@ -46,4 +47,6 @@ public class Talent {
private List<TalentAttachedFile> talentAttachedFiles = new ArrayList<>();
@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 @@ -24,8 +25,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 +38,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;
@OneToOne(mappedBy = "proof", cascade = CascadeType.ALL, orphanRemoval = true)
private 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
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void userAndProofVerification(Optional<Talent> talent,
if (talentProof.isEmpty()) {
throw new ResponseStatusException(NOT_FOUND, String.format("proof with id = %d not found", proofId));
}
if (talentProof.get().getTalentId() != talentId) {
if (talentProof.get().getTalent().getId() != talentId) {
throw new ResponseStatusException(FORBIDDEN, "you can`t delete/update another proof");
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/com/provedcode/user/model/entity/UserInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ public class UserInfo {
@Column(name = "id", nullable = false)
private Long id;
@NotNull
@Column(name = "talent_id")
private Long talentId;
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "talent_id", updatable = false)
private Talent talent;
@NotEmpty
@NotNull
@Column(name = "login", length = 100)
Expand All @@ -32,9 +33,6 @@ public class UserInfo {
@NotNull
@Column(name = "password")
private String password;
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "talent_id", insertable = false, updatable = false)
private Talent talent;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "user_authorities",
joinColumns = @JoinColumn(name = "user_id"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public UserInfoDTO login(String name, Collection<? extends GrantedAuthority> aut

return UserInfoDTO.builder()
.token(generateJWTToken(name, authorities))
.id(userInfo.getTalentId())
.id(userInfo.getTalent().getId())
.build();
}

Expand All @@ -68,7 +68,7 @@ public UserInfoDTO register(RegistrationDTO user) {
talentEntityRepository.save(talent);

UserInfo userInfo = UserInfo.builder()
.talentId(talent.getId())
.talent(talent)
.login(user.login())
.password(passwordEncoder.encode(user.password()))
.authorities(Set.of(authorityRepository.findByAuthority(Role.TALENT).orElseThrow()))
Expand Down
15 changes: 14 additions & 1 deletion src/main/resources/schema.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

drop table if exists authority cascade ;
create table authority
(
Expand Down Expand Up @@ -94,6 +93,16 @@ create table user_info
primary key (id)
);


drop table if exists kudos cascade;
create table kudos
(
id bigserial not null,
proof_id bigint,
talent_id bigint,
primary key (id)
);

alter table if exists talent_attached_file
add constraint FKdtjomr27q99ufe065trf8jr7b foreign key (talent_id) references talent;
alter table if exists talent_contact
Expand All @@ -112,3 +121,7 @@ alter table if exists user_authorities
add constraint FKhrxn11h0wl1txiaukxjp01uji foreign key (user_id) references user_info;
alter table if exists user_info
add constraint FKng34qd4ikmdcwg4f8bcpghar9 foreign key (talent_id) references talent;
alter table if exists kudos
add constraint FKkk086iax3mb3yn50g6q4u4gx9 foreign key (proof_id) references talent_proofs;
alter table if exists kudos
add constraint FKsgluvtc41jxfpn3v6ymv8t39k foreign key (talent_id) references talent;

0 comments on commit d9b77b8

Please sign in to comment.