1
0

implements HsOfficePartnerController.patch

This commit is contained in:
Michael Hoennig
2022-09-14 17:51:04 +02:00
parent 68c3375a08
commit bad7d146fa
8 changed files with 544 additions and 58 deletions

View File

@ -1,6 +1,7 @@
package net.hostsharing.hsadminng.hs.office.partner;
import net.hostsharing.hsadminng.Mapper;
import net.hostsharing.hsadminng.OptionalFromJson;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactRepository;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficePartnersApi;
@ -114,14 +115,24 @@ public class HsOfficePartnerController implements HsOfficePartnersApi {
@Override
@Transactional
public ResponseEntity<HsOfficePartnerResource> updatePartner(
public ResponseEntity<HsOfficePartnerResource> patchPartner(
final String currentUser,
final String assumedRoles,
final UUID partnerUuid,
final HsOfficePartnerPatchResource body) {
return null;
context.define(currentUser, assumedRoles);
final var current = partnerRepo.findByUuid(partnerUuid).orElseThrow();
new HsOfficePartnerEntityPatch(current, contactRepo::findByUuid, personRepo::findByUuid).apply(body);
final var saved = partnerRepo.save(current);
final var mapped = map(saved, HsOfficePartnerResource.class);
return ResponseEntity.ok(mapped);
}
final BiConsumer<HsOfficePartnerEntity, HsOfficePartnerResource> PARTNER_ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> {
resource.setPerson(map(entity.getPerson(), HsOfficePersonResource.class));
resource.setContact(map(entity.getContact(), HsOfficeContactResource.class));

View File

@ -0,0 +1,45 @@
package net.hostsharing.hsadminng.hs.office.partner;
import net.hostsharing.hsadminng.OptionalFromJson;
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactEntity;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePartnerPatchResource;
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonEntity;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;
class HsOfficePartnerEntityPatch {
private final HsOfficePartnerEntity entity;
private final Function<UUID, Optional<HsOfficeContactEntity>> fetchContact;
private final Function<UUID, Optional<HsOfficePersonEntity>> fetchPerson;
HsOfficePartnerEntityPatch(
final HsOfficePartnerEntity entity,
final Function<UUID, Optional<HsOfficeContactEntity>> fetchContact,
final Function<UUID, Optional<HsOfficePersonEntity>> fetchPerson) {
this.entity = entity;
this.fetchContact = fetchContact;
this.fetchPerson = fetchPerson;
}
void apply(final HsOfficePartnerPatchResource resource) {
OptionalFromJson.of(resource.getContactUuid()).ifPresent(newValue -> {
entity.setContact(fetchContact.apply(newValue).orElseThrow(
() -> new NoSuchElementException("cannot find contact uuid " + newValue)
));
});
OptionalFromJson.of(resource.getPersonUuid()).ifPresent(newValue -> {
entity.setPerson(fetchPerson.apply(newValue).orElseThrow(
() -> new NoSuchElementException("cannot find person uuid " + newValue)
));
});
OptionalFromJson.of(resource.getRegistrationOffice()).ifPresent(entity::setRegistrationOffice);
OptionalFromJson.of(resource.getRegistrationNumber()).ifPresent(entity::setRegistrationNumber);
OptionalFromJson.of(resource.getBirthday()).ifPresent(entity::setBirthday);
OptionalFromJson.of(resource.getBirthName()).ifPresent(entity::setBirthName);
OptionalFromJson.of(resource.getDateOfDeath()).ifPresent(entity::setDateOfDeath);
}
}