add hs-office-coopshares API+controller
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package net.hostsharing.hsadminng.hs.office.coopshares;
|
||||
|
||||
import net.hostsharing.hsadminng.Mapper;
|
||||
import net.hostsharing.hsadminng.context.Context;
|
||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficeCoopSharesApi;
|
||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeCoopSharesTransactionInsertResource;
|
||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeCoopSharesTransactionResource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
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 javax.persistence.EntityManager;
|
||||
import javax.validation.Valid;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
import static net.hostsharing.hsadminng.Mapper.map;
|
||||
|
||||
@RestController
|
||||
|
||||
public class HsOfficeCoopSharesTransactionController implements HsOfficeCoopSharesApi {
|
||||
|
||||
@Autowired
|
||||
private Context context;
|
||||
|
||||
@Autowired
|
||||
private HsOfficeCoopSharesTransactionRepository coopSharesTransactionRepo;
|
||||
|
||||
@Autowired
|
||||
private EntityManager em;
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public ResponseEntity<List<HsOfficeCoopSharesTransactionResource>> listCoopShares(
|
||||
final String currentUser,
|
||||
final String assumedRoles,
|
||||
final UUID membershipUuid,
|
||||
Integer memberNumber, // TODO: remove
|
||||
final @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate fromValueDate,
|
||||
final @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate toValueDate) {
|
||||
context.define(currentUser, assumedRoles);
|
||||
|
||||
|
||||
final var entities = coopSharesTransactionRepo.findCoopSharesTransactionByOptionalMembershipUuidAndDateRange(
|
||||
memberNumber,
|
||||
fromValueDate,
|
||||
toValueDate);
|
||||
|
||||
final var resources = Mapper.mapList(entities, HsOfficeCoopSharesTransactionResource.class,
|
||||
COOP_SHARES_ENTITY_TO_RESOURCE_POSTMAPPER);
|
||||
return ResponseEntity.ok(resources);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public ResponseEntity<HsOfficeCoopSharesTransactionResource> addCoopSharesTransaction(
|
||||
final String currentUser,
|
||||
final String assumedRoles,
|
||||
@Valid final HsOfficeCoopSharesTransactionInsertResource body) {
|
||||
|
||||
context.define(currentUser, assumedRoles);
|
||||
|
||||
final var entityToSave = map(
|
||||
body,
|
||||
HsOfficeCoopSharesTransactionEntity.class,
|
||||
COOP_SHARES_RESOURCE_TO_ENTITY_POSTMAPPER);
|
||||
entityToSave.setUuid(UUID.randomUUID());
|
||||
|
||||
final var saved = coopSharesTransactionRepo.save(entityToSave);
|
||||
|
||||
final var uri =
|
||||
MvcUriComponentsBuilder.fromController(getClass())
|
||||
.path("/api/hs/office/CoopSharesTransactions/{id}")
|
||||
.buildAndExpand(entityToSave.getUuid())
|
||||
.toUri();
|
||||
final var mapped = map(saved, HsOfficeCoopSharesTransactionResource.class,
|
||||
COOP_SHARES_ENTITY_TO_RESOURCE_POSTMAPPER);
|
||||
return ResponseEntity.created(uri).body(mapped);
|
||||
}
|
||||
|
||||
final BiConsumer<HsOfficeCoopSharesTransactionEntity, HsOfficeCoopSharesTransactionResource> COOP_SHARES_ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> {
|
||||
// resource.setValidFrom(entity.getValidity().lower());
|
||||
// if (entity.getValidity().hasUpperBound()) {
|
||||
// resource.setValidTo(entity.getValidity().upper().minusDays(1));
|
||||
// }
|
||||
};
|
||||
|
||||
final BiConsumer<HsOfficeCoopSharesTransactionInsertResource, HsOfficeCoopSharesTransactionEntity> COOP_SHARES_RESOURCE_TO_ENTITY_POSTMAPPER = (resource, entity) -> {
|
||||
// entity.setValidity(toPostgresDateRange(resource.getValidFrom(), resource.getValidTo()));
|
||||
};
|
||||
}
|
@@ -15,8 +15,8 @@ public interface HsOfficeCoopSharesTransactionRepository extends Repository<HsOf
|
||||
@Query("""
|
||||
SELECT st FROM HsOfficeCoopSharesTransactionEntity st
|
||||
WHERE (:memberNumber IS NULL OR st.membership.memberNumber = :memberNumber)
|
||||
AND (:fromValueDate IS NULL OR (st.valueDate >= :fromValueDate))
|
||||
AND (:toValueDate IS NULL OR (st.valueDate <= :toValueDate))
|
||||
AND ( CAST(:fromValueDate AS java.time.LocalDate) IS NULL OR (st.valueDate >= :fromValueDate))
|
||||
AND ( CAST(:toValueDate AS java.time.LocalDate)IS NULL OR (st.valueDate <= :toValueDate))
|
||||
ORDER BY st.membership.memberNumber, st.valueDate
|
||||
""")
|
||||
List<HsOfficeCoopSharesTransactionEntity> findCoopSharesTransactionByOptionalMembershipUuidAndDateRange(
|
||||
|
@@ -0,0 +1,54 @@
|
||||
|
||||
components:
|
||||
|
||||
schemas:
|
||||
|
||||
HsOfficeCoopSharesTransactionType:
|
||||
type: string
|
||||
enum:
|
||||
- ADJUSTMENT
|
||||
- SUBSCRIPTION
|
||||
- CANCELLATION;
|
||||
|
||||
HsOfficeCoopSharesTransaction:
|
||||
type: object
|
||||
properties:
|
||||
uuid:
|
||||
type: string
|
||||
format: uuid
|
||||
transactionType:
|
||||
$ref: '#/components/schemas/HsOfficeCoopSharesTransactionType'
|
||||
sharesCount:
|
||||
type: integer
|
||||
valueDate:
|
||||
type: string
|
||||
format: date
|
||||
reference:
|
||||
type: string
|
||||
comment:
|
||||
type: string
|
||||
|
||||
HsOfficeCoopSharesTransactionInsert:
|
||||
type: object
|
||||
properties:
|
||||
membershipUuid:
|
||||
type: string
|
||||
format: uuid
|
||||
nullable: false
|
||||
transactionType:
|
||||
$ref: '#/components/schemas/HsOfficeCoopSharesTransactionType'
|
||||
sharesCount:
|
||||
type: integer
|
||||
valueDate:
|
||||
type: string
|
||||
format: date
|
||||
reference:
|
||||
type: string
|
||||
comment:
|
||||
type: string
|
||||
required:
|
||||
- transactionType
|
||||
- sharesCount
|
||||
- valueDate
|
||||
- reference
|
||||
additionalProperties: false
|
@@ -0,0 +1,78 @@
|
||||
get:
|
||||
summary: Returns a list of (optionally filtered) cooperative share transactions.
|
||||
description: Returns the list of (optionally filtered) cooperative share transactions which are visible to the current user or any of it's assumed roles.
|
||||
tags:
|
||||
- hs-office-coopShares
|
||||
operationId: listCoopShares
|
||||
parameters:
|
||||
- $ref: './auth.yaml#/components/parameters/currentUser'
|
||||
- $ref: './auth.yaml#/components/parameters/assumedRoles'
|
||||
- name: membershipUuid
|
||||
in: query
|
||||
required: false
|
||||
schema:
|
||||
type: string
|
||||
format: uuid
|
||||
description: Optional UUID of the related membership.
|
||||
- name: memberNumber
|
||||
in: query
|
||||
required: false
|
||||
schema:
|
||||
type: integer
|
||||
description: Optional member number of the related membership.
|
||||
- name: fromValueDate
|
||||
in: query
|
||||
required: false
|
||||
schema:
|
||||
type: string
|
||||
format: date
|
||||
description: Optional value date range start (inclusive).
|
||||
- name: toValueDate
|
||||
in: query
|
||||
required: false
|
||||
schema:
|
||||
type: string
|
||||
format: date
|
||||
description: Optional value date range end (inclusive).
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
content:
|
||||
'application/json':
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
$ref: './hs-office-coopshares-schemas.yaml#/components/schemas/HsOfficeCoopSharesTransaction'
|
||||
"401":
|
||||
$ref: './error-responses.yaml#/components/responses/Unauthorized'
|
||||
"403":
|
||||
$ref: './error-responses.yaml#/components/responses/Forbidden'
|
||||
|
||||
post:
|
||||
summary: Adds a new cooperative share transaction.
|
||||
tags:
|
||||
- hs-office-coopShares
|
||||
operationId: addCoopSharesTransaction
|
||||
parameters:
|
||||
- $ref: './auth.yaml#/components/parameters/currentUser'
|
||||
- $ref: './auth.yaml#/components/parameters/assumedRoles'
|
||||
requestBody:
|
||||
description: A JSON object describing the new cooperative shares transaction.
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '/hs-office-coopshares-schemas.yaml#/components/schemas/HsOfficeCoopSharesTransactionInsert'
|
||||
responses:
|
||||
"201":
|
||||
description: Created
|
||||
content:
|
||||
'application/json':
|
||||
schema:
|
||||
$ref: './hs-office-coopshares-schemas.yaml#/components/schemas/HsOfficeCoopSharesTransaction'
|
||||
"401":
|
||||
$ref: './error-responses.yaml#/components/responses/Unauthorized'
|
||||
"403":
|
||||
$ref: './error-responses.yaml#/components/responses/Forbidden'
|
||||
"409":
|
||||
$ref: './error-responses.yaml#/components/responses/Conflict'
|
@@ -78,3 +78,9 @@ paths:
|
||||
|
||||
/api/hs/office/memberships/{membershipUUID}:
|
||||
$ref: "./hs-office-memberships-with-uuid.yaml"
|
||||
|
||||
|
||||
# Coop Shares Transaction
|
||||
|
||||
/api/hs/office/coopsharestransactions:
|
||||
$ref: "./hs-office-coopshares.yaml"
|
||||
|
@@ -25,7 +25,7 @@ begin
|
||||
into hs_office_coopsharestransaction(uuid, membershipuuid, transactiontype, valuedate, sharecount, reference, comment)
|
||||
values
|
||||
(uuid_generate_v4(), membership.uuid, 'SUBSCRIPTION', '2010-03-15', 2, 'ref '||givenMembershipNumber||'-1', 'initial subscription'),
|
||||
(uuid_generate_v4(), membership.uuid, 'SUBSCRIPTION', '2021-09-01', 24, 'ref '||givenMembershipNumber||'-2', 'subsscibing more'),
|
||||
(uuid_generate_v4(), membership.uuid, 'SUBSCRIPTION', '2021-09-01', 24, 'ref '||givenMembershipNumber||'-2', 'subscibing more'),
|
||||
(uuid_generate_v4(), membership.uuid, 'CANCELLATION', '2022-10-20', 12, 'ref '||givenMembershipNumber||'-3', 'cancelling some');
|
||||
end; $$;
|
||||
--//
|
||||
|
Reference in New Issue
Block a user