Booking backend project · Chapter 7 of 12
Browsing shows
By the end of this chapter you can write the public side of Parda's API: filter a list with conditions built from a request, search text with ILIKE safely, return one page of results with the total, and draw a seat map with left joins that keep every seat. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Selling seats
- 3 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of Booking backend project are free. This one comes with the all labs plan.
Why you need this
Rukhsana's React site has a home page of shows, a search box, filter chips for films, plays, comedy and music, and a seat map. Until now its api.js answered from JSON files baked into the site. For Diwali week the site must read Parda's real data: the 24 shows of the catalogue now, and Sayali plans 120 by December, with a page of 10 at a time on a phone. And the seat map must be live: a seat someone booked a minute ago cannot look free.
Everything so far was for admins. These routes are for everyone, signed in or not: GET /shows, GET /showtimes and GET /showtimes/:id/seats. The routes and controllers are given; the queries behind them 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.ts: pages of results
Write pageOf(query) and pageResult(items, total, p) in src/lib/pagination.ts.
pageOf: query holds page and limit as the URL gave them, as text or missing. Return { page, limit, offset }: page is a whole number from 1 (missing or empty text means 1); limit is a whole number from 1 (missing or empty means DEFAULT_LIMIT, 10), and above MAX_LIMIT (50) it is cut to 50; offset is how many rows the page skips, (page - 1) times limit. Anything else (abc, 0, -3, 1.5, 2abc) throws badRequest('page must be a whole number from 1 up'), or the same sentence for limit: a 400 with code bad_request.
pageResult: return { items, page, limit, total, pages }, where pages is how many pages the total makes with p.limit to a page, at least 1. pageResult of two items, a total of 24 and page 2 of 10 is { items, page: 2, limit: 10, total: 24, pages: 3 }.
The Check calls both, then GET /shows?limit=abc and GET /shows?page=0 through the app (400 each).
- Task 2 · interview question
Interview: browse.service.ts, find shows
Write listShows(filters, p) in browse.service.ts. filters may hold kind (only that kind), language (only that language, in any case: hindi finds Hindi) and q (only shows whose title contains those words, in any case). A missing filter keeps every show. p is pageOf's { page, limit, offset }.
Return pageResult(items, total, p): items is one page of shows ordered by title (then id), each with id, title, kind, language, minutes and certificate; total is how many shows match every filter, on all pages together.
Before q goes into the pattern, put a backslash before each backslash, percent sign and underscore in it, so that someone who types 100% finds no show instead of every show.
The Check lists the 24 seeded shows a page at a time (page 1 starts Aaji Rocks, page 3 is the last 4), filters Hindi films (4: Biryani Bros, Ghar Wapsi Express, Kaagaz Ki Nao, Lal Batti), searches ghar and THE, combines all three filters, and searches %, _ and 100% (0 each).
Nikhil will ask: q already travels to Postgres as a parameter, so it cannot inject SQL. Why escape the percent sign and the underscore at all?
- Task 3
browse.service.ts: the seat map of a showtime
Write getSeatMap(showtimeId) in browse.service.ts. No such showtime: throw notFound with a sentence (404).
Otherwise return { showtimeId, status, counts: { free, held, booked }, rows }, where status is the showtime's status and rows lists every seat of the showtime's screen, row by row in the layout's order (A first): { row, seats }, with each seat { id, label, class, price, status } by number. label is like E7 (seatLabel makes it); price is the showtime's price for the seat's class.
A seat's status is booked when booking_seats has it for this showtime; held when holds has it for this showtime with an expires_at still in the future; free otherwise, so an expired hold is free. Holds and bookings of other showtimes do not count. counts says how many seats have each status.
The Check holds E7 of showtime 9 (Ghar Wapsi Express), gives E8 a hold that expired yesterday, books E9, and holds and books two seats of another showtime on the same screen, then wants all 412 seats, E7 held, E8, E10, E11 and E12 free, E9 booked, A1 free at ₹120, and counts { free: 410, held: 1, booked: 1 }.
What you will learn
- Pagination: page and limit from the URL, limit and offset in SQL, and the total that "Page 2 of 3" needs.
- Optional filters: building a list of conditions and combining them.
- ILIKE and its wildcards: case-insensitive search, and escaping % and _.
- Left joins: keeping rows with no match, and where a condition must go.
More in Selling seats
Build "Browsing shows" yourself
Parda sells tickets for Pune's own cinemas and stages. You build its booking 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 Booking backend project are free. This one comes with the all labs plan.