forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits

menu.rs138 lines, 4.8 KiBHistoryRawDownload
1//! The kitchen's workflows: publishing and unpublishing menus, and every
2//! dish write. The queries live in [`crate::db::menu`] and
3//! [`crate::db::menu_dish`]; the row shapes and the date/slot/capacity
4//! rules in [`crate::domain::menu`] and [`crate::domain::menu_dish`].
5//!
6//! **No process-wide lock guards this domain.** The dish cap
7//! (`MAX_DISHES_PER_MENU`) used to be a count-then-write pair only a
8//! process mutex could make safe; it is now one guarded statement — the
9//! menu row is taken `FOR NO KEY UPDATE` inside the dish write's own
10//! transaction and the `INSERT … WHERE count(*) < cap` runs behind that
11//! lock, so two racing writers serialize on the row the database owns.
12//! Every dish write moves the menu's revision in that same transaction, and
13//! a booking claims its seat at the revision it priced itself against.
14
15use crate::constant::MAX_DISHES_PER_MENU;
16use crate::database::Database;
17use crate::db::{menu, menu_dish};
18use crate::domain::menu::{Menu, MenuDate, MenuId, MenuSlot};
19use crate::domain::menu_dish::{
20 DishDescription, DishName, DishPrice, DishTags, MenuDish, MenuDishId,
21};
22use crate::domain::user::UserId;
23use crate::error::AppError;
24
25/// Publish a menu for one day and meal slot. The caller has validated the
26/// date (a real calendar day, not already over), the slot (one the school
27/// serves) and the capacity; the day+slot duplicate refusal is decided by
28/// the store ([`menu::create`]).
29pub async fn create(
30 db: &Database,
31 date: MenuDate,
32 slot: MenuSlot,
33 capacity: Option<i64>,
34 created_by: &UserId,
35) -> Result<Menu, AppError> {
36 menu::create(db, date, slot, capacity, created_by).await
37}
38
39/// The menu, for callers that only inspect it — the web layer's handlers
40/// read through here.
41pub async fn read(db: &Database, id: &MenuId) -> Result<Option<Menu>, AppError> {
42 menu::read(db, id).await
43}
44
45/// Menus, newest day first, within inclusive `YYYY-MM-DD` bounds. `slot`,
46/// when given, narrows to menus published under that exact snapshotted
47/// slot text.
48pub async fn list(
49 db: &Database,
50 from: Option<&MenuDate>,
51 to: Option<&MenuDate>,
52 slot: Option<&str>,
53 limit: Option<i64>,
54 offset: i64,
55) -> Result<(Vec<Menu>, i64), AppError> {
56 menu::list(db, from, to, slot, limit, offset).await
57}
58
59/// Change a menu's seat cap; `None` keeps it, `Some(None)` clears it.
60pub async fn update(
61 db: &Database,
62 menu_row: Menu,
63 capacity: Option<Option<i64>>,
64) -> Result<Menu, AppError> {
65 menu::update(db, menu_row, capacity).await
66}
67
68/// Unpublish a menu, sweeping its dishes and attendance marks in the same
69/// transaction; refused while a seat is still held.
70pub async fn delete(db: &Database, menu_row: Menu) -> Result<Menu, AppError> {
71 menu::delete(db, menu_row).await
72}
73
74/// Add a dish to a menu, under the dish cap.
75///
76/// The cap is the database's to enforce, not this process's: the write
77/// locks the menu row `FOR NO KEY UPDATE` and its `INSERT` carries a
78/// `count(*) < cap` guard, so a writer that queued behind a concurrent one
79/// re-counts after the lock and is refused (`409`) instead of overfilling
80/// — the count and the insert are one decision, with no window a second
81/// writer can slip through.
82///
83/// The *price* needs no lock — a dish write moves the menu's revision, and
84/// a booking claims its seat at the revision it priced itself against.
85pub async fn add_dish(
86 db: &Database,
87 menu_id: &MenuId,
88 name: DishName,
89 description: Option<DishDescription>,
90 price_minor: DishPrice,
91 tags: DishTags,
92) -> Result<MenuDish, AppError> {
93 menu_dish::create(
94 db,
95 menu_id,
96 name,
97 description,
98 price_minor,
99 tags,
100 MAX_DISHES_PER_MENU as i64,
101 )
102 .await
103}
104
105/// One dish, for callers that only inspect it.
106pub async fn read_dish(db: &Database, id: &MenuDishId) -> Result<Option<MenuDish>, AppError> {
107 menu_dish::read(db, id).await
108}
109
110/// Every dish on one menu, in the order they were added.
111pub async fn list_dishes(db: &Database, menu_id: &MenuId) -> Result<Vec<MenuDish>, AppError> {
112 menu_dish::list_for_menu(db, menu_id).await
113}
114
115/// Dishes for a whole page of menus in one round trip.
116pub async fn list_dishes_for_menus(
117 db: &Database,
118 menus: &[MenuId],
119) -> Result<Vec<MenuDish>, AppError> {
120 menu_dish::list_for_menus(db, menus).await
121}
122
123/// Edit a dish. Only what the request carried is written.
124pub async fn update_dish(
125 db: &Database,
126 dish: MenuDish,
127 name: Option<DishName>,
128 description: Option<Option<DishDescription>>,
129 price_minor: Option<DishPrice>,
130 tags: Option<DishTags>,
131) -> Result<MenuDish, AppError> {
132 menu_dish::update(db, dish, name, description, price_minor, tags).await
133}
134
135/// Remove a dish from its menu.
136pub async fn delete_dish(db: &Database, dish: MenuDish) -> Result<MenuDish, AppError> {
137 menu_dish::delete(db, dish).await
138}