introduce-booking-module #41

Merged
hsh-michaelhoennig merged 21 commits from introduce-booking-module into master 2024-04-16 11:21:35 +02:00
3 changed files with 7 additions and 33 deletions
Showing only changes of commit 1d0121f79d - Show all commits

View File

@ -157,7 +157,6 @@ openapiProcessor {
targetDir "$buildDir/generated/sources/openapi-javax" targetDir "$buildDir/generated/sources/openapi-javax"
showWarnings true showWarnings true
openApiNullable true openApiNullable true
// generateAliasAsModel true
} }
} }
sourceSets.main.java.srcDir 'build/generated/sources/openapi' sourceSets.main.java.srcDir 'build/generated/sources/openapi'

View File

@ -3,14 +3,10 @@ package net.hostsharing.hsadminng.hs.booking.item;
import net.hostsharing.hsadminng.hs.booking.generated.api.v1.model.HsBookingItemPatchResource; import net.hostsharing.hsadminng.hs.booking.generated.api.v1.model.HsBookingItemPatchResource;
import net.hostsharing.hsadminng.mapper.EntityPatcher; import net.hostsharing.hsadminng.mapper.EntityPatcher;
import net.hostsharing.hsadminng.mapper.OptionalFromJson; import net.hostsharing.hsadminng.mapper.OptionalFromJson;
import org.apache.commons.lang3.tuple.ImmutablePair;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import static java.util.Arrays.stream;
public class HsBookingItemEntityPatcher implements EntityPatcher<HsBookingItemPatchResource> { public class HsBookingItemEntityPatcher implements EntityPatcher<HsBookingItemPatchResource> {
private final HsBookingItemEntity entity; private final HsBookingItemEntity entity;
@ -24,40 +20,19 @@ public class HsBookingItemEntityPatcher implements EntityPatcher<HsBookingItemPa
OptionalFromJson.of(resource.getCaption()) OptionalFromJson.of(resource.getCaption())
.ifPresent(entity::setCaption); .ifPresent(entity::setCaption);
Optional.ofNullable(resource.getResources()) Optional.ofNullable(resource.getResources())
.ifPresent(r -> entity.getResources().patch(objectToMap(resource.getResources()))); .ifPresent(r -> entity.getResources().patch(castToKeyValueMap(resource.getResources())));
OptionalFromJson.of(resource.getValidFrom()) OptionalFromJson.of(resource.getValidFrom())
.ifPresent(entity::setValidFrom); .ifPresent(entity::setValidFrom);
OptionalFromJson.of(resource.getValidTo()) OptionalFromJson.of(resource.getValidTo())
.ifPresent(entity::setValidTo); .ifPresent(entity::setValidTo);
} }
static Map<String, Object> objectToMap(final Object obj) {
if (obj instanceof Map<?, ?>) {
return toKeyValueMap(obj);
}
return stream(obj.getClass().getDeclaredFields())
.map(field -> {
try {
field.setAccessible(true);
return new ImmutablePair<>(field.getName(), field.get(obj));
} catch (final IllegalAccessException exc) {
throw new RuntimeException(exc);
}
})
.reduce(
new HashMap<>(),
(map, pair) -> {
map.put(pair.getKey(), pair.getValue());
return map;
},
(map1, map2) -> {
map1.putAll(map2);
return map1;
});
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private static Map<String, Object> toKeyValueMap(final Object obj) { static Map<String, Object> castToKeyValueMap(final Object obj) {
if (obj instanceof Map<?, ?>) {
return (Map<String, Object>) obj; return (Map<String, Object>) obj;
} }
throw new ClassCastException("Map expected, but got: " + obj);
}
} }

View File

@ -16,7 +16,7 @@ import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Stream; import java.util.stream.Stream;
import static net.hostsharing.hsadminng.hs.booking.item.HsBookingItemEntityPatcher.objectToMap; import static net.hostsharing.hsadminng.hs.booking.item.HsBookingItemEntityPatcher.castToKeyValueMap;
import static net.hostsharing.hsadminng.hs.office.debitor.TestHsOfficeDebitor.TEST_DEBITOR; import static net.hostsharing.hsadminng.hs.office.debitor.TestHsOfficeDebitor.TEST_DEBITOR;
import static net.hostsharing.hsadminng.mapper.PatchMap.entry; import static net.hostsharing.hsadminng.mapper.PatchMap.entry;
import static net.hostsharing.hsadminng.mapper.PatchMap.patchMap; import static net.hostsharing.hsadminng.mapper.PatchMap.patchMap;
@ -72,7 +72,7 @@ class HsBookingItemEntityPatcherUnitTest extends PatchUnitTestBase<
final var entity = new HsBookingItemEntity(); final var entity = new HsBookingItemEntity();
entity.setUuid(INITIAL_BOOKING_ITEM_UUID); entity.setUuid(INITIAL_BOOKING_ITEM_UUID);
entity.setDebitor(TEST_DEBITOR); entity.setDebitor(TEST_DEBITOR);
entity.getResources().putAll(objectToMap(INITIAL_RESOURCES)); entity.getResources().putAll(castToKeyValueMap(INITIAL_RESOURCES));
entity.setCaption(INITIAL_CAPTION); entity.setCaption(INITIAL_CAPTION);
entity.setValidity(Range.closedInfinite(GIVEN_VALID_FROM)); entity.setValidity(Range.closedInfinite(GIVEN_VALID_FROM));
return entity; return entity;