Skip to content

Commit

Permalink
label deletion reworked
Browse files Browse the repository at this point in the history
  • Loading branch information
sergi-Jr committed Jul 17, 2024
1 parent c7153a3 commit a58ccc5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import hexlet.code.app.exception.ResourceNotFoundException;
import hexlet.code.app.exception.UnableDeletionException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
Expand All @@ -20,9 +19,4 @@ public ResponseEntity<String> handleResourceNotFound(ResourceNotFoundException e
public ResponseEntity<String> handleUnableDeletion(UnableDeletionException exception) {
return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body(exception.getMessage());
}

@ExceptionHandler(DataIntegrityViolationException.class)
public ResponseEntity<String> handleIntegrityViolationException(DataIntegrityViolationException exception) {
return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body(exception.getMessage());
}
}
8 changes: 8 additions & 0 deletions src/main/java/hexlet/code/app/label/LabelRepository.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package hexlet.code.app.label;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Optional;
import java.util.Set;

@Repository
public interface LabelRepository extends JpaRepository<Label, Long> {
Optional<Label> findByName(String name);

@Query(value = """
select label_id from task_labels
where label_id = ?1
""", nativeQuery = true)
Set<Long> getIdFromCrossTable(Long id);
}
6 changes: 6 additions & 0 deletions src/main/java/hexlet/code/app/label/LabelService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hexlet.code.app.label;

import hexlet.code.app.exception.ResourceNotFoundException;
import hexlet.code.app.exception.UnableDeletionException;
import hexlet.code.app.label.dto.LabelCreateDTO;
import hexlet.code.app.label.dto.LabelDTO;
import hexlet.code.app.label.dto.LabelUpdateDTO;
Expand All @@ -11,6 +12,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service
@Transactional(readOnly = true)
Expand Down Expand Up @@ -51,6 +53,10 @@ public LabelDTO update(Long id, LabelUpdateDTO data) {

@Transactional
public void delete(Long id) {
Set<Long> links = repository.getIdFromCrossTable(id);
if (!links.isEmpty()) {
throw new UnableDeletionException("Label has active tasks");
}
repository.deleteById(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
Expand Down

0 comments on commit a58ccc5

Please sign in to comment.