Booking backend project · Chapter 5 of 12
Admin: venues and shows
By the end of this chapter you can write the services behind Parda's admin screens: create a venue with every seat in one transaction, add, change and delete shows with Drizzle, turn Postgres's refusals into 404 and 409 answers, and put a 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 Parda
- 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
Every week Asha, who runs Parda's operations, adds what is new: venues that join Parda, the films and plays they will show. Today she sends Nikhil a spreadsheet and he types SQL. For Diwali week three venues join at once: Sur Sangam Hall in Deccan (280 seats), Natya Ghar in Shivajinagar (240) and Sahyadri Lawns in Aundh (870). That is 1,390 seat rows, and nobody types those by hand.
When Nikhil added Moti Mahal Talkies in September, his script stopped halfway through the balcony: the venue was there with 340 of its 460 seats, the site showed it, and the missing rows looked sold out. He fixed it with three more SQL statements at midnight. A venue must appear whole 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 Asha'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
venues.service.ts: a venue with all its seats, all or nothing
Write createVenue(input) in venues.service.ts. input is { name, area, kind, screens }, and each screen is { name, layout }, a layout being rows like { row: 'A', seats: 18, class: 'Front' }.
In one transaction: insert the venue; for each screen, insert it with the venue's id, then insert every seat of its layout (seatsForLayout from chapter 2 makes them) with the screen's id.
Return { id, name, area, kind, screens }, where screens has one { id, name, seats } per screen given, in order, and seats is how many seats that screen got. The Check adds Sur Sangam Hall, Deccan (a hall of 14 rows of 20, so 280 seats) and reads the seats back.
When anything fails, nothing may stay: no venue, no screen, no seat. A unique violation (a venue name that exists, or one screen name twice in a venue) becomes conflict('venue_exists', a sentence), a 409; any other error is thrown on as it is. The Check tries Natya Ghar with its screen name Main stage twice, Kala Talkies again, and a layout with row A twice, and counts the rows after each.
- Task 2
shows.service.ts: add, change and delete a show
Write three functions in shows.service.ts.
createShow(input): input is { title, kind, language, minutes, certificate }. Insert it and return the whole new row, with its id and createdAt.
updateShow(id, changes): changes holds only the fields to change. Change them on the show with that id only, and return the whole updated row. When there is no such show, throw notFound('There is no show ' + id), a 404 with code not_found.
deleteShow(id): when the show has showtimes, throw conflict('show_has_showtimes', a sentence), a 409, and keep it. Otherwise delete it; when there is no such show, throw the same notFound. It returns nothing.
The Check adds Diwali Dhamaka (comedy, Hindi, 80 minutes, UA 13+), changes its minutes to 85 and makes sure no other show changed, updates show 999, tries to delete Ghar Wapsi Express (show 1, which has showtimes), deletes Diwali Dhamaka, and deletes show 999.
- Task 3 · interview question
Interview: admin.routes.ts, admins only
Write adminRouter in admin.routes.ts, the Express Router that app.ts mounts. For every path under /admin, first requireAuth and then requireRole('admin') (chapter 4's middleware): signed out is 401, a member such as Neha is 403, and nothing else runs for them.
Then five routes, each with the given validate and controller: GET /admin/venues with listVenuesHandler; POST /admin/venues with a body of createVenueSchema and createVenueHandler; POST /admin/shows with createShowSchema and createShowHandler; PATCH /admin/shows/:id with params idParams and a body of updateShowSchema, and updateShowHandler; DELETE /admin/shows/:id with params idParams and deleteShowHandler.
The Check calls the whole app: signed out, as Neha and as Asha. As Asha it sends each route a body or id its schema refuses and wants the validation's 400, which shows the route and its validation are there.
Nikhil will ask: why is a member who is logged in told 403, and a visitor who is not logged in told 401?
What you will learn
- Controller, service, router: where each piece of a route lives in this project.
- Transactions: db.transaction, all or nothing, and why every query inside goes through tx.
- Insert, update and delete with returning: getting the rows back, and seeing "not found".
- From a database error to an answer: a unique violation becomes a 409 your client can read.
- Guarding a router: requireAuth then requireRole('admin'), in the right order.
More in Running Parda
Build "Admin: venues and 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.