Discover the Operations

What can happen to it? Add, remove, change quantity, view, total, clear — sorted into create / read / update / delete / domain actions, each with inputs, state read, state changed, output, side effects and errors. Pure logic before database, network and UI.

What Can Happen to It?
▶ lab

The cart's state is items of { productId, quantity }. What can happen to it? Add an item, remove one, change a quantity, view the items, calculate the total, clear it. Six sentences of behaviour, in words, before a single function signature.

Q · You have the state. How do you find the operations — everything that can happen to a cart — without starting to code and discovering them one bug at a time?
CRUD and Domain Actions
▶ lab

Sort the operations into CREATE, READ, UPDATE, DELETE and DOMAIN ACTIONS. The cart fits mostly into CRUD with "calculate total" as a domain action — but "reserve stock" and "check out" are domain actions that CRUD would mangle, and not every domain should be modelled as four verbs on a table.

Q · You have the operations in words. Which of them are plain create / read / update / delete, which are something else, and what goes wrong when a domain action is forced into a CRUD shape?
Inputs, Outputs and Side Effects
▶ lab

Every operation, analysed the same six ways: inputs, state read, state changed, output, side effects, errors. Add item takes a product id and a quantity, reads the items and the catalog, changes one entry, returns the cart, has no side effects in V0, and fails on an unknown product or a non-positive quantity. Six answers per operation, and the function writes itself.

Q · An operation has a name and a sentence. What do you need to know about it before you can implement it — and how do you know when you have all of it?
Pure Logic First
▶ lab

Cart state plus an operation gives a new cart state. Get that right as a plain function in memory — no database, no network, no UI — because every one of those wraps behaviour that must already work, and each one added first makes the behaviour harder to see and impossible to test alone.

Q · The operations are analysed. Should the first implementation be an endpoint, a component, a table — or a function that takes a cart and returns a cart — and what does the order cost?
Operation Contracts

Each operation promises something and refuses something: add item promises one entry per product and refuses a non-positive quantity or an unknown product, leaving the cart untouched. Written as a contract — given, promises, refuses — the operation is already most of an API contract, and the endpoint can only keep promises the operation made.

Q · What does each operation promise its caller, what does it refuse, and how does a promise made in a function become one an endpoint can keep?