Commit 22f59074 authored by Tomáš Biloš's avatar Tomáš Biloš
Browse files

fix creating skills

parent 06fe27d4
Pipeline #142519 failed with stage
in 11 seconds
......@@ -4,8 +4,11 @@ import org.springframework.data.repository.CrudRepository;
import cz.fi.muni.pa165.seminar4.group7.entity.Skill;
import java.util.Optional;
/**
* @author Tomáš Biloš
*/
public interface SkillDao extends CrudRepository<Skill, String> {
Optional<Skill> findByName(String name);
}
......@@ -100,7 +100,12 @@ public class AgentFacadeImpl implements AgentFacade {
private Set<Skill> createSkills(Set<Skill> skills) {
for (var skill : skills) {
if (skill.getId() == null) {
skill.setId(UUID.randomUUID().toString());
var optSkill = skillService.findByName(skill.getName());
if (optSkill.isEmpty()) {
skill.setId(UUID.randomUUID().toString());
} else {
skill.setId(optSkill.get().getId());
}
}
}
return skills;
......
......@@ -27,6 +27,16 @@ public interface SkillService {
*/
Optional<Skill> findById(String id);
/**
* Look for skill with specific name.
*
* @param name
* name to look for.
*
* @return Optional of skill if skill with name was found, optional of empty otherwise.
*/
Optional<Skill> findByName(String name);
/**
* Creates skill.
*
......
......@@ -31,6 +31,11 @@ public class SkillServiceImpl implements SkillService {
return skillDao.findById(id);
}
@Override
public Optional<Skill> findByName(String name) {
return skillDao.findByName(name);
}
@Override
public void create(Skill skill) {
if (skillDao.findById(skill.getId()).isPresent()) {
......@@ -41,7 +46,7 @@ public class SkillServiceImpl implements SkillService {
@Override
public void deleteById(String id) {
if (!skillDao.findById(id).isPresent()) {
if (skillDao.findById(id).isEmpty()) {
throw new IllegalArgumentException("Record for given skill doesn't exist.");
}
skillDao.deleteById(id);
......@@ -49,7 +54,7 @@ public class SkillServiceImpl implements SkillService {
@Override
public void update(Skill skill) {
if (!skillDao.findById(skill.getId()).isPresent()) {
if (skillDao.findById(skill.getId()).isEmpty()) {
throw new IllegalArgumentException("Record for given skill doesn't exist.");
}
skillDao.save(skill);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment