Ordering backend project · Chapter 5 of 12
Admin: kitchens and menus
By the end of this chapter you can write the services behind DabbaDrop's admin routes: open a kitchen with its whole menu in one transaction, add, change and delete dishes with a SQLAlchemy session, turn Postgres's refusals into 404 and 409 answers, and put a whole router behind a login and an admin role. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Running the kitchens
- 3 tasks
- Python
- In the all labs plan
The first 2 chapters of Ordering backend project are free. This one comes with the all labs plan.
Why you need this
Bhavna runs DabbaDrop's kitchens. Every week something changes: Dadar Kitchen's Mutton Biryani is off because the supplier let them down, Andheri Kitchen's Paneer Butter Masala goes up by ₹10, a new dish joins a menu. Today she sends Kunal a WhatsApp message and he edits the shared sheet. For launch week a fourth kitchen opens in Powai, with its whole menu at once.
When Kunal added Thane Kitchen in September, his script stopped after the third of eight dishes: the kitchen was there with half a menu, and for an afternoon members in Thane could order only rice and dal. A kitchen must appear with its whole menu or not at all.
The API so far has accounts and roles (chapters 3 and 4) but nothing for an admin to do. This chapter adds the admin routes, and only Bhavna's account may use them.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
kitchens/service.py: a kitchen with its menu, all or nothing
Write create_kitchen(db, data) in app/modules/kitchens/service.py. data is a KitchenIn (name, area, dishes: a list of DishIn with name, category, price, available). In one transaction: add a Kitchen with the name and area trimmed and flush it so it has its id; add a Dish for every one of data.dishes, name trimmed, with the kitchen's id; commit once; return {"id", "name", "area", "dishes": how many dishes}.
If the flush or the commit fails with an IntegrityError (a taken kitchen name is refused at the flush, a dish listed twice at the commit), roll back, then raise conflict("kitchen_exists", ...) for a unique violation on kitchens_name_key, conflict("dish_exists", ...) for one on dishes_kitchen_name (a dish listed twice), and anything else on unchanged. Nothing may be left behind: not the kitchen, not a dish.
The Check creates Powai Kitchen with 3 dishes (id 4, its dishes in order, available), then a second Powai Kitchen (409 kitchen_exists) and Bandra Kitchen with Poha listed twice (409 dish_exists, and no Bandra Kitchen and none of its dishes in the database).
- Task 2
menus/service.py: add, change and delete a dish
Write three functions in app/modules/menus/service.py. add_dish(db, kitchen_id, data) adds a Dish from a DishIn (name trimmed) to that kitchen and returns dish_out(dish) (given); no such kitchen is not_found(...) (404); a name the kitchen already lists fails the commit on dishes_kitchen_name: roll back and raise conflict("dish_exists", ...).
update_dish(db, dish_id, changes) sets only the fields the request sent (changes.model_dump(exclude_unset=True)), commits and returns dish_out(dish); no such dish is a 404; a new name the kitchen already lists is a 409 dish_exists.
delete_dish(db, dish_id) deletes the dish and returns None; no such dish is a 404; a dish in an order makes the commit fail with a foreign key violation (order_items references it): roll back and raise conflict("dish_has_orders", ...).
The Check adds Pav Bhaji to Andheri Kitchen, tries kitchen 99 and a second Pav Bhaji, changes dish 2's price and then its availability (the other fields kept), tries dish 999 and renaming dish 2 to Chole, deletes Pav Bhaji, and tries deleting Dal Tadka (dish 1, in Advait's order: 409, and it stays) and dish 999.
- Task 3 · interview question
Interview: admin/router.py, admins only
Write the admin router in app/modules/admin/router.py: an APIRouter with prefix "/admin" and the dependency require_role("admin") for all its routes (dependencies=[...] on the router), and five routes, each with get_db's session: GET /admin/kitchens answers {"kitchens": list_kitchens(db)}; POST /admin/kitchens (a KitchenIn) 201 {"kitchen": create_kitchen(db, body)}; POST /admin/kitchens/{kitchen_id}/dishes (a DishIn) 201 {"dish": add_dish(...)}; PATCH /admin/dishes/{dish_id} (a DishPatch) {"dish": update_dish(...)}; DELETE /admin/dishes/{dish_id} 204 with no body.
The Check sends them signed out (401), as Tara (403, and her POST creates nothing) and as Bhavna: the list, a new kitchen, a dish for it, a new price, the delete (204, no body), deleting Dal Tadka (409) and changing dish 999 (404).
Kunal will ask: Tara's request to /admin/kitchens is refused with 403, not 401. What is the difference, and why does the role check sit on the router rather than in each service?
What you will learn
- Router, schema, service: where each part of a route lives in this project.
- A transaction over many rows: one session, flush for ids, one commit, and where each refusal surfaces.
- Finding, changing and deleting with the session: db.get, setting only the fields sent, db.delete.
- From a database error to an answer: a unique violation becomes a 409, a foreign key violation too, "no such row" a 404.
- Guarding a router: dependencies=[...] on the APIRouter, once for every route.
More in Running the kitchens
Build "Admin: kitchens and menus" yourself
DabbaDrop delivers tiffins across Mumbai. You build its ordering API, one module a chapter, and take the whole codebase home. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Ordering backend project are free. This one comes with the all labs plan.