Skip to content

feat: orders with a computed total - #7

Merged
EdgarEldy merged 15 commits into
developfrom
feature/orders
Sep 19, 2026
Merged

EdgarEldy merged 15 commits into
developfrom
feature/orders

Conversation

@EdgarEldy

Copy link
Copy Markdown
Owner

Branch

feature/orders

Task checklist

  • Order entity, repository, contract/implementation service: computes total = quantity * product.unitPrice
  • OrderResource (GET list with customerId/productId filters, GET detail, POST)
  • Tests, including the total computation

Commit summary

  • entity (JPA name CustomerOrder because ORDER is an HQL keyword), repository with combinable filters, DTOs, service computing the frozen total, resource (ORDER:READ / ORDER:WRITE)
  • product and customer deletion now refused with 422 when orders reference them (the rule left open by the two previous branches)
  • tests for the resource, the service (Mockito) and the repository (Dev Services PostgreSQL), adaptation of the product and customer service tests
  • README checklist ticked

Test checklist

  • Unit tests pass
  • Integration tests pass (where the branch's checklist calls for them)
  • mvn verify is green on the whole project (181 tests)

Code review checklist

  • No business logic in JAX-RS resources
  • Success responses are wrapped in ApiResponse<T>, errors are RFC 9457 Problem Details (HttpProblem), never ApiResponse on an error path
  • Permission checks are declarative (@PermissionsAllowed), no manual permission if
  • Every @CacheResult has its matching @CacheInvalidate on the write operations (unchanged)
  • Every RBAC mutation calls AuditLogger (n/a here)
  • The database schema only changes through a Flyway migration (no schema change)
  • No manual Testcontainers setup (Dev Services only)
  • Commits are atomic (one commit per file added or modified), follow Conventional Commits, no em dash, no Claude/Anthropic mention

Known limits: check-then-act deletes and order creation without row locks (documented, same class as the category delete).

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.
@EdgarEldy
EdgarEldy merged commit d747563 into develop Sep 19, 2026
2 checks passed
@EdgarEldy EdgarEldy mentioned this pull request Sep 19, 2026
20 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant