forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiBaf331ecAhmet Kaan Gümüş2 months ago451 commits

class_course.rs59 lines, 2.5 KiBHistoryRawDownload
1//! The `class_course` table: the course-axis attach entry point and the
2//! attachment listing. The refusals-to-errors policy and the detach that turns
3//! a zero-row sweep into a 404 live in [`crate::service::class_course`]; the
4//! transaction itself is the pump's, [`crate::db::class_pump`].
5
6use crate::constant::CLASS_COURSE_TABLE;
7use crate::database::Database;
8use crate::db::class_pump::Attached;
9use crate::db::page::PagedList;
10use crate::domain::class_blueprint::ClassBlueprintId;
11use crate::domain::class_course::ClassCourse;
12use crate::domain::class_group::ClassGroupId;
13use crate::domain::course::CourseId;
14use crate::domain::user::UserId;
15use crate::error::AppError;
16
17/// The attach itself, with the refusals left *unmapped*.
18///
19/// A hand attach ([`crate::service::class_course::attach`]) turns each of them
20/// into the error the route answers with, because one call is one course and a
21/// refusal is that call's whole answer. A blueprint pump cannot: it runs
22/// one of these per (class, course), and a course that does not fit one
23/// section must not abort the other eleven — so it needs to *read* the
24/// refusal and carry on ([`crate::domain::class_blueprint`]).
25///
26/// `source` is the provenance tag, and it is written by the same statement
27/// that writes the link, so no attachment can exist without the answer to
28/// "may a blueprint take this back". It is also *claimed* in that
29/// transaction ([`Attached::SourceGone`]): a blueprint deleted while this
30/// pump ran has already swept by that tag, so a row landing afterwards
31/// would carry a name nothing can reach.
32pub(crate) async fn attach_sourced(
33 db: &Database,
34 class: &ClassGroupId,
35 course: &CourseId,
36 attached_by: &UserId,
37 source: Option<&ClassBlueprintId>,
38) -> Result<Attached<ClassCourse>, AppError> {
39 crate::db::class_pump::attach_course(db, class, course, attached_by, source).await
40}
41
42/// The courses a class is attached to, newest first — by when they were
43/// attached. The composite primary key is the tiebreaker (class, then course:
44/// the two halves of the id this order used to sort), which makes the order
45/// total.
46pub async fn list_for_class(
47 db: &Database,
48 class: &ClassGroupId,
49 limit: Option<i64>,
50 offset: i64,
51) -> Result<(Vec<ClassCourse>, i64), AppError> {
52 PagedList::new(
53 format!("{CLASS_COURSE_TABLE} WHERE class = $1"),
54 "ORDER BY attached_at DESC, class DESC, course DESC",
55 )
56 .bind(class.uuid())
57 .run(limit, offset, db)
58 .await
59}