Repository navigation
feat: orders with a computed total - #7
Merged
Merged
Conversation
Order maps the orders table with two lazy, non-optional links, to the customer and to the product, and a total stored as NUMERIC(14, 2). The total is a snapshot taken when the order is created, so a later price change never rewrites history. ORDER is a keyword of HQL, the query language behind Panache, and a generated query such as FROM Order would collide with ORDER BY. The JPA entity name is therefore CustomerOrder, while @table keeps the real table name, so queries stay valid and the schema does not change.
The repository offers one query ordered by id whose two filters, customer and product, are optional and combine with AND, and a count that answers the same filters so paging totals match the page content. countByCustomerId and countByProductId give the customer and product services what they need to refuse deleting a row that orders still reference.
An order names a customer and a product and a quantity of at least one. There is no total in the payload: it is computed by the server from the current price, so a client can never set its own price.
The response is flat, with the ids of the customer and product, the quantity and the frozen total. Names are left out so the payload does not depend on rows that can change after the order was placed.
The contract has a filterable list, a lookup and a creation, and nothing else: an order is a record of a sale, so it is not updated or deleted through the API. The create operation is where the business rule, the total, lives, hence in the service and not in the resource.
Creating an order loads the customer and the product, and the total is the unit price times the quantity, scaled to two decimals like the column. An unknown customer or product answers 422 through BusinessRuleException, and so does a total too large for NUMERIC(14, 2), which would otherwise surface as a 500 from the database. The product is read through its repository and not through the cached product service, so an order is always priced from the current row and never from a cached copy.
The three endpoints are protected with @PermissionsAllowed, ORDER:READ for the list and the detail and ORDER:WRITE for creation, and the resource only validates, delegates and wraps the result in the success envelope. Creating answers 201. The list takes the optional customerId and productId filters and bounds the page size like the other lists.
Orders reference products through a foreign key, so deleting a referenced product would fail in the database and answer 500. The service now checks first and answers 422 through BusinessRuleException, as the category deletion does, since the request is well formed and the state of the data forbids it. The cache invalidation stays on the method, so the entry is evicted only when the delete is actually allowed to go through.
The same rule as for products: a customer referenced by orders cannot be deleted, and the answer is a 422 instead of a 500 from the foreign key. The check and the deletion run in one transaction.
These tests are the runtime proof of the entity name choice: the derived and Panache queries run on CustomerOrder against the real schema, with every filter combination, both counts, and the foreign key and quantity constraints.
With mocked repositories, the tests check the total computation, that the price is read fresh from the product repository, the 422 for an unknown customer or product and for a total at the limit of the column, and the 404 and the paging.
The tests call the endpoints over HTTP: creation with the computed total, a total that stays frozen when the price changes, the filters, validation, the 422 cases, and the 401 and 403 cases. They also check that a product or a customer with orders answers 422 on deletion and can be deleted once the orders are gone.
The service test now mocks the order repository and adds the refusal case, while the existing case still covers deletion without orders.
The service test mocks the order repository and adds the refusal case next to the deletion without orders.
The three tasks are ticked: the entity, repository and service with the computed total, the resource, and the tests.
20 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Branch
feature/orders
Task checklist
Orderentity, repository, contract/implementation service: computestotal = quantity * product.unitPriceOrderResource(GET list withcustomerId/productIdfilters, GET detail, POST)Commit summary
CustomerOrderbecause ORDER is an HQL keyword), repository with combinable filters, DTOs, service computing the frozen total, resource (ORDER:READ/ORDER:WRITE)Test checklist
mvn verifyis green on the whole project (181 tests)Code review checklist
ApiResponse<T>, errors are RFC 9457 Problem Details (HttpProblem), neverApiResponseon an error path@PermissionsAllowed), no manual permissionif@CacheResulthas its matching@CacheInvalidateon the write operations (unchanged)AuditLogger(n/a here)Known limits: check-then-act deletes and order creation without row locks (documented, same class as the category delete).