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);
}
}

View File

@@ -12,5 +12,5 @@ map:
- type: string:uuid => java.util.UUID
paths:
/api/hs/office/partners/{packageUUID}:
/api/hs/office/partners/{partnerUUID}:
null: org.openapitools.jackson.nullable.JsonNullable

View File

@@ -3,58 +3,89 @@ components:
schemas:
HsOfficePartnerBase:
HsOfficePartner:
type: object
properties:
uuid:
type: string
format: uuid
person:
$ref: './hs-office-person-schemas.yaml#/components/schemas/HsOfficePerson'
contact:
$ref: './hs-office-contact-schemas.yaml#/components/schemas/HsOfficeContact'
registrationOffice:
type: string
registrationNumber:
type: string
birthName:
type: string
birthday:
type: string
format: date
dateOfDeath:
type: string
format: date
HsOfficePartnerPatch:
type: object
properties:
personUuid:
type: string
format: uuid
nullable: true
contactUuid:
type: string
format: uuid
nullable: true
registrationOffice:
type: string
nullable: true
registrationNumber:
type: string
nullable: true
birthName:
type: string
nullable: true
birthday:
type: string
format: date
nullable: true
dateOfDeath:
type: string
format: date
HsOfficePartner:
allOf:
- type: object
properties:
uuid:
type: string
format: uuid
person:
$ref: './hs-office-person-schemas.yaml#/components/schemas/HsOfficePerson'
contact:
$ref: './hs-office-contact-schemas.yaml#/components/schemas/HsOfficeContact'
- $ref: '#/components/schemas/HsOfficePartnerBase'
HsOfficePartnerPatch:
allOf:
- type: object
properties:
personUuid:
type: string
format: uuid
contactUuid:
type: string
format: uuid
- $ref: '#/components/schemas/HsOfficePartnerBase'
nullable: true
HsOfficePartnerInsert:
allOf:
- type: object
properties:
personUuid:
type: string
format: uuid
contactUuid:
type: string
format: uuid
- required:
- personUuid
- contactUuid
- $ref: '#/components/schemas/HsOfficePartnerBase'
type: object
properties:
personUuid:
type: string
format: uuid
contactUuid:
type: string
format: uuid
registrationOffice:
type: string
nullable: true
registrationNumber:
type: string
nullable: true
birthName:
type: string
nullable: true
birthday:
type: string
format: date
nullable: true
dateOfDeath:
type: string
format: date
nullable: true
required:
- personUuid
- contactUuid
- registrationOffice
- registrationNumber
- birthName
- birthday
- dateOfDeath

View File

@@ -29,7 +29,8 @@ get:
patch:
tags:
- hs-office-partners
operationId: updatePartner
description: 'Updates a single business partner by its uuid, if permitted for the current subject.'
operationId: patchPartner
parameters:
- $ref: './auth.yaml#/components/parameters/currentUser'
- $ref: './auth.yaml#/components/parameters/assumedRoles'