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
3 changes: 2 additions & 1 deletion src/main/java/com/provedcode/aws/service/S3Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ private String getFullPath(String fileType, String userLogin) {
}

private String getFileType(MultipartFile file) {
return file.getContentType().split("/")[1];
String fileName = file.getOriginalFilename();
return fileName != null ? fileName.substring(fileName.lastIndexOf('.') + 1) : null;
}

private File convertMultiPartToFile(MultipartFile file)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.provedcode.talent.controller;

import com.provedcode.talent.model.dto.ProofSkillsDTO;
import com.provedcode.talent.service.TalentSkillsService;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Validated
@AllArgsConstructor

@RestController
@RequestMapping("/api/v4/talents")
public class TalentSkillsController {
TalentSkillsService talentSkillsService;

@PostMapping("/{talent-id}/proofs/{proof-id}/skills")
void addSkillOnProof(@PathVariable("talent-id") long talentId,
@PathVariable("proof-id") long proofId,
@RequestBody @Valid ProofSkillsDTO skills,
Authentication authentication) {
talentSkillsService.addSkillsOnProof(talentId, proofId, skills, authentication);
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/provedcode/talent/model/dto/ProofSkillsDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.provedcode.talent.model.dto;

import jakarta.validation.constraints.NotEmpty;
import lombok.Builder;

import java.util.List;

@Builder
public record ProofSkillsDTO(
@NotEmpty
List<Long> skills
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Getter
@Setter
@Entity
@Table(name = "skills")
@Table(name = "skill")
public class Skills {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public class TalentProof {
@OneToMany(fetch = FetchType.EAGER, mappedBy = "proof", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Kudos> kudos;
@ManyToMany
@JoinTable(name = "talent_skills",
@JoinTable(name = "talent_skill",
joinColumns = @JoinColumn(name = "proof_id"),
inverseJoinColumns = @JoinColumn(name = "skill_id"))
private Set<Skills> skillses = new LinkedHashSet<>();
private Set<Skills> skills = new LinkedHashSet<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.provedcode.talent.repo;

import com.provedcode.talent.model.entity.Skills;
import org.springframework.data.jpa.repository.JpaRepository;

public interface SkillsRepository extends JpaRepository<Skills, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.provedcode.talent.service;

import com.provedcode.talent.model.ProofStatus;
import com.provedcode.talent.model.dto.ProofSkillsDTO;
import com.provedcode.talent.model.entity.Skills;
import com.provedcode.talent.model.entity.TalentProof;
import com.provedcode.talent.repo.SkillsRepository;
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.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;

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

@Transactional
@Service
@AllArgsConstructor
public class TalentSkillsService {
SkillsRepository skillsRepository;
TalentRepository talentRepository;
UserInfoRepository userInfoRepository;
TalentProofRepository talentProofRepository;

static BiConsumer<Long, UserInfo> isValidUserEditTalent = (talentId, userInfo) -> {
if (!userInfo.getTalent().getId().equals(talentId)) {
throw new ResponseStatusException(CONFLICT, "you can`t change another talent");
}
};

public void addSkillsOnProof(long talentId, long proofId, ProofSkillsDTO skills, Authentication authentication) {
if (!talentRepository.existsById(talentId)) {
throw new ResponseStatusException(NOT_FOUND, "talent with id = %s not found".formatted(talentId));
}
UserInfo userInfo = userInfoRepository.findByLogin(authentication.getName())
.orElseThrow(() -> new ResponseStatusException(NOT_FOUND));
TalentProof talentProof = talentProofRepository.findById(proofId)
.orElseThrow(() -> new ResponseStatusException(NOT_FOUND, "proof with id = %s not found".formatted(proofId)));
if (!talentProof.getStatus().equals(ProofStatus.DRAFT)) {
throw new ResponseStatusException(CONFLICT, "proof status must be DRAFT");
}

isValidUserEditTalent.accept(talentId, userInfo);
if (skills.skills().stream().anyMatch(i -> !skillsRepository.existsById(i))) {
throw new ResponseStatusException(BAD_REQUEST, "no such skill with id");
}

Set<Skills> skillsSet = new HashSet<>(skillsRepository.findAllById(skills.skills()));

talentProof.getSkills().addAll(skillsSet);
talentProofRepository.save(talentProof);
}


}