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 `exam_attempt` table: sitting reads, the two field-scoped writes |
| 2 | //! (`finished_at`, `left_at`), the guarded sitting create, and the freeze |
| 3 | //! gate — the in-transaction check every exam-child write runs before it |
| 4 | //! touches anything. |
| 5 | |
| 6 | use PgConnection; |
| 7 | |
| 8 | use crate; |
| 9 | use crateClaimed; |
| 10 | use crate; |
| 11 | use crate; |
| 12 | use crateTimestamp; |
| 13 | use crateUserId; |
| 14 | use crateAppError; |
| 15 | |
| 16 | /// The 409 the freeze gate answers — the same one the handlers' pre-flight |
| 17 | /// check ([`crate::service::exam_question::ensure_questions_editable`]) |
| 18 | /// answers, so a client cannot tell which of the two refused. |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | /// The freeze gate, run inside a caller's transaction on that caller's own |
| 24 | /// connection: take the exam row `FOR UPDATE`, then refuse unless the exam |
| 25 | /// still exists and nobody has started an attempt. |
| 26 | /// |
| 27 | /// This is the whole of the old `write_unfrozen` machinery — the |
| 28 | /// `questions_frozen` THROW and the bump-and-restore existence touch — with |
| 29 | /// Postgres doing the work the tricks stood in for. The row lock makes the |
| 30 | /// check-and-write one unit against [`crate::db::exam::delete`] (which locks |
| 31 | /// the same row before its cascade) and against a sitting create (whose |
| 32 | /// guard below locks it too): a question edit can no longer sail past an |
| 33 | /// attempt that started in the gate's gap, and a child whose exam died |
| 34 | /// mid-flight finds no row here and is the same 404 the old `no_exam` THROW |
| 35 | /// answered with. Real foreign keys retire the bump-and-restore: the child |
| 36 | /// inserts themselves refuse a parent that is gone. |
| 37 | pub async |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | /// Stamp the submission time. The caller has already checked the deadline |
| 60 | /// and that the attempt isn't finished. |
| 61 | /// |
| 62 | /// Writes *only* `finished_at`, for the mirror image of |
| 63 | /// [`set_left`]'s reason: the submit path reads the attempt, |
| 64 | /// then awaits its deadline and already-finished checks before writing, and |
| 65 | /// the exam room stamps or clears `left_at` on the same row from a socket. |
| 66 | /// A whole-row write from the pre-read snapshot would carry its stale |
| 67 | /// `left_at` back over that stamp, erasing the recorded walk-out. |
| 68 | pub async |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | /// Stamp (or clear) the walked-out marker. The exam room sets it when the |
| 89 | /// student's socket closes mid-attempt and clears it when they come back. |
| 90 | /// |
| 91 | /// Writes *only* the `left_at` field (never the whole row): the room's |
| 92 | /// teardown reads the attempt, sees it in progress, then stamps here — and |
| 93 | /// a submission that lands in that gap must survive. A whole-row write from |
| 94 | /// the pre-read snapshot would carry its blank `finished_at` back over the |
| 95 | /// fresh submission, silently un-submitting the exam (and, with |
| 96 | /// `allow_rejoin` off, locking the student out). |
| 97 | pub async |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | /// Stamp `left_at` on one sitting, but only while it is genuinely in |
| 122 | /// progress: nothing stamped yet, nothing submitted, and the exam's *live* |
| 123 | /// schedule has not run past the sitting's deadline. One conditional |
| 124 | /// statement — the exam-room teardown's whole stamp, with the re-derive |
| 125 | /// [`crate::domain::exam_attempt::ExamAttempt::status`] used to do folded |
| 126 | /// into the `WHERE` (joined to `exam` so a deleted exam's sitting is simply |
| 127 | /// not stamped, and concurrent last-outs are harmless: only the first finds |
| 128 | /// `left_at IS NULL`). |
| 129 | pub async |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | /// One sitting by id — the exam room re-reads its own attempt this way, |
| 153 | /// so a retake started elsewhere can never be mistaken for it. |
| 154 | pub async |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | /// The student's current sitting — the highest `seq` for the pair. All |
| 172 | /// reads that used to mean "the attempt" mean this now. |
| 173 | pub async |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | /// Every sitting of `user` at `exam`, newest first. |
| 196 | pub async |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | /// Every sitting of `user` nobody has submitted yet, across all exams. Only |
| 218 | /// the *candidates* for "in progress": a deadline comes off the exam's live |
| 219 | /// schedule, so the caller still judges [`ExamAttempt::status`] per exam rather |
| 220 | /// than re-spelling that rule in SQL. |
| 221 | pub async |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | pub async |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | /// Whether anyone has started this exam — the gate that freezes `mode` |
| 257 | /// edits once an attempt exists. |
| 258 | pub async |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | /// The sitting-create guard: the exam row locked `FOR UPDATE` and |
| 269 | /// re-judged *inside the inserting transaction* — draft (a 404, a draft is |
| 270 | /// invisible to students), no mode (a 409: visible, nothing to sit), the |
| 271 | /// window not yet open, the window already closed. This is what the old |
| 272 | /// writer lease `EXAM_LOCK` used to buy the sittable/window gates: they are |
| 273 | /// now judged against the locked row the attempt lands under, so a mode |
| 274 | /// change or re-draft cannot slip between a gate and the insert. The |
| 275 | /// refusal texts are [`crate::service::exam_attempt`]'s own — the same |
| 276 | /// words that pre-flight answers, so a client cannot tell which fired. |
| 277 | pub async |
| 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 | /// Insert the sitting row, claiming the student's `exam_sat_total` in the |
| 312 | /// same statement when — and only when — this is a first sitting (`seq == |
| 313 | /// 1`): that counter is *exams sat*, not sittings, and a retake counts |
| 314 | /// nothing. The claim-CTE recipe ([`cap`]): bump the counter row, insert |
| 315 | /// the child only if the bump landed, and let the sitting's natural |
| 316 | /// composite primary key (`exam_attempt_exam_user_seq`) answer "another |
| 317 | /// writer started this very sitting first" as `Claimed::Duplicate`. The |
| 318 | /// bump rides the insert's own transaction, so a refused insert takes its |
| 319 | /// increment straight back. |
| 320 | /// |
| 321 | /// `Claimed::Full` here can only mean the student's account row is missing |
| 322 | /// — nothing caps sittings — which the caller surfaces as its own internal. |
| 323 | pub async |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | /// The sitting create as one transaction: [`guard_start`], then |
| 360 | /// [`create_in`]. Retried while Postgres answers "contended"; a duplicate |
| 361 | /// or a missing student row is a verdict, not a conflict, and is never |
| 362 | /// re-sent. |
| 363 | pub async |
| 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 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 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 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 |