implements HsOfficePartnerController.patch
This commit is contained in:
@ -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));
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user