1
0

implements HsOfficePersonController

This commit is contained in:
Michael Hoennig
2022-09-21 09:44:09 +02:00
parent 0bab27d723
commit 00174e4c4a
9 changed files with 895 additions and 17 deletions

View File

@@ -0,0 +1,118 @@
package net.hostsharing.hsadminng.hs.office.person;
import net.hostsharing.hsadminng.Mapper;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficePersonsApi;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePersonInsertResource;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePersonPatchResource;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePersonResource;
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonEntityPatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import java.util.List;
import java.util.UUID;
import static net.hostsharing.hsadminng.Mapper.map;
@RestController
public class HsOfficePersonController implements HsOfficePersonsApi {
@Autowired
private Context context;
@Autowired
private HsOfficePersonRepository personRepo;
@Override
@Transactional(readOnly = true)
public ResponseEntity<List<HsOfficePersonResource>> listPersons(
final String currentUser,
final String assumedRoles,
final String label) {
context.define(currentUser, assumedRoles);
final var entities = personRepo.findPersonByOptionalNameLike(label);
final var resources = Mapper.mapList(entities, HsOfficePersonResource.class);
return ResponseEntity.ok(resources);
}
@Override
@Transactional
public ResponseEntity<HsOfficePersonResource> addPerson(
final String currentUser,
final String assumedRoles,
final HsOfficePersonInsertResource body) {
context.define(currentUser, assumedRoles);
final var entityToSave = map(body, HsOfficePersonEntity.class);
entityToSave.setUuid(UUID.randomUUID());
final var saved = personRepo.save(entityToSave);
final var uri =
MvcUriComponentsBuilder.fromController(getClass())
.path("/api/hs/office/persons/{id}")
.buildAndExpand(entityToSave.getUuid())
.toUri();
final var mapped = map(saved, HsOfficePersonResource.class);
return ResponseEntity.created(uri).body(mapped);
}
@Override
@Transactional(readOnly = true)
public ResponseEntity<HsOfficePersonResource> getPersonByUuid(
final String currentUser,
final String assumedRoles,
final UUID personUuid) {
context.define(currentUser, assumedRoles);
final var result = personRepo.findByUuid(personUuid);
if (result.isEmpty()) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(map(result.get(), HsOfficePersonResource.class));
}
@Override
@Transactional
public ResponseEntity<Void> deletePersonByUuid(
final String currentUser,
final String assumedRoles,
final UUID personUuid) {
context.define(currentUser, assumedRoles);
final var result = personRepo.deleteByUuid(personUuid);
if (result == 0) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.noContent().build();
}
@Override
@Transactional
public ResponseEntity<HsOfficePersonResource> patchPerson(
final String currentUser,
final String assumedRoles,
final UUID personUuid,
final HsOfficePersonPatchResource body) {
context.define(currentUser, assumedRoles);
final var current = personRepo.findByUuid(personUuid).orElseThrow();
new HsOfficePersonEntityPatch(current).apply(body);
final var saved = personRepo.save(current);
final var mapped = map(saved, HsOfficePersonResource.class);
return ResponseEntity.ok(mapped);
}
}

View File

@@ -0,0 +1,26 @@
package net.hostsharing.hsadminng.hs.office.person;
import net.hostsharing.hsadminng.OptionalFromJson;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePersonPatchResource;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePersonTypeResource;
import java.util.Optional;
class HsOfficePersonEntityPatch {
private final HsOfficePersonEntity entity;
HsOfficePersonEntityPatch(final HsOfficePersonEntity entity) {
this.entity = entity;
}
void apply(final HsOfficePersonPatchResource resource) {
Optional.ofNullable(resource.getPersonType())
.map(HsOfficePersonTypeResource::getValue)
.map(HsOfficePersonType::valueOf)
.ifPresent(entity::setPersonType);
OptionalFromJson.of(resource.getTradeName()).ifPresent(entity::setTradeName);
OptionalFromJson.of(resource.getFamilyName()).ifPresent(entity::setFamilyName);
OptionalFromJson.of(resource.getGivenName()).ifPresent(entity::setGivenName);
}
}

View File

@@ -16,3 +16,5 @@ map:
null: org.openapitools.jackson.nullable.JsonNullable
/api/hs/office/contacts/{contactUUID}:
null: org.openapitools.jackson.nullable.JsonNullable
/api/hs/office/persons/{personUUID}:
null: org.openapitools.jackson.nullable.JsonNullable

View File

@@ -3,16 +3,28 @@ components:
schemas:
HsOfficePersonBase:
HsOfficePersonTypeValues:
- NATURAL # a human
- LEGAL # e.g. Corp., Inc., AG, GmbH, eG
- SOLE_REPRESENTATION # e.g. OHG, GbR
- JOINT_REPRESENTATION # e.g. community of heirs
HsOfficePersonType:
type: string
enum:
- NATURAL # a human
- LEGAL # e.g. Corp., Inc., AG, GmbH, eG
- SOLE_REPRESENTATION # e.g. OHG, GbR
- JOINT_REPRESENTATION # e.g. community of heirs
HsOfficePerson:
type: object
properties:
personType:
uuid:
type: string
enum:
- NATURAL # a human
- LEGAL # e.g. Corp., Inc., AG, GmbH, eG
- SOLE_REPRESENTATION # e.g. OHG, GbR
- JOINT_REPRESENTATION # e.g. community of heirs
format: uuid
personType:
$ref: '#/components/schemas/HsOfficePersonType'
tradeName:
type: string
givenName:
@@ -20,14 +32,32 @@ components:
familyName:
type: string
HsOfficePerson:
allOf:
- type: object
properties:
uuid:
type: string
format: uuid
- $ref: '#/components/schemas/HsOfficePersonBase'
HsOfficePersonInsert:
type: object
properties:
personType:
$ref: '#/components/schemas/HsOfficePersonType'
tradeName:
type: string
givenName:
type: string
familyName:
type: string
required:
- personType
HsOfficePersonUpdate:
$ref: '#/components/schemas/HsOfficePersonBase'
HsOfficePersonPatch:
type: object
properties:
personType:
nullable: true
$ref: '#/components/schemas/HsOfficePersonType'
tradeName:
type: string
nullable: true
givenName:
type: string
nullable: true
familyName:
type: string
nullable: true

View File

@@ -0,0 +1,83 @@
get:
tags:
- hs-office-persons
description: 'Fetch a single business person by its uuid, if visible for the current subject.'
operationId: getPersonByUuid
parameters:
- $ref: './auth.yaml#/components/parameters/currentUser'
- $ref: './auth.yaml#/components/parameters/assumedRoles'
- name: personUUID
in: path
required: true
schema:
type: string
format: uuid
description: UUID of the person to fetch.
responses:
"200":
description: OK
content:
'application/json':
schema:
$ref: './hs-office-person-schemas.yaml#/components/schemas/HsOfficePerson'
"401":
$ref: './error-responses.yaml#/components/responses/Unauthorized'
"403":
$ref: './error-responses.yaml#/components/responses/Forbidden'
patch:
tags:
- hs-office-persons
description: 'Updates a single person by its uuid, if permitted for the current subject.'
operationId: patchPerson
parameters:
- $ref: './auth.yaml#/components/parameters/currentUser'
- $ref: './auth.yaml#/components/parameters/assumedRoles'
- name: personUUID
in: path
required: true
schema:
type: string
format: uuid
requestBody:
content:
'application/json':
schema:
$ref: './hs-office-person-schemas.yaml#/components/schemas/HsOfficePersonPatch'
responses:
"200":
description: OK
content:
'application/json':
schema:
$ref: './hs-office-person-schemas.yaml#/components/schemas/HsOfficePerson'
"401":
$ref: './error-responses.yaml#/components/responses/Unauthorized'
"403":
$ref: './error-responses.yaml#/components/responses/Forbidden'
delete:
tags:
- hs-office-persons
description: 'Delete a single business person by its uuid, if permitted for the current subject.'
operationId: deletePersonByUuid
parameters:
- $ref: './auth.yaml#/components/parameters/currentUser'
- $ref: './auth.yaml#/components/parameters/assumedRoles'
- name: personUUID
in: path
required: true
schema:
type: string
format: uuid
description: UUID of the person to delete.
responses:
"204":
description: No Content
"401":
$ref: './error-responses.yaml#/components/responses/Unauthorized'
"403":
$ref: './error-responses.yaml#/components/responses/Forbidden'
"404":
$ref: './error-responses.yaml#/components/responses/NotFound'

View File

@@ -0,0 +1,56 @@
get:
summary: Returns a list of (optionally filtered) persons.
description: Returns the list of (optionally filtered) persons which are visible to the current user or any of it's assumed roles.
tags:
- hs-office-persons
operationId: listPersons
parameters:
- $ref: './auth.yaml#/components/parameters/currentUser'
- $ref: './auth.yaml#/components/parameters/assumedRoles'
- name: name
in: query
required: false
schema:
type: string
description: Prefix of label to filter the results.
responses:
"200":
description: OK
content:
'application/json':
schema:
type: array
items:
$ref: './hs-office-person-schemas.yaml#/components/schemas/HsOfficePerson'
"401":
$ref: './error-responses.yaml#/components/responses/Unauthorized'
"403":
$ref: './error-responses.yaml#/components/responses/Forbidden'
post:
summary: Adds a new person.
tags:
- hs-office-persons
operationId: addPerson
parameters:
- $ref: './auth.yaml#/components/parameters/currentUser'
- $ref: './auth.yaml#/components/parameters/assumedRoles'
requestBody:
content:
'application/json':
schema:
$ref: './hs-office-person-schemas.yaml#/components/schemas/HsOfficePersonInsert'
required: true
responses:
"201":
description: Created
content:
'application/json':
schema:
$ref: './hs-office-person-schemas.yaml#/components/schemas/HsOfficePerson'
"401":
$ref: './error-responses.yaml#/components/responses/Unauthorized'
"403":
$ref: './error-responses.yaml#/components/responses/Forbidden'
"409":
$ref: './error-responses.yaml#/components/responses/Conflict'

View File

@@ -25,3 +25,12 @@ paths:
/api/hs/office/contacts/{contactUUID}:
$ref: "./hs-office-contacts-with-uuid.yaml"
# Persons
/api/hs/office/persons:
$ref: "./hs-office-persons.yaml"
/api/hs/office/persons/{personUUID}:
$ref: "./hs-office-persons-with-uuid.yaml"