hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits
| 1 | //! The `course_offering` table: the grade-level course template and the reads |
| 2 | //! and writes behind `/offerings`. The row shape lives in |
| 3 | //! [`crate::domain::course_offering`]; the workflows (the class-delivered |
| 4 | //! gate, the duplicate refusal) in [`crate::service::course_offering`]. |
| 5 | //! |
| 6 | //! One row per (course, grade_level) — the `UNIQUE` key the attach pump's |
| 7 | //! auto-create races on: [`ensure_tx`] inserts `ON CONFLICT DO NOTHING` and |
| 8 | //! reads the winner's id back, so two attaches landing together share one |
| 9 | //! template whatever the interleaving was. |
| 10 | //! |
| 11 | //! Every content column is nullable by design — `NULL` means *inherit* (from |
| 12 | //! the catalog course, or from the constants), never "missing". The update is |
| 13 | //! field-scoped in the `set_or_clear` shape the other PATCH handlers speak: |
| 14 | //! an absent field keeps its column, an explicit `null` clears back to |
| 15 | //! inherit, a value sets the override. |
| 16 | |
| 17 | use PgConnection; |
| 18 | |
| 19 | use crate; |
| 20 | use cratePagedList; |
| 21 | use crateDersSaati; |
| 22 | use crate; |
| 23 | use crate; |
| 24 | use crateGradeLevel; |
| 25 | use crateTimestamp; |
| 26 | use crateUserId; |
| 27 | use crateAppError; |
| 28 | |
| 29 | /// One offering as the *runtime* row decode sees it. Identical to |
| 30 | /// [`CourseOffering`] except `default_ders_saati` stays the raw `SMALLINT` |
| 31 | /// (`Option<i16>`): the runtime [`sqlx::FromRow`] path cannot name the |
| 32 | /// `::bigint` cast the static `query_as!` projections use, and |
| 33 | /// [`DersSaati`]'s transparent `i64` would refuse a `SMALLINT` at decode. |
| 34 | /// [`OfferingRow::into_course_offering`] does the two-step conversions the |
| 35 | /// column `CHECK`s already guarantee. |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | /// One offering by id, or `None` when the id names no row. |
| 71 | pub async |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | /// Load every offering behind `ids` (one query) — the batch half of the |
| 93 | /// resolved-content reads (`service::instance_resolve::resolved_content`), |
| 94 | /// which map a page of instances onto their templates without an N+1. |
| 95 | pub async |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | /// The offering a (course, grade_level) pair resolves to, or `None` when no |
| 124 | /// template exists there yet — the read behind "what does this grade teach |
| 125 | /// from" outside a pump transaction. |
| 126 | pub async |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | /// The offerings, grade ladder first, paged via `limit`/`offset`; both |
| 153 | /// filters are optional and combine. The page and its count share the same |
| 154 | /// predicate ([`PagedList`]). |
| 155 | pub async |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | /// Mint the template. The duplicate (course, grade_level) answer is a **409 |
| 185 | /// `offering_exists`**, not a store error: a manager creating the same |
| 186 | /// template twice wants to be told it exists, and the attach pump's |
| 187 | /// auto-create would have silently shared the row instead. |
| 188 | // the offering row, spelled field by field |
| 189 | pub async |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | /// Field-scoped PATCH in the `set_or_clear` shape: an absent field keeps its |
| 251 | /// column, `Some(None)` clears back to inherit (`NULL`), `Some(Some(v))` sets |
| 252 | /// the override. `updated_at` moves with every write that changes anything. |
| 253 | /// An empty request writes nothing and reads the row back. `Err(NotFound)` |
| 254 | /// when the offering is gone. |
| 255 | pub async |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | /// The offering's two defaults, straight off its row — `(default_ders_saati, |
| 329 | /// default_counts_toward_karne)`, `None`s *included* (a `NULL` default is the |
| 330 | /// inherit state, and the caller layers its own fallbacks on top). `None` when |
| 331 | /// the id names no offering: the `RESTRICT` foreign key on `class_course` |
| 332 | /// keeps every instance's offering alive, so a missing row is the honest 404 |
| 333 | /// the service turns into one, not a case to paper over. |
| 334 | pub async |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | /// Delete the template — refused while any instance still teaches from it. |
| 350 | /// The refusal is a **409 `offering_in_use`** (the foreign key's `RESTRICT` |
| 351 | /// is the store's mirror of the same rule); a gone id is the usual 404. One |
| 352 | /// conditional statement is the whole invariant, so a check-then-delete race |
| 353 | /// cannot exist: the `NOT EXISTS` is judged on the row as it is *at the |
| 354 | /// delete*. |
| 355 | pub async |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | /// Resolve — or mint empty — the template a (course, grade_level) pair |
| 389 | /// teaches from, inside the caller's transaction. The attach pump's helper: |
| 390 | /// every instance insert needs its offering link, and an untouched grade |
| 391 | /// mints an *empty* template (every content column `NULL`) so it inherits the |
| 392 | /// catalog course until a manager fills it in. `created_by` is the actor |
| 393 | /// whose attach asked. |
| 394 | /// |
| 395 | /// The `ON CONFLICT DO NOTHING` + read-back pair is the race-closer: two |
| 396 | /// attaches landing on the same (course, grade_level) together both insert, |
| 397 | /// one wins, and the loser's read-back returns the winner's row — one |
| 398 | /// template per pair, whatever the order was. The table's only `UNIQUE` |
| 399 | /// constraint is that pair, so the bare `DO NOTHING` cannot swallow a |
| 400 | /// different conflict. |
| 401 | pub async |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 |