diff --git a/src/main/java/hexlet/code/app/component/DataInitializer.java b/src/main/java/hexlet/code/app/component/DataInitializer.java index c2e187d78..efa5b263d 100644 --- a/src/main/java/hexlet/code/app/component/DataInitializer.java +++ b/src/main/java/hexlet/code/app/component/DataInitializer.java @@ -3,6 +3,7 @@ import hexlet.code.app.label.Label; import hexlet.code.app.label.LabelRepository; import hexlet.code.app.task.model.TaskStatus; +import hexlet.code.app.task.repository.TaskRepository; import hexlet.code.app.task.repository.TaskStatusRepository; import hexlet.code.app.user.User; import hexlet.code.app.user.dto.UserCreateDTO; @@ -26,11 +27,15 @@ public class DataInitializer implements ApplicationRunner { @Autowired private final LabelRepository labelRepository; + @Autowired + private final TaskRepository taskRepository; + @Autowired private final UserMapper mapper; @Override public void run(ApplicationArguments args) throws Exception { + dbCleanup(); initUser(); initTaskStatuses(new String[]{"draft", "to_review", "to_be_fixed", "to_publish", "published"}); initLabels(new String[]{"feature", "bug"}); @@ -60,4 +65,11 @@ private void initLabels(String[] defaults) { labelRepository.save(label); } } + + private void dbCleanup() { + labelRepository.deleteAll(); + statusRepository.deleteAll(); + taskRepository.deleteAll(); + userRepository.deleteAll(); + } } diff --git a/src/main/java/hexlet/code/app/label/LabelService.java b/src/main/java/hexlet/code/app/label/LabelService.java index 8e96d80e0..1018504df 100644 --- a/src/main/java/hexlet/code/app/label/LabelService.java +++ b/src/main/java/hexlet/code/app/label/LabelService.java @@ -52,8 +52,8 @@ public LabelDTO update(Long id, LabelUpdateDTO data) { @Transactional public void delete(Long id) { - String label = repository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Not found")).getName(); - if (!taskRepository.findAllByLabel(label).isEmpty()) { + Long labelId = repository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Not found")).getId(); + if (!taskRepository.findAllByLabel(labelId).isEmpty()) { throw new UnableDeletionException("Label has active tasks"); } repository.deleteById(id); diff --git a/src/main/java/hexlet/code/app/task/model/Task.java b/src/main/java/hexlet/code/app/task/model/Task.java index 3fe4a1e79..00e6bc4f7 100644 --- a/src/main/java/hexlet/code/app/task/model/Task.java +++ b/src/main/java/hexlet/code/app/task/model/Task.java @@ -12,7 +12,6 @@ import jakarta.persistence.JoinTable; import jakarta.persistence.ManyToMany; import jakarta.persistence.ManyToOne; -import jakarta.persistence.OneToOne; import jakarta.persistence.PrimaryKeyJoinColumn; import jakarta.persistence.Table; import jakarta.validation.constraints.NotBlank; @@ -49,7 +48,7 @@ public class Task implements BaseEntity { private String description; - @OneToOne(fetch = FetchType.LAZY) + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "task_status_id", referencedColumnName = "id") @ToString.Include private TaskStatus taskStatus; diff --git a/src/main/java/hexlet/code/app/task/repository/TaskRepository.java b/src/main/java/hexlet/code/app/task/repository/TaskRepository.java index fb9da8c50..4744b91db 100644 --- a/src/main/java/hexlet/code/app/task/repository/TaskRepository.java +++ b/src/main/java/hexlet/code/app/task/repository/TaskRepository.java @@ -13,10 +13,11 @@ public interface TaskRepository extends JpaRepository, JpaSpecificationExecutor { List findAllByTaskStatus(TaskStatus status); - @Query(""" - select t from Task t - join Label l on - l.name like :label - """) - List findAllByLabel(String label); + @Query(value = """ + select * from Tasks t + inner join TASK_LABELS tl on + tl.label_id = ?1 + where tl.task_id = t.id + """, nativeQuery = true) + List findAllByLabel(Long labelId); }