Skip to content

Commit

Permalink
fix tables realations. add data cleanup before initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
sergi-Jr committed Jul 2, 2024
1 parent 694060b commit 4690a92
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/main/java/hexlet/code/app/component/DataInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"});
Expand Down Expand Up @@ -60,4 +65,11 @@ private void initLabels(String[] defaults) {
labelRepository.save(label);
}
}

private void dbCleanup() {
labelRepository.deleteAll();
statusRepository.deleteAll();
taskRepository.deleteAll();
userRepository.deleteAll();
}
}
4 changes: 2 additions & 2 deletions src/main/java/hexlet/code/app/label/LabelService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/hexlet/code/app/task/model/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
public interface TaskRepository extends JpaRepository<Task, Long>, JpaSpecificationExecutor<Task> {
List<Task> findAllByTaskStatus(TaskStatus status);

@Query("""
select t from Task t
join Label l on
l.name like :label
""")
List<Task> 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<Task> findAllByLabel(Long labelId);
}

0 comments on commit 4690a92

Please sign in to comment.