1
0

add HsOfficeMembershipEntityPatcher

This commit is contained in:
Michael Hoennig
2022-10-22 10:40:59 +02:00
parent 8eab3b7cd5
commit b3b70aaaf4
18 changed files with 231 additions and 59 deletions

View File

@ -1,12 +1,12 @@
package net.hostsharing.hsadminng.hs.office.membership;
import com.vladmihalcea.hibernate.type.range.Range;
import net.hostsharing.hsadminng.mapper.Mapper;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficeMembershipsApi;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeMembershipInsertResource;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeMembershipPatchResource;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeMembershipResource;
import net.hostsharing.hsadminng.hs.office.partner.HsOfficePartnerEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
@ -15,13 +15,13 @@ import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBui
import javax.persistence.EntityManager;
import javax.validation.Valid;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.function.BiConsumer;
import static net.hostsharing.hsadminng.mapper.Mapper.map;
import static net.hostsharing.hsadminng.mapper.Mapper.mapList;
import static net.hostsharing.hsadminng.mapper.PostgresDateRange.toPostgresDateRange;
@RestController
@ -48,7 +48,7 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi {
final var entities =
membershipRepo.findMembershipsByOptionalPartnerUuidAndOptionalMemberNumber(partnerUuid, memberNumber);
final var resources = Mapper.mapList(entities, HsOfficeMembershipResource.class,
final var resources = mapList(entities, HsOfficeMembershipResource.class,
SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER);
return ResponseEntity.ok(resources);
}
@ -62,7 +62,7 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi {
context.define(currentUser, assumedRoles);
final var entityToSave = map(body, HsOfficeMembershipEntity.class, SEPA_MANDATE_RESOURCE_TO_ENTITY_POSTMAPPER);
final var entityToSave = mapX(body, HsOfficeMembershipEntity.class);
entityToSave.setUuid(UUID.randomUUID());
final var saved = membershipRepo.save(entityToSave);
@ -122,25 +122,13 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi {
final var current = membershipRepo.findByUuid(membershipUuid).orElseThrow();
current.setValidity(toPostgresDateRange(current.getValidity().lower(), body.getValidTo()));
// current.setReasonForTermination(HsOfficeReasonForTermination.valueOf(body.getReasonForTermination().name()));
current.setReasonForTermination(
Optional.ofNullable(body.getReasonForTermination()).map(Enum::name).map(HsOfficeReasonForTermination::valueOf).orElse(current.getReasonForTermination())
);
new HsOfficeMembershipEntityPatcher(em, current).apply(body);
final var saved = membershipRepo.save(current);
final var mapped = map(saved, HsOfficeMembershipResource.class, SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER);
return ResponseEntity.ok(mapped);
}
private static Range<LocalDate> toPostgresDateRange(
final LocalDate validFrom,
final LocalDate validTo) {
return validTo != null
? Range.closedOpen(validFrom, validTo.plusDays(1))
: Range.closedInfinite(validFrom);
}
final BiConsumer<HsOfficeMembershipEntity, HsOfficeMembershipResource> SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> {
resource.setValidFrom(entity.getValidity().lower());
if (entity.getValidity().hasUpperBound()) {
@ -148,7 +136,15 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi {
}
};
final BiConsumer<HsOfficeMembershipInsertResource, HsOfficeMembershipEntity> SEPA_MANDATE_RESOURCE_TO_ENTITY_POSTMAPPER = (resource, entity) -> {
private HsOfficeMembershipEntity mapX(
final HsOfficeMembershipInsertResource resource,
final Class<HsOfficeMembershipEntity> entityClass) {
final var entity = new HsOfficeMembershipEntity();
entity.setPartner(em.getReference(HsOfficePartnerEntity.class, resource.getPartnerUuid()));
entity.setMainDebitor(em.getReference(HsOfficeDebitorEntity.class, resource.getMainDebitorUuid()));
entity.setMemberNumber(resource.getMemberNumber());
entity.setValidity(toPostgresDateRange(resource.getValidFrom(), resource.getValidTo()));
};
entity.setReasonForTermination(map(resource.getReasonForTermination(), HsOfficeReasonForTermination.class));
return entity;
}
}

View File

@ -18,6 +18,7 @@ import javax.persistence.*;
import java.time.LocalDate;
import java.util.UUID;
import static net.hostsharing.hsadminng.mapper.PostgresDateRange.toPostgresDateRange;
import static net.hostsharing.hsadminng.stringify.Stringify.stringify;
@Entity
@ -69,6 +70,10 @@ public class HsOfficeMembershipEntity implements Stringifyable {
@Type(type = "pgsql_enum")
private HsOfficeReasonForTermination reasonForTermination;
public void setValidTo(final LocalDate validTo) {
validity = toPostgresDateRange(getValidity().lower(), validTo);
}
@Override
public String toString() {
return stringify.apply(this);

View File

@ -0,0 +1,45 @@
package net.hostsharing.hsadminng.hs.office.membership;
import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeMembershipPatchResource;
import net.hostsharing.hsadminng.mapper.EntityPatcher;
import net.hostsharing.hsadminng.mapper.OptionalFromJson;
import javax.persistence.EntityManager;
import java.util.Optional;
import java.util.UUID;
import static net.hostsharing.hsadminng.mapper.Mapper.map;
public class HsOfficeMembershipEntityPatcher implements EntityPatcher<HsOfficeMembershipPatchResource> {
private final EntityManager em;
private final HsOfficeMembershipEntity entity;
public HsOfficeMembershipEntityPatcher(
final EntityManager em,
final HsOfficeMembershipEntity entity) {
this.em = em;
this.entity = entity;
}
@Override
public void apply(final HsOfficeMembershipPatchResource resource) {
OptionalFromJson.of(resource.getMainDebitorUuid())
.ifPresent(newValue -> {
verifyNotNull(newValue, "debitor");
entity.setMainDebitor(em.getReference(HsOfficeDebitorEntity.class, newValue));
});
OptionalFromJson.of(resource.getValidTo()).ifPresent(
entity::setValidTo);
Optional.ofNullable(resource.getReasonForTermination())
.map(v -> map(v, HsOfficeReasonForTermination.class))
.ifPresent(entity::setReasonForTermination);
}
private void verifyNotNull(final UUID newValue, final String propertyName) {
if (newValue == null) {
throw new IllegalArgumentException("property '" + propertyName + "' must not be null");
}
}
}

View File

@ -8,13 +8,11 @@ import javax.persistence.EntityManager;
class HsOfficePartnerDetailsEntityPatcher implements EntityPatcher<HsOfficePartnerDetailsPatchResource> {
private final EntityManager em;
private final HsOfficePartnerDetailsEntity entity;
HsOfficePartnerDetailsEntityPatcher(
final EntityManager em,
final HsOfficePartnerDetailsEntity entity) {
this.em = em;
this.entity = entity;
}

View File

@ -21,6 +21,7 @@ import java.util.UUID;
import java.util.function.BiConsumer;
import static net.hostsharing.hsadminng.mapper.Mapper.map;
import static net.hostsharing.hsadminng.mapper.PostgresDateRange.toPostgresDateRange;
@RestController
@ -126,14 +127,6 @@ public class HsOfficeSepaMandateController implements HsOfficeSepaMandatesApi {
return ResponseEntity.ok(mapped);
}
private static Range<LocalDate> toPostgresDateRange(
final LocalDate validFrom,
final LocalDate validTo) {
return validTo != null
? Range.closedOpen(validFrom, validTo.plusDays(1))
: Range.closedInfinite(validFrom);
}
final BiConsumer<HsOfficeSepaMandateEntity, HsOfficeSepaMandateResource> SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> {
resource.setValidFrom(entity.getValidity().lower());
if (entity.getValidity().hasUpperBound()) {