intoduce Mapper.withMatchingStrategy
This commit is contained in:
parent
6225eb3bf6
commit
087d59b238
@ -23,6 +23,7 @@ import java.util.UUID;
|
|||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
import static net.hostsharing.hsadminng.mapper.PostgresDateRange.toPostgresDateRange;
|
import static net.hostsharing.hsadminng.mapper.PostgresDateRange.toPostgresDateRange;
|
||||||
|
import static org.modelmapper.convention.MatchingStrategies.STRICT;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class HsBookingItemController implements HsBookingItemsApi {
|
public class HsBookingItemController implements HsBookingItemsApi {
|
||||||
@ -62,7 +63,8 @@ public class HsBookingItemController implements HsBookingItemsApi {
|
|||||||
|
|
||||||
context.define(currentSubject, assumedRoles);
|
context.define(currentSubject, assumedRoles);
|
||||||
|
|
||||||
final var entityToSave = mapper.map(body, HsBookingItemRbacEntity.class, RESOURCE_TO_ENTITY_POSTMAPPER);
|
final var entityToSave = mapper.withMatchingStrategy(STRICT)
|
||||||
|
.map(body, HsBookingItemRbacEntity.class, RESOURCE_TO_ENTITY_POSTMAPPER);
|
||||||
final var mapped = new BookingItemEntitySaveProcessor(em, entityToSave)
|
final var mapped = new BookingItemEntitySaveProcessor(em, entityToSave)
|
||||||
.preprocessEntity()
|
.preprocessEntity()
|
||||||
.validateEntity()
|
.validateEntity()
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package net.hostsharing.hsadminng.mapper;
|
package net.hostsharing.hsadminng.mapper;
|
||||||
|
|
||||||
import org.modelmapper.ModelMapper;
|
import org.modelmapper.ModelMapper;
|
||||||
|
import org.modelmapper.spi.MatchingStrategy;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
import jakarta.persistence.EntityManager;
|
import jakarta.persistence.EntityManager;
|
||||||
@ -15,7 +16,6 @@ import java.util.stream.Stream;
|
|||||||
|
|
||||||
import static java.util.Arrays.stream;
|
import static java.util.Arrays.stream;
|
||||||
import static net.hostsharing.hsadminng.errors.DisplayAs.DisplayName;
|
import static net.hostsharing.hsadminng.errors.DisplayAs.DisplayName;
|
||||||
import static org.modelmapper.convention.MatchingStrategies.STRICT;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A nicer API for ModelMapper.
|
* A nicer API for ModelMapper.
|
||||||
@ -26,8 +26,17 @@ public class Mapper extends ModelMapper {
|
|||||||
EntityManager em;
|
EntityManager em;
|
||||||
|
|
||||||
public Mapper() {
|
public Mapper() {
|
||||||
// make sure that resource.whateverUuid does not get mapped to entity.uuid, if resource.uuid does not exist
|
getConfiguration().setAmbiguityIgnored(true);
|
||||||
getConfiguration().setMatchingStrategy(STRICT);
|
}
|
||||||
|
|
||||||
|
/** Use STRICT to make sure that resource.whateverUuid does not accidentally get mapped to entity.uuid,
|
||||||
|
* if resource.uuid does not exist
|
||||||
|
*
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Mapper withMatchingStrategy(final MatchingStrategy matchingStrategy) {
|
||||||
|
getConfiguration().setMatchingStrategy(matchingStrategy);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <S, T> List<T> mapList(final List<S> source, final Class<T> targetClass) {
|
public <S, T> List<T> mapList(final List<S> source, final Class<T> targetClass) {
|
||||||
|
Loading…
Reference in New Issue
Block a user