Ordering backend project · Chapter 7 of 12
Browsing menus and slots
By the end of this chapter you can write the public side of DabbaDrop's API: return one page of results with the total, filter a list with conditions built from a request, search names word by word with ILIKE, order a page so that no row is lost between pages, and count the places left in each slot with a subquery per row. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Taking orders
- 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
Members order on their phones. Before they can, they need two lists. The menu: 29 dishes members can order today across three kitchens, and Farah wants a hundred by the new year, shown 10 at a time with a category filter (veg, Jain, non-veg) and a search box. And the slots: a member in Goregaon picks Monday and sees 12:00, 12:30 and 1:00 pm, each with the places left. On 19 October the sheet said nothing of the kind, and 52 people ordered for a 12:30 slot with 40 places.
Everything so far was for Bhavna. These routes are for everyone, signed in or not: GET /dishes, GET /zones and GET /zones/{zone_id}/slots. The router is given; the queries behind it are yours.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
pagination.py: pages of results
Write two functions in app/lib/pagination.py. page_of(page=1, limit=10) returns the given Page(page, limit, offset): limit cut to MAX_LIMIT (50), offset the rows of the pages before (page_of(3, 20) is Page(page=3, limit=20, offset=40)). A page below 1 raises bad_request("page starts at 1"), a limit below 1 bad_request("limit must be at least 1"), both 400.
page_result(items, total, page) returns {"items": items, "page": page.page, "limit": page.limit, "total": total, "pages": the number of pages of page.limit rows that hold total rows, rounded up, 0 when total is 0}.
The Check calls page_of with no arguments, (3, 20), (2, 500), (1, 50) and four bad pairs, page_result with totals of 31, 30, 0 and 1, then sends GET /dishes?page=0 (400) and GET /dishes?limit=500 (limit 50).
- Task 2 · interview question
Interview: browse/service.py, find dishes
Write list_dishes(db, page, kitchen_id=None, category=None, q=None) in app/modules/browse/service.py. It finds the available dishes and, when given, only that kitchen's, only that category, and only those whose name has every word of q in it, in any case ("DAL tadka" finds both Dal Tadka, not Jain Dal; extra spaces in q do not matter; % and _ in q are plain characters, so q="%" finds no dish). Sort by name, then id; take page.limit rows from page.offset. Each item is {"id", "kitchen_id", "kitchen" (the kitchen's name), "name", "category", "price"}. Return page_result(items, total, page), where total counts every match, not only this page.
The Check compares page 1 and page 3 with the 29 available dishes (Mutton Biryani is not), then filters by category jain, by q "dal", "DAL tadka" and " tadka dal ", by kitchen 3 with q "dal", by kitchen 2 with category non-veg, and by q "pizza" (none); then sends GET /dishes?category=jain.
Kunal will ask: why does the page need its own count query, and why sort by id after name?
- Task 3
browse/service.py: the places left in a zone's slots
Write list_slots(db, zone_id, day) in app/modules/browse/service.py. No such zone is not_found(...) (404). Find the zone's slots that start on that day in Mumbai (from 00:00 Mumbai time that day to 00:00 the next), sorted by starts_at, each as {"id", "starts_at", "ends_at", "capacity", "status", "places_left"}. places_left is capacity minus the slot's live holds (expires_at after now) minus its pending or confirmed orders; a closed slot has 0. Return {"zone": {"id", "name"}, "day": day.isoformat(), "slots": [...]}.
The Check gives slot 17 a live hold, an expired hold, a pending and a cancelled order, closes slot 18 and adds a slot at 00:00 on 3 Nov, then wants Goregaon's 2 Nov to be slots 16, 17, 18 with [40, 38, 0] places left, 3 Nov to start with the midnight slot, Malad's 12:00 to have 39 (Advait's paid order), zone 99 to be a 404, and GET /zones/2/slots?day=2026-11-02 to answer 200.
What you will learn
- Pagination: limit and offset in SQL, the total that "page 2 of 3" needs, and pages rounded up.
- Query parameters in FastAPI: typed function parameters, and the 400 when a value is not a number.
- Optional filters: a list of conditions, and a where that takes them all.
- ILIKE, word by word: a case-insensitive match for each word typed.
- A full order: why a page sorted by a name that two dishes share loses rows.
- A count per row: a correlated scalar subquery, and Mumbai's day as two moments.
More in Taking orders
Build "Browsing menus and slots" 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.