implements HsOfficeContactController
This commit is contained in:
@@ -95,11 +95,11 @@ public class Context {
|
||||
.getSingleResult();
|
||||
}
|
||||
|
||||
private static String getCallerMethodNameFromStack() {
|
||||
public static String getCallerMethodNameFromStackFrame(final int skipFrames) {
|
||||
final Optional<StackWalker.StackFrame> caller =
|
||||
StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE)
|
||||
.walk(frames -> frames
|
||||
.skip(2)
|
||||
.skip(skipFrames)
|
||||
.filter(c -> c.getDeclaringClass() != Context.class)
|
||||
.filter(c -> c.getDeclaringClass()
|
||||
.getPackageName()
|
||||
@@ -115,7 +115,7 @@ public class Context {
|
||||
if (isRequestScopeAvailable()) {
|
||||
return request.getMethod() + " " + request.getRequestURI();
|
||||
} else {
|
||||
return getCallerMethodNameFromStack();
|
||||
return getCallerMethodNameFromStackFrame(2);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,117 @@
|
||||
package net.hostsharing.hsadminng.hs.office.contact;
|
||||
|
||||
import net.hostsharing.hsadminng.Mapper;
|
||||
import net.hostsharing.hsadminng.context.Context;
|
||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficeContactsApi;
|
||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeContactInsertResource;
|
||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeContactPatchResource;
|
||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeContactResource;
|
||||
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 HsOfficeContactController implements HsOfficeContactsApi {
|
||||
|
||||
@Autowired
|
||||
private Context context;
|
||||
|
||||
@Autowired
|
||||
private HsOfficeContactRepository contactRepo;
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public ResponseEntity<List<HsOfficeContactResource>> listContacts(
|
||||
final String currentUser,
|
||||
final String assumedRoles,
|
||||
final String label) {
|
||||
context.define(currentUser, assumedRoles);
|
||||
|
||||
final var entities = contactRepo.findContactByOptionalLabelLike(label);
|
||||
|
||||
final var resources = Mapper.mapList(entities, HsOfficeContactResource.class);
|
||||
return ResponseEntity.ok(resources);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public ResponseEntity<HsOfficeContactResource> addContact(
|
||||
final String currentUser,
|
||||
final String assumedRoles,
|
||||
final HsOfficeContactInsertResource body) {
|
||||
|
||||
context.define(currentUser, assumedRoles);
|
||||
|
||||
final var entityToSave = map(body, HsOfficeContactEntity.class);
|
||||
entityToSave.setUuid(UUID.randomUUID());
|
||||
|
||||
final var saved = contactRepo.save(entityToSave);
|
||||
|
||||
final var uri =
|
||||
MvcUriComponentsBuilder.fromController(getClass())
|
||||
.path("/api/hs/office/contacts/{id}")
|
||||
.buildAndExpand(entityToSave.getUuid())
|
||||
.toUri();
|
||||
final var mapped = map(saved, HsOfficeContactResource.class);
|
||||
return ResponseEntity.created(uri).body(mapped);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public ResponseEntity<HsOfficeContactResource> getContactByUuid(
|
||||
final String currentUser,
|
||||
final String assumedRoles,
|
||||
final UUID contactUuid) {
|
||||
|
||||
context.define(currentUser, assumedRoles);
|
||||
|
||||
final var result = contactRepo.findByUuid(contactUuid);
|
||||
if (result.isEmpty()) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
return ResponseEntity.ok(map(result.get(), HsOfficeContactResource.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public ResponseEntity<Void> deleteContactByUuid(
|
||||
final String currentUser,
|
||||
final String assumedRoles,
|
||||
final UUID contactUuid) {
|
||||
context.define(currentUser, assumedRoles);
|
||||
|
||||
final var result = contactRepo.deleteByUuid(contactUuid);
|
||||
if (result == 0) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public ResponseEntity<HsOfficeContactResource> patchContact(
|
||||
final String currentUser,
|
||||
final String assumedRoles,
|
||||
final UUID contactUuid,
|
||||
final HsOfficeContactPatchResource body) {
|
||||
|
||||
context.define(currentUser, assumedRoles);
|
||||
|
||||
final var current = contactRepo.findByUuid(contactUuid).orElseThrow();
|
||||
|
||||
new HsOfficeContactEntityPatch(current).apply(body);
|
||||
|
||||
final var saved = contactRepo.save(current);
|
||||
final var mapped = map(saved, HsOfficeContactResource.class);
|
||||
return ResponseEntity.ok(mapped);
|
||||
}
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
package net.hostsharing.hsadminng.hs.office.contact;
|
||||
|
||||
import net.hostsharing.hsadminng.OptionalFromJson;
|
||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeContactPatchResource;
|
||||
|
||||
class HsOfficeContactEntityPatch {
|
||||
|
||||
private final HsOfficeContactEntity entity;
|
||||
|
||||
HsOfficeContactEntityPatch(final HsOfficeContactEntity entity) {
|
||||
this.entity = entity;
|
||||
}
|
||||
|
||||
void apply(final HsOfficeContactPatchResource resource) {
|
||||
OptionalFromJson.of(resource.getLabel()).ifPresent(entity::setLabel);
|
||||
OptionalFromJson.of(resource.getPostalAddress()).ifPresent(entity::setPostalAddress);
|
||||
OptionalFromJson.of(resource.getEmailAddresses()).ifPresent(entity::setEmailAddresses);
|
||||
OptionalFromJson.of(resource.getPhoneNumbers()).ifPresent(entity::setPhoneNumbers);
|
||||
}
|
||||
}
|
@@ -14,3 +14,5 @@ map:
|
||||
paths:
|
||||
/api/hs/office/partners/{partnerUUID}:
|
||||
null: org.openapitools.jackson.nullable.JsonNullable
|
||||
/api/hs/office/contacts/{contactUUID}:
|
||||
null: org.openapitools.jackson.nullable.JsonNullable
|
||||
|
@@ -3,7 +3,22 @@ components:
|
||||
|
||||
schemas:
|
||||
|
||||
HsOfficeContactBase:
|
||||
HsOfficeContact:
|
||||
type: object
|
||||
properties:
|
||||
uuid:
|
||||
type: string
|
||||
format: uuid
|
||||
label:
|
||||
type: string
|
||||
postalAddress:
|
||||
type: string
|
||||
emailAddresses:
|
||||
type: string
|
||||
phoneNumbers:
|
||||
type: string
|
||||
|
||||
HsOfficeContactInsert:
|
||||
type: object
|
||||
properties:
|
||||
label:
|
||||
@@ -14,15 +29,21 @@ components:
|
||||
type: string
|
||||
phoneNumbers:
|
||||
type: string
|
||||
required:
|
||||
- label
|
||||
|
||||
HsOfficeContact:
|
||||
allOf:
|
||||
- type: object
|
||||
properties:
|
||||
uuid:
|
||||
type: string
|
||||
format: uuid
|
||||
- $ref: '#/components/schemas/HsOfficeContactBase'
|
||||
|
||||
HsOfficeContactUpdate:
|
||||
$ref: '#/components/schemas/HsOfficeContactBase'
|
||||
HsOfficeContactPatch:
|
||||
type: object
|
||||
properties:
|
||||
label:
|
||||
type: string
|
||||
nullable: true
|
||||
postalAddress:
|
||||
type: string
|
||||
nullable: true
|
||||
emailAddresses:
|
||||
type: string
|
||||
nullable: true
|
||||
phoneNumbers:
|
||||
type: string
|
||||
nullable: true
|
||||
|
@@ -0,0 +1,83 @@
|
||||
get:
|
||||
tags:
|
||||
- hs-office-contacts
|
||||
description: 'Fetch a single business contact by its uuid, if visible for the current subject.'
|
||||
operationId: getContactByUuid
|
||||
parameters:
|
||||
- $ref: './auth.yaml#/components/parameters/currentUser'
|
||||
- $ref: './auth.yaml#/components/parameters/assumedRoles'
|
||||
- name: contactUUID
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
format: uuid
|
||||
description: UUID of the contact to fetch.
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
content:
|
||||
'application/json':
|
||||
schema:
|
||||
$ref: './hs-office-contact-schemas.yaml#/components/schemas/HsOfficeContact'
|
||||
|
||||
"401":
|
||||
$ref: './error-responses.yaml#/components/responses/Unauthorized'
|
||||
"403":
|
||||
$ref: './error-responses.yaml#/components/responses/Forbidden'
|
||||
|
||||
patch:
|
||||
tags:
|
||||
- hs-office-contacts
|
||||
description: 'Updates a single business contact by its uuid, if permitted for the current subject.'
|
||||
operationId: patchContact
|
||||
parameters:
|
||||
- $ref: './auth.yaml#/components/parameters/currentUser'
|
||||
- $ref: './auth.yaml#/components/parameters/assumedRoles'
|
||||
- name: contactUUID
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
format: uuid
|
||||
requestBody:
|
||||
content:
|
||||
'application/json':
|
||||
schema:
|
||||
$ref: './hs-office-contact-schemas.yaml#/components/schemas/HsOfficeContactPatch'
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
content:
|
||||
'application/json':
|
||||
schema:
|
||||
$ref: './hs-office-contact-schemas.yaml#/components/schemas/HsOfficeContact'
|
||||
"401":
|
||||
$ref: './error-responses.yaml#/components/responses/Unauthorized'
|
||||
"403":
|
||||
$ref: './error-responses.yaml#/components/responses/Forbidden'
|
||||
|
||||
delete:
|
||||
tags:
|
||||
- hs-office-contacts
|
||||
description: 'Delete a single business contact by its uuid, if permitted for the current subject.'
|
||||
operationId: deleteContactByUuid
|
||||
parameters:
|
||||
- $ref: './auth.yaml#/components/parameters/currentUser'
|
||||
- $ref: './auth.yaml#/components/parameters/assumedRoles'
|
||||
- name: contactUUID
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
format: uuid
|
||||
description: UUID of the contact 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'
|
@@ -0,0 +1,56 @@
|
||||
get:
|
||||
summary: Returns a list of (optionally filtered) contacts.
|
||||
description: Returns the list of (optionally filtered) contacts which are visible to the current user or any of it's assumed roles.
|
||||
tags:
|
||||
- hs-office-contacts
|
||||
operationId: listContacts
|
||||
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-contact-schemas.yaml#/components/schemas/HsOfficeContact'
|
||||
"401":
|
||||
$ref: './error-responses.yaml#/components/responses/Unauthorized'
|
||||
"403":
|
||||
$ref: './error-responses.yaml#/components/responses/Forbidden'
|
||||
|
||||
post:
|
||||
summary: Adds a new contact.
|
||||
tags:
|
||||
- hs-office-contacts
|
||||
operationId: addContact
|
||||
parameters:
|
||||
- $ref: './auth.yaml#/components/parameters/currentUser'
|
||||
- $ref: './auth.yaml#/components/parameters/assumedRoles'
|
||||
requestBody:
|
||||
content:
|
||||
'application/json':
|
||||
schema:
|
||||
$ref: './hs-office-contact-schemas.yaml#/components/schemas/HsOfficeContactInsert'
|
||||
required: true
|
||||
responses:
|
||||
"201":
|
||||
description: Created
|
||||
content:
|
||||
'application/json':
|
||||
schema:
|
||||
$ref: './hs-office-contact-schemas.yaml#/components/schemas/HsOfficeContact'
|
||||
"401":
|
||||
$ref: './error-responses.yaml#/components/responses/Unauthorized'
|
||||
"403":
|
||||
$ref: './error-responses.yaml#/components/responses/Forbidden'
|
||||
"409":
|
||||
$ref: './error-responses.yaml#/components/responses/Conflict'
|
@@ -12,7 +12,7 @@ get:
|
||||
required: false
|
||||
schema:
|
||||
type: string
|
||||
description: Customer-prefix to filter the results. TODO
|
||||
description: Prefix of name properties from person or contact to filter the results.
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
|
@@ -8,9 +8,20 @@ servers:
|
||||
|
||||
paths:
|
||||
|
||||
# Partners
|
||||
|
||||
/api/hs/office/partners:
|
||||
$ref: "./hs-office-partners.yaml"
|
||||
|
||||
/api/hs/office/partners/{partnerUUID}:
|
||||
$ref: "./hs-office-partners-with-uuid.yaml"
|
||||
|
||||
|
||||
# Contacts
|
||||
|
||||
/api/hs/office/contacts:
|
||||
$ref: "./hs-office-contacts.yaml"
|
||||
|
||||
/api/hs/office/contacts/{contactUUID}:
|
||||
$ref: "./hs-office-contacts-with-uuid.yaml"
|
||||
|
||||
|
Reference in New Issue
Block a user