Commit 5b4c7e2a authored by François-Xavier Lebastard's avatar François-Xavier Lebastard
Browse files

feat(gestion ordre écran) : suppression d'un écran

parent b420fe0e
......@@ -2,6 +2,7 @@ package com.unantes.orientactive.repository;
import com.unantes.orientactive.domain.Screen;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
......@@ -99,4 +100,16 @@ public interface ScreenRepository extends JpaRepository<Screen, Long> {
List<Screen> findAllByFormId(final Long formId);
Optional<Screen> findScreenByIndexAndFormId(final Integer index, final Long formId);
/**
* Diminue l'index des écrans d'un formulaire dont l'index est supérieur à celui passé en paramètre.
* @param formId un identifiant de formulaire
* @param index un index (position) d'écran
*/
@Query(
value = "UPDATE screen SET index = (index - 1) WHERE index > :index AND form_id = :formId",
nativeQuery = true
)
@Modifying(flushAutomatically = true, clearAutomatically = true)
void decreaseScreenIndex(@Param("formId") Long formId, @Param("index") Integer index);
}
package com.unantes.orientactive.service;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.unantes.orientactive.domain.Screen;
import com.unantes.orientactive.domain.User;
import com.unantes.orientactive.repository.ScreenRepository;
......@@ -8,17 +20,7 @@ import com.unantes.orientactive.service.dto.ScreenDTO;
import com.unantes.orientactive.service.dto.ScreenPanelDTO;
import com.unantes.orientactive.service.mapper.ScreenMapper;
import com.unantes.orientactive.service.mapper.ScreenPanelMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import com.unantes.orientactive.service.exception.EntityNotFoundException;
/**
* Service Implementation for managing {@link Screen}.
......@@ -113,7 +115,9 @@ public class ScreenService {
*/
public void delete(Long id) {
log.debug("Supression de l'écran d'identifiant '{}'.", id);
screenRepository.deleteById(id);
final Screen screenToDelete = screenRepository.findById(id).orElseThrow(() -> new EntityNotFoundException(id));
screenRepository.delete(screenToDelete);
screenRepository.decreaseScreenIndex(screenToDelete.getForm().getId(), screenToDelete.getIndex());
}
/**
......
package com.unantes.orientactive.service.exception;
/**
* Erreur retournée lorsqu'une opération est exécutée sur une entité qui n'existe pas en base de donnée.
*/
public class EntityNotFoundException extends RuntimeException {
/**
*
* @param entityId la clé primaire de l'entité non trouvée en base de donnée.
*/
public EntityNotFoundException(final Long entityId) {
super(String.format("L'entité avec l'ID : '%d' n'existe pas en base", entityId));
}
}
......@@ -3,6 +3,8 @@ package com.unantes.orientactive.web.rest.errors;
import com.unantes.orientactive.filariane.FilArianeException;
import com.unantes.orientactive.navigation.exception.NavigationException;
import com.unantes.orientactive.security.permissions.web.PermissionException;
import com.unantes.orientactive.service.exception.EntityNotFoundException;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
......@@ -173,6 +175,15 @@ public class ExceptionTranslator implements ProblemHandling, SecurityAdviceTrait
.build();
return create(exception, problem, request);
}
@ExceptionHandler
public ResponseEntity<Problem> handleEntityNotFoundException(EntityNotFoundException exception, NativeWebRequest request) {
Problem problem = Problem
.builder()
.withStatus(Status.NOT_FOUND)
.with(MESSAGE_KEY, exception.getMessage())
.build();
return create(exception, problem, request);
}
@ExceptionHandler
public ResponseEntity<Problem> handlePermissionException(PermissionException exception, NativeWebRequest request) {
......
......@@ -73,6 +73,7 @@ public class ScreenResourceIT {
public static final String UPDATED_NEXT_SCREEN_EXPRESSION_02 = "answers[S01_Q1_4]";
public static final String SCREEN_2 = "screen2";
private List<NextScreenExpression> nextScreenExpressions;
......@@ -125,6 +126,7 @@ public class ScreenResourceIT {
.defaultNextScreenReference(DEFAULT_NEXT_SCREEN_REFERENCE)
.items(DEFAULT_ITEMS)
.nextScreenExpressions(nextScreenExpressions);
return screen;
}
......@@ -345,20 +347,44 @@ public class ScreenResourceIT {
@Test
@Transactional
public void deleteScreen() throws Exception {
// Initialize the database
screenRepository.saveAndFlush(screen);
int databaseSizeBeforeDelete = screenRepository.findAll().size();
Screen screen2 = new Screen()
.name(SCREEN_2)
.nameBo(SCREEN_2)
.index(2)
.form(form)
.reference(SCREEN_2)
.description(SCREEN_2)
.defaultNextScreenReference(DEFAULT_NEXT_SCREEN_REFERENCE)
.items(DEFAULT_ITEMS)
.nextScreenExpressions(nextScreenExpressions);
// Initialize the database
screenRepository.save(screen);
screenRepository.save(screen2);
screenRepository.findAll().size();
// Delete the screen
restScreenMockMvc.perform(delete("/api/screens/{id}", screen.getId())
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
// Validate the database contains one less item
List<Screen> screenList = screenRepository.findAll();
assertThat(screenList).hasSize(databaseSizeBeforeDelete - 1);
assertThat(screenList).hasSize(1);
Screen screen2AfterDeletion = screenList.get(0);
assertThat(screen2AfterDeletion.getName()).isEqualTo(screen2.getName());
assertThat(screen2AfterDeletion.getIndex()).isEqualTo(1);
}
@Transactional
@Test
public void deleteNotExistingScreen() throws Exception {
restScreenMockMvc.perform(delete("/api/screens/{id}", 1L)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.message").value("L'entité avec l'ID : '1' n'existe pas en base"));
}
@Test
@Transactional
public void deleteNextScreenExpression() throws Exception {
......
......@@ -12,7 +12,10 @@
# Full reference is available at:
# http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
# ===================================================================
logging:
level:
org.hibernate.type.descriptor.sql.BasicBinder: TRACE
org.hibernate.SQL: DEBUG
spring:
profiles:
# Uncomment the following line to enable tests against production database type rather than H2, using Testcontainers
......@@ -33,6 +36,7 @@ spring:
jpa:
database-platform: tech.jhipster.domain.util.FixedH2Dialect
open-in-view: false
show-sql: true
hibernate:
ddl-auto: none
naming:
......
Markdown is supported
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