Ajout d'une vérification lors de la suppression d'une entité qui vérifie si l'entité peut être supprimée.

This commit is contained in:
FyloZ 2021-03-09 19:24:00 -05:00
parent c5d3791460
commit 16f4a36693
51 changed files with 217 additions and 105 deletions

View File

@ -1,22 +0,0 @@
package dev.fyloz.trial.colorrecipesexplorer.exception.model
import dev.fyloz.trial.colorrecipesexplorer.exception.RestException
import dev.fyloz.trial.colorrecipesexplorer.model.Model
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.ResponseStatus
class EntityNotFoundException(
modelType: Class<out Model>,
val identifierType: IdentifierType,
val requestedId: Any
) : ModelException(modelType)
@ResponseStatus(HttpStatus.NOT_FOUND)
class EntityNotFoundRestException(val value: Any) :
RestException("An entity could not be found with the given identifier", HttpStatus.NOT_FOUND) {
@Suppress("unused")
override fun buildBody(): RestExceptionBody = object : RestExceptionBody() {
val id = value
}
}

View File

@ -1,6 +1,6 @@
package dev.fyloz.trial.colorrecipesexplorer.service;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.ModelException;
import dev.fyloz.trial.colorrecipesexplorer.model.NamedModel;
import dev.fyloz.trial.colorrecipesexplorer.repository.NamedJpaRepository;

View File

@ -3,8 +3,8 @@ package dev.fyloz.trial.colorrecipesexplorer.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.fyloz.trial.colorrecipesexplorer.config.Preferences;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.ModelException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.NullIdentifierException;
import dev.fyloz.trial.colorrecipesexplorer.model.Model;

View File

@ -1,6 +1,6 @@
package dev.fyloz.trial.colorrecipesexplorer.service.model;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityLinkedException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.ModelException;
import dev.fyloz.trial.colorrecipesexplorer.model.Company;

View File

@ -1,6 +1,6 @@
package dev.fyloz.trial.colorrecipesexplorer.service.model;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.ModelException;
import dev.fyloz.trial.colorrecipesexplorer.model.Material;
import dev.fyloz.trial.colorrecipesexplorer.model.MaterialSaveDto;

View File

@ -2,8 +2,8 @@ package dev.fyloz.trial.colorrecipesexplorer.service.model;
import dev.fyloz.trial.colorrecipesexplorer.exception.CannotDeleteDefaultMaterialTypeException;
import dev.fyloz.trial.colorrecipesexplorer.exception.CannotEditDefaultMaterialTypeException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.ModelException;
import dev.fyloz.trial.colorrecipesexplorer.model.MaterialType;
import dev.fyloz.trial.colorrecipesexplorer.model.dto.MaterialTypeEditorDto;

View File

@ -1,6 +1,6 @@
package dev.fyloz.trial.colorrecipesexplorer.service.model;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.ModelException;
import dev.fyloz.trial.colorrecipesexplorer.model.*;
import dev.fyloz.trial.colorrecipesexplorer.model.dto.MixFormDto;

View File

@ -1,7 +1,7 @@
package dev.fyloz.trial.colorrecipesexplorer.service.model;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.ModelException;
import dev.fyloz.trial.colorrecipesexplorer.model.Material;
import dev.fyloz.trial.colorrecipesexplorer.model.MaterialType;

View File

@ -1,6 +1,6 @@
package dev.fyloz.trial.colorrecipesexplorer.web.controller.thymeleaf;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ModelResponseBuilder;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ResponseCode;
import dev.fyloz.trial.colorrecipesexplorer.service.files.MarkdownFilesService;

View File

@ -1,6 +1,6 @@
package dev.fyloz.trial.colorrecipesexplorer.web.controller.thymeleaf;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.model.Recipe;
import dev.fyloz.trial.colorrecipesexplorer.web.response.JSONResponseBuilder;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ModelResponseBuilder;

View File

@ -1,6 +1,6 @@
package dev.fyloz.trial.colorrecipesexplorer.web.controller.thymeleaf.creators;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.model.Company;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ModelResponseBuilder;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ResponseCode;

View File

@ -1,7 +1,7 @@
//package dev.fyloz.trial.colorrecipesexplorer.web.controller.thymeleaf.creators;
//
//import dev.fyloz.trial.colorrecipesexplorer.exception.SimdutException;
//import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsException;
//import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsException;
//import dev.fyloz.trial.colorrecipesexplorer.web.response.ModelResponseBuilder;
//import dev.fyloz.trial.colorrecipesexplorer.web.response.ResponseCode;
//import dev.fyloz.trial.colorrecipesexplorer.web.response.ResponseDataType;

View File

@ -1,7 +1,7 @@
package dev.fyloz.trial.colorrecipesexplorer.web.controller.thymeleaf.creators
import dev.fyloz.trial.colorrecipesexplorer.exception.SimdutException
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsException
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsException
import dev.fyloz.trial.colorrecipesexplorer.web.response.ResponseCode
import dev.fyloz.trial.colorrecipesexplorer.model.Material
import dev.fyloz.trial.colorrecipesexplorer.model.MaterialSaveDto

View File

@ -1,6 +1,6 @@
package dev.fyloz.trial.colorrecipesexplorer.web.controller.thymeleaf.creators;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.ModelException;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ModelResponseBuilder;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ResponseCode;

View File

@ -1,7 +1,7 @@
package dev.fyloz.trial.colorrecipesexplorer.web.controller.thymeleaf.creators;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.model.MixKt;
import dev.fyloz.trial.colorrecipesexplorer.model.MixTypeKt;
import dev.fyloz.trial.colorrecipesexplorer.model.Recipe;

View File

@ -1,8 +1,8 @@
package dev.fyloz.trial.colorrecipesexplorer.web.controller.thymeleaf.editors;
import dev.fyloz.trial.colorrecipesexplorer.exception.SimdutException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ModelResponseBuilder;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ResponseCode;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ResponseDataType;

View File

@ -1,8 +1,8 @@
package dev.fyloz.trial.colorrecipesexplorer.web.controller.thymeleaf.editors;
import dev.fyloz.trial.colorrecipesexplorer.exception.CannotEditDefaultMaterialTypeException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.ModelException;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ModelResponseBuilder;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ResponseCode;

View File

@ -1,7 +1,7 @@
package dev.fyloz.trial.colorrecipesexplorer.web.controller.thymeleaf.editors;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.model.Mix;
import dev.fyloz.trial.colorrecipesexplorer.model.MixKt;
import dev.fyloz.trial.colorrecipesexplorer.model.MixTypeKt;

View File

@ -1,6 +1,6 @@
package dev.fyloz.trial.colorrecipesexplorer.web.controller.thymeleaf.editors;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.model.Recipe;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ModelResponseBuilder;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ResponseCode;

View File

@ -1,6 +1,6 @@
package dev.fyloz.trial.colorrecipesexplorer.web.controller.thymeleaf.files;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.model.Recipe;
import dev.fyloz.trial.colorrecipesexplorer.web.response.JSONResponseBuilder;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ModelResponseBuilder;

View File

@ -1,6 +1,6 @@
package dev.fyloz.trial.colorrecipesexplorer.web.controller.thymeleaf.files;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.service.files.SimdutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;

View File

@ -1,6 +1,6 @@
package dev.fyloz.trial.colorrecipesexplorer.web.controller.thymeleaf.files;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.service.files.XlsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;

View File

@ -1,7 +1,7 @@
package dev.fyloz.trial.colorrecipesexplorer.web.controller.thymeleaf.removers;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityLinkedException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.model.Company;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ModelResponseBuilder;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ResponseCode;

View File

@ -1,7 +1,7 @@
package dev.fyloz.trial.colorrecipesexplorer.web.controller.thymeleaf.removers;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityLinkedException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ModelResponseBuilder;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ResponseCode;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ResponseDataType;

View File

@ -2,7 +2,7 @@ package dev.fyloz.trial.colorrecipesexplorer.web.controller.thymeleaf.removers;
import dev.fyloz.trial.colorrecipesexplorer.exception.CannotDeleteDefaultMaterialTypeException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityLinkedException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ModelResponseBuilder;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ResponseCode;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ResponseDataType;

View File

@ -1,6 +1,6 @@
package dev.fyloz.trial.colorrecipesexplorer.web.controller.thymeleaf.removers;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.model.Mix;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ModelResponseBuilder;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ResponseCode;

View File

@ -1,6 +1,6 @@
package dev.fyloz.trial.colorrecipesexplorer.web.controller.thymeleaf.removers;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.model.Recipe;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ModelResponseBuilder;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ResponseCode;

View File

@ -1,7 +1,7 @@
package dev.fyloz.trial.colorrecipesexplorer.config
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundRestException
import dev.fyloz.trial.colorrecipesexplorer.model.Employee
import dev.fyloz.trial.colorrecipesexplorer.model.EmployeeLoginRequest
import dev.fyloz.trial.colorrecipesexplorer.model.EmployeePermission

View File

@ -1,9 +1,7 @@
package dev.fyloz.trial.colorrecipesexplorer.exception.model
package dev.fyloz.trial.colorrecipesexplorer.exception
import dev.fyloz.trial.colorrecipesexplorer.exception.RestException
import dev.fyloz.trial.colorrecipesexplorer.exception.model.ModelException
import dev.fyloz.trial.colorrecipesexplorer.model.Model
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.ResponseStatus
class EntityAlreadyExistsException(
modelType: Class<out Model>,
@ -26,12 +24,8 @@ class EntityAlreadyExistsException(
)
}
@ResponseStatus(HttpStatus.CONFLICT)
class EntityAlreadyExistsRestException(val value: Any) :
RestException("An entity with the given identifier already exists", HttpStatus.CONFLICT) {
@Suppress("unused")
override fun buildBody(): RestExceptionBody = object : RestExceptionBody() {
val id = value
}
}
class EntityNotFoundException(
modelType: Class<out Model>,
val identifierType: IdentifierType,
val requestedId: Any
) : ModelException(modelType)

View File

@ -2,6 +2,7 @@ package dev.fyloz.trial.colorrecipesexplorer.exception
import com.fasterxml.jackson.annotation.JsonProperty
import dev.fyloz.trial.colorrecipesexplorer.exception.model.*
import dev.fyloz.trial.colorrecipesexplorer.model.Model
import org.springframework.context.annotation.Profile
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
@ -10,15 +11,51 @@ import org.springframework.validation.FieldError
import org.springframework.web.bind.MethodArgumentNotValidException
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.context.request.WebRequest
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
abstract class RestException(val exceptionMessage: String, val httpStatus: HttpStatus) : RuntimeException(exceptionMessage) {
abstract class RestException(val exceptionMessage: String, val httpStatus: HttpStatus) :
RuntimeException(exceptionMessage) {
abstract fun buildBody(): RestExceptionBody
open inner class RestExceptionBody(val status: Int = httpStatus.value(), @JsonProperty("message") val message: String = exceptionMessage)
open inner class RestExceptionBody(
val status: Int = httpStatus.value(),
@JsonProperty("message") val message: String = exceptionMessage
)
}
@ResponseStatus(HttpStatus.CONFLICT)
class EntityAlreadyExistsRestException(val value: Any) :
RestException("An entity with the given identifier already exists", HttpStatus.CONFLICT) {
@Suppress("unused")
override fun buildBody(): RestExceptionBody = object : RestExceptionBody() {
val id = value
}
}
@ResponseStatus(HttpStatus.NOT_FOUND)
class EntityNotFoundRestException(val value: Any) :
RestException("An entity could not be found with the given identifier", HttpStatus.NOT_FOUND) {
@Suppress("unused")
override fun buildBody(): RestExceptionBody = object : RestExceptionBody() {
val id = value
}
}
@ResponseStatus(HttpStatus.CONFLICT)
class CannotDeleteEntityRestException(val value: Long) :
RestException(
"The entity with the given identifier could not be deleted because it is required by other entities",
HttpStatus.CONFLICT
) {
@Suppress("unused")
override fun buildBody(): RestExceptionBody = object : RestExceptionBody() {
val id = value
}
}
@ControllerAdvice
@Profile("rest")
class RestResponseEntityExceptionHandler : ResponseEntityExceptionHandler() {
@ -32,7 +69,12 @@ class RestResponseEntityExceptionHandler : ResponseEntityExceptionHandler() {
return handleExceptionInternal(exception, exception.buildBody(), HttpHeaders(), exception.httpStatus, request)
}
override fun handleMethodArgumentNotValid(ex: MethodArgumentNotValidException, headers: HttpHeaders, status: HttpStatus, request: WebRequest): ResponseEntity<Any> {
override fun handleMethodArgumentNotValid(
ex: MethodArgumentNotValidException,
headers: HttpHeaders,
status: HttpStatus,
request: WebRequest
): ResponseEntity<Any> {
val errors = hashMapOf<String, String>()
ex.bindingResult.allErrors.forEach {
val fieldName = (it as FieldError).field

View File

@ -1,7 +1,18 @@
package dev.fyloz.trial.colorrecipesexplorer.repository
import dev.fyloz.trial.colorrecipesexplorer.model.Company
import org.springframework.data.jpa.repository.Query
import org.springframework.stereotype.Repository
@Repository
interface CompanyRepository : NamedJpaRepository<Company>
interface CompanyRepository : NamedJpaRepository<Company> {
@Query(
"""
select case when(count(r.id) > 0) then false else true end
from Company c
left join Recipe r on c.id = r.company.id
where c.id = :id
"""
)
fun canBeDeleted(id: Long): Boolean
}

View File

@ -2,6 +2,7 @@ package dev.fyloz.trial.colorrecipesexplorer.repository
import dev.fyloz.trial.colorrecipesexplorer.model.Material
import dev.fyloz.trial.colorrecipesexplorer.model.MaterialType
import org.springframework.data.jpa.repository.Query
import org.springframework.stereotype.Repository
@Repository
@ -11,4 +12,15 @@ interface MaterialRepository : NamedJpaRepository<Material> {
/** Gets all the materials with the given [materialType]. */
fun findAllByMaterialType(materialType: MaterialType): Collection<Material>
@Query(
"""
select case when(count(mm.id) + count(mt.id) > 0) then false else true end
from Material m
left join MixMaterial mm on m.id = mm.material.id
left join MixType mt on m.id = mt.material.id
where m.id = :id
"""
)
fun canBeDeleted(id: Long): Boolean
}

View File

@ -1,6 +1,7 @@
package dev.fyloz.trial.colorrecipesexplorer.repository
import dev.fyloz.trial.colorrecipesexplorer.model.MaterialType
import org.springframework.data.jpa.repository.Query
import org.springframework.stereotype.Repository
@Repository
@ -13,4 +14,14 @@ interface MaterialTypeRepository : NamedJpaRepository<MaterialType> {
/** Gets the material type with the given [prefix]. */
fun findByPrefix(prefix: String): MaterialType?
@Query(
"""
select case when(count(m.id) > 0) then false else true end
from MaterialType t
left join Material m on t.id = m.materialType.id
where t.id = :id
"""
)
fun canBeDeleted(id: Long): Boolean
}

View File

@ -3,8 +3,19 @@ package dev.fyloz.trial.colorrecipesexplorer.repository
import dev.fyloz.trial.colorrecipesexplorer.model.Mix
import dev.fyloz.trial.colorrecipesexplorer.model.MixType
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
interface MixRepository : JpaRepository<Mix, Long> {
/** Finds all mixes with the given [mixType]. */
fun findAllByMixType(mixType: MixType): Collection<Mix>
@Query(
"""
select case when(count(mm.id) > 0) then false else true end
from Mix m
left join MixMaterial mm on m.mixType.material.id = mm.material.id
where m.id = :id
"""
)
fun canBeDeleted(id: Long): Boolean
}

View File

@ -2,10 +2,21 @@ package dev.fyloz.trial.colorrecipesexplorer.repository
import dev.fyloz.trial.colorrecipesexplorer.model.Material
import dev.fyloz.trial.colorrecipesexplorer.model.MixType
import org.springframework.data.jpa.repository.Query
import org.springframework.stereotype.Repository
@Repository
interface MixTypeRepository : NamedJpaRepository<MixType> {
/** Gets the mix type with the given [material]. */
fun findByMaterial(material: Material): MixType?
@Query(
"""
select case when(count(m.id) > 0) then false else true end
from MixType t
left join Mix m on t.id = m.mixType.id
where t.id = :id
"""
)
fun canBeDeleted(id: Long): Boolean
}

View File

@ -2,9 +2,9 @@ package dev.fyloz.trial.colorrecipesexplorer.service
import dev.fyloz.trial.colorrecipesexplorer.config.blacklistedJwtTokens
import dev.fyloz.trial.colorrecipesexplorer.config.defaultGroupCookieName
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundException
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundRestException
import dev.fyloz.trial.colorrecipesexplorer.model.*
import dev.fyloz.trial.colorrecipesexplorer.model.validation.or
import dev.fyloz.trial.colorrecipesexplorer.repository.EmployeeGroupRepository

View File

@ -1,5 +1,6 @@
package dev.fyloz.trial.colorrecipesexplorer.service
import dev.fyloz.trial.colorrecipesexplorer.exception.CannotDeleteEntityRestException
import dev.fyloz.trial.colorrecipesexplorer.model.Company
import dev.fyloz.trial.colorrecipesexplorer.model.CompanySaveDto
import dev.fyloz.trial.colorrecipesexplorer.model.CompanyUpdateDto
@ -30,4 +31,9 @@ class CompanyServiceImpl(companyRepository: CompanyRepository, val recipeService
)
})
}
override fun deleteById(id: Long) {
if (!repository.canBeDeleted(id)) throw CannotDeleteEntityRestException(id)
super.deleteById(id)
}
}

View File

@ -1,5 +1,6 @@
package dev.fyloz.trial.colorrecipesexplorer.service
import dev.fyloz.trial.colorrecipesexplorer.exception.CannotDeleteEntityRestException
import dev.fyloz.trial.colorrecipesexplorer.model.*
import dev.fyloz.trial.colorrecipesexplorer.repository.MaterialRepository
import dev.fyloz.trial.colorrecipesexplorer.service.files.SimdutService
@ -90,4 +91,9 @@ class MaterialServiceImpl(
private fun assertPersistedMaterial(material: Material) {
Assert.notNull(material.name, "The persisted material with the id ${material.id} has a null name")
}
override fun deleteById(id: Long) {
if (!repository.canBeDeleted(id)) throw CannotDeleteEntityRestException(id)
super.deleteById(id)
}
}

View File

@ -1,8 +1,9 @@
package dev.fyloz.trial.colorrecipesexplorer.service
import dev.fyloz.trial.colorrecipesexplorer.config.properties.MaterialTypeProperties
import dev.fyloz.trial.colorrecipesexplorer.exception.CannotDeleteEntityRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.RestException
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsRestException
import dev.fyloz.trial.colorrecipesexplorer.model.MaterialType
import dev.fyloz.trial.colorrecipesexplorer.model.MaterialTypeSaveDto
import dev.fyloz.trial.colorrecipesexplorer.model.MaterialTypeUpdateDto
@ -73,11 +74,9 @@ class MaterialTypeServiceImpl(repository: MaterialTypeRepository, private val ma
return super<AbstractExternalNamedModelService>.update(entity)
}
override fun delete(entity: MaterialType) {
if (isUsedByMaterial(entity))
throw CannotDeleteUsedMaterialTypeRestException()
super.delete(entity)
override fun deleteById(id: Long) {
if (!repository.canBeDeleted(id)) throw CannotDeleteEntityRestException(id)
super.deleteById(id)
}
override fun saveSystemTypes(systemTypeProperties: Collection<MaterialTypeProperties.MaterialTypeProperty>) {

View File

@ -1,5 +1,6 @@
package dev.fyloz.trial.colorrecipesexplorer.service
import dev.fyloz.trial.colorrecipesexplorer.exception.CannotDeleteEntityRestException
import dev.fyloz.trial.colorrecipesexplorer.model.*
import dev.fyloz.trial.colorrecipesexplorer.repository.MixRepository
import org.springframework.context.annotation.Lazy
@ -110,4 +111,9 @@ class MixServiceImpl(
recipeService.removeMix(entity)
super.delete(entity)
}
override fun deleteById(id: Long) {
if (!repository.canBeDeleted(id)) throw CannotDeleteEntityRestException(id)
super.deleteById(id)
}
}

View File

@ -1,7 +1,8 @@
package dev.fyloz.trial.colorrecipesexplorer.service
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.CannotDeleteEntityRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundRestException
import dev.fyloz.trial.colorrecipesexplorer.model.*
import dev.fyloz.trial.colorrecipesexplorer.repository.MixTypeRepository
import org.springframework.context.annotation.Lazy
@ -53,4 +54,9 @@ class MixTypeServiceImpl(
material.name = name
material.materialType = materialType
})
override fun deleteById(id: Long) {
if (!repository.canBeDeleted(id)) throw CannotDeleteEntityRestException(id)
super.deleteById(id)
}
}

View File

@ -1,6 +1,6 @@
package dev.fyloz.trial.colorrecipesexplorer.service
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundRestException
import dev.fyloz.trial.colorrecipesexplorer.model.*
import dev.fyloz.trial.colorrecipesexplorer.model.validation.isNotNullAndNotBlank
import dev.fyloz.trial.colorrecipesexplorer.model.validation.or

View File

@ -1,8 +1,8 @@
package dev.fyloz.trial.colorrecipesexplorer.service
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundRestException
import dev.fyloz.trial.colorrecipesexplorer.model.EntityDto
import dev.fyloz.trial.colorrecipesexplorer.model.Model
import dev.fyloz.trial.colorrecipesexplorer.model.NamedModel

View File

@ -1,8 +1,8 @@
package dev.fyloz.trial.colorrecipesexplorer.service
import com.nhaarman.mockitokotlin2.*
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundRestException
import dev.fyloz.trial.colorrecipesexplorer.model.EntityDto
import dev.fyloz.trial.colorrecipesexplorer.model.Model
import dev.fyloz.trial.colorrecipesexplorer.model.NamedModel

View File

@ -2,8 +2,8 @@ package dev.fyloz.trial.colorrecipesexplorer.service
import com.nhaarman.mockitokotlin2.*
import dev.fyloz.trial.colorrecipesexplorer.config.defaultGroupCookieName
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundRestException
import dev.fyloz.trial.colorrecipesexplorer.model.*
import dev.fyloz.trial.colorrecipesexplorer.repository.EmployeeGroupRepository
import dev.fyloz.trial.colorrecipesexplorer.repository.EmployeeRepository

View File

@ -52,4 +52,10 @@ class CompanyServiceTest :
override fun `update(dto) calls and returns update() with the created entity`() =
withBaseUpdateDtoTest(entity, entityUpdateDto, service, any())
// delete()
override fun `deleteById() deletes the entity with the given id in the repository`() {
super.`deleteById() deletes the entity with the given id in the repository`()
}
}

View File

@ -1,12 +1,11 @@
package dev.fyloz.trial.colorrecipesexplorer.service
import com.nhaarman.mockitokotlin2.*
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsRestException
import dev.fyloz.trial.colorrecipesexplorer.model.*
import dev.fyloz.trial.colorrecipesexplorer.repository.MaterialRepository
import dev.fyloz.trial.colorrecipesexplorer.service.files.SimdutService
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.springframework.mock.web.MockMultipartFile
@ -191,6 +190,12 @@ class MaterialServiceTest :
verify(simdutService).update(eq(mockSimdutFile), any())
}
// delete()
override fun `deleteById() deletes the entity with the given id in the repository`() {
super.`deleteById() deletes the entity with the given id in the repository`()
}
/** Helper function to replace collections.in because the id is not considered in the equals function of Material while Thymeleaf is supported. */
private infix fun Collection<Material>.contains(material: Material): Boolean =
any { it.id == material.id }

View File

@ -1,12 +1,11 @@
package dev.fyloz.trial.colorrecipesexplorer.service
import com.nhaarman.mockitokotlin2.*
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundRestException
import dev.fyloz.trial.colorrecipesexplorer.model.*
import dev.fyloz.trial.colorrecipesexplorer.repository.MaterialTypeRepository
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import kotlin.test.assertEquals
@ -167,10 +166,7 @@ class MaterialTypeServiceTest :
verify(repository).delete(entity)
}
@Test
fun `delete() throws CannotDeleteUsedMaterialTypeRestException when the material type is in use`() {
doReturn(true).whenever(service).isUsedByMaterial(entity)
assertThrows<CannotDeleteUsedMaterialTypeRestException> { service.delete(entity) }
override fun `deleteById() deletes the entity with the given id in the repository`() {
super.`deleteById() deletes the entity with the given id in the repository`()
}
}

View File

@ -191,6 +191,12 @@ class MixServiceTest : AbstractExternalModelServiceTest<Mix, MixSaveDto, MixUpda
verify(service).update(expected)
}
// delete()
override fun `deleteById() deletes the entity with the given id in the repository`() {
super.`deleteById() deletes the entity with the given id in the repository`()
}
}
data class MixUpdateDtoTestScope(

View File

@ -1,12 +1,11 @@
package dev.fyloz.trial.colorrecipesexplorer.service
import com.nhaarman.mockitokotlin2.*
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityAlreadyExistsRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityAlreadyExistsRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundRestException
import dev.fyloz.trial.colorrecipesexplorer.model.*
import dev.fyloz.trial.colorrecipesexplorer.repository.MixTypeRepository
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import kotlin.test.assertEquals
@ -97,4 +96,11 @@ class MixTypeServiceTest : AbstractNamedModelServiceTest<MixType, MixTypeService
assertEquals(materialType, found.material.materialType)
assertTrue(found.material.isMixType)
}
// delete()
override fun `deleteById() deletes the entity with the given id in the repository`() {
whenever(repository.canBeDeleted(any())).doReturn(true)
super.`deleteById() deletes the entity with the given id in the repository`()
}
}

View File

@ -1,7 +1,7 @@
package dev.fyloz.trial.colorrecipesexplorer.service
import com.nhaarman.mockitokotlin2.*
import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundRestException
import dev.fyloz.trial.colorrecipesexplorer.exception.EntityNotFoundRestException
import dev.fyloz.trial.colorrecipesexplorer.model.*
import dev.fyloz.trial.colorrecipesexplorer.repository.RecipeRepository
import dev.fyloz.trial.colorrecipesexplorer.service.files.FilesService