Params structs for the d8dad92 list doors
Browse files at this commit62f10ea7df748cc00a5e98532b352f03610c5632Parent: 57527df- fix
- params structs for the d8dad92 list doors
8 changed files +231 -160
- src/db/appointment.rs +25 -6
- src/db/exam.rs +29 -13
- src/db/pomodoro.rs +1 -1
- src/service/appointment.rs +96 -61
- src/service/exam.rs +39 -41
- src/web/appointments.rs +8 -6
- src/web/courses.rs +23 -8
- src/web/exams/mod.rs +10 -24
src/db/appointment.rsmodified+25 -6
| @@ -132,6 +132,22 @@ where | |||
| 132 | Ok(appointment) | 132 | Ok(appointment) |
| 133 | } | 133 | } |
| 134 | 134 | ||
| 135 | +/// The optional filters and the page of [`list_for_requester`], in one | ||
| 136 | +/// struct — the fields outgrew a readable argument list. The default is | ||
| 137 | +/// the unfiltered, unpaged read. | ||
| 138 | +#[derive(Default)] | ||
| 139 | +pub struct RequesterListParams<'a> { | ||
| 140 | + pub status: Option<AppointmentStatus>, | ||
| 141 | + /// The meeting's effective start bounds, `[after, before)`. | ||
| 142 | + pub starts_after: Option<Timestamp>, | ||
| 143 | + pub starts_before: Option<Timestamp>, | ||
| 144 | + /// The booked slot's teacher. | ||
| 145 | + pub teacher: Option<&'a UserId>, | ||
| 146 | + /// `None` = the full list. | ||
| 147 | + pub limit: Option<i64>, | ||
| 148 | + pub offset: i64, | ||
| 149 | +} | ||
| 150 | + | ||
| 135 | /// The caller's own bookings, newest first. The optional filters narrow the | 151 | /// The caller's own bookings, newest first. The optional filters narrow the |
| 136 | /// read: `status` matches the stored state, `starts_after`/`starts_before` | 152 | /// read: `status` matches the stored state, `starts_after`/`starts_before` |
| 137 | /// bound the meeting's effective start — a standing proposal, the slot's | 153 | /// bound the meeting's effective start — a standing proposal, the slot's |
| @@ -145,13 +161,16 @@ where | |||
| 145 | pub async fn list_for_requester( | 161 | pub async fn list_for_requester( |
| 146 | db: &Database, | 162 | db: &Database, |
| 147 | requester: &UserId, | 163 | requester: &UserId, |
| 148 | - status: Option<AppointmentStatus>, | 164 | + params: RequesterListParams<'_>, |
| 149 | - starts_after: Option<Timestamp>, | ||
| 150 | - starts_before: Option<Timestamp>, | ||
| 151 | - teacher: Option<&UserId>, | ||
| 152 | - limit: Option<i64>, | ||
| 153 | - offset: i64, | ||
| 154 | ) -> Result<(Vec<Appointment>, i64), AppError> { | 165 | ) -> Result<(Vec<Appointment>, i64), AppError> { |
| 166 | + let RequesterListParams { | ||
| 167 | + status, | ||
| 168 | + starts_after, | ||
| 169 | + starts_before, | ||
| 170 | + teacher, | ||
| 171 | + limit, | ||
| 172 | + offset, | ||
| 173 | + } = params; | ||
| 155 | // Spell the appointment columns against the join alias, so the derived | 174 | // Spell the appointment columns against the join alias, so the derived |
| 156 | // table's row keeps the exact names [`Appointment`] decodes and the | 175 | // table's row keeps the exact names [`Appointment`] decodes and the |
| 157 | // slot's duplicate column names never leak in. The plain shape aliases | 176 | // slot's duplicate column names never leak in. The plain shape aliases |
src/db/exam.rsmodified+29 -13
| @@ -206,6 +206,26 @@ pub async fn read(db: &Database, id: &ExamId) -> Result<Option<Exam>, AppError> | |||
| 206 | .await?) | 206 | .await?) |
| 207 | } | 207 | } |
| 208 | 208 | ||
| 209 | +/// The schedule window and the page of [`list_windowed`], in one struct — | ||
| 210 | +/// the fields outgrew a readable argument list. The four optional bounds | ||
| 211 | +/// are [`crate::web::page::WindowParams`]'s schedule window, validated | ||
| 212 | +/// upstream. The default is the windowless, unpaged read: all four bounds | ||
| 213 | +/// absent keeps `id DESC` (newest first); any bound set drops window-less | ||
| 214 | +/// exams (no `mode` → both schedule columns null, so every predicate is | ||
| 215 | +/// `NULL`-rejecting) and flips the order to ascending by schedule — | ||
| 216 | +/// `starts_at`, falling back to `ends_at`, ties by id — the order | ||
| 217 | +/// `WindowParams::apply` used to produce in Rust. | ||
| 218 | +#[derive(Default)] | ||
| 219 | +pub struct WindowedListParams { | ||
| 220 | + pub starts_after: Option<i64>, | ||
| 221 | + pub ends_after: Option<i64>, | ||
| 222 | + pub starts_before: Option<i64>, | ||
| 223 | + pub ends_before: Option<i64>, | ||
| 224 | + /// `None` = the full list. | ||
| 225 | + pub limit: Option<i64>, | ||
| 226 | + pub offset: i64, | ||
| 227 | +} | ||
| 228 | + | ||
| 209 | /// The exams list behind `GET /exams`, paged by the database — visibility, | 229 | /// The exams list behind `GET /exams`, paged by the database — visibility, |
| 210 | /// the draft rule, the schedule window, the `LIMIT/OFFSET` and the `total` | 230 | /// the draft rule, the schedule window, the `LIMIT/OFFSET` and the `total` |
| 211 | /// count all share one `WHERE` per branch, so a filtered page never decodes | 231 | /// count all share one `WHERE` per branch, so a filtered page never decodes |
| @@ -216,24 +236,20 @@ pub async fn read(db: &Database, id: &ExamId) -> Result<Option<Exam>, AppError> | |||
| 216 | /// exam), with `NOT draft OR owner ∈ managed`, today's rule unchanged — a | 236 | /// exam), with `NOT draft OR owner ∈ managed`, today's rule unchanged — a |
| 217 | /// draft shows only to someone who manages its owner instance. Both `None` is | 237 | /// draft shows only to someone who manages its owner instance. Both `None` is |
| 218 | /// the manager+ whole-table read. | 238 | /// the manager+ whole-table read. |
| 219 | -/// | ||
| 220 | -/// The four optional bounds are [`crate::web::page::WindowParams`]'s schedule | ||
| 221 | -/// window, validated upstream. All four absent keeps `id DESC` (newest | ||
| 222 | -/// first); any bound set drops window-less exams (no `mode` → both schedule | ||
| 223 | -/// columns null, so every predicate is `NULL`-rejecting) and flips the order | ||
| 224 | -/// to ascending by schedule — `starts_at`, falling back to `ends_at`, ties by | ||
| 225 | -/// id — the order `WindowParams::apply` used to produce in Rust. | ||
| 226 | pub async fn list_windowed( | 239 | pub async fn list_windowed( |
| 227 | db: &Database, | 240 | db: &Database, |
| 228 | visible: Option<&[ClassCourseId]>, | 241 | visible: Option<&[ClassCourseId]>, |
| 229 | managed: Option<&[ClassCourseId]>, | 242 | managed: Option<&[ClassCourseId]>, |
| 230 | - starts_after: Option<i64>, | 243 | + params: WindowedListParams, |
| 231 | - ends_after: Option<i64>, | ||
| 232 | - starts_before: Option<i64>, | ||
| 233 | - ends_before: Option<i64>, | ||
| 234 | - limit: Option<i64>, | ||
| 235 | - offset: i64, | ||
| 236 | ) -> Result<(Vec<Exam>, i64), AppError> { | 244 | ) -> Result<(Vec<Exam>, i64), AppError> { |
| 245 | + let WindowedListParams { | ||
| 246 | + starts_after, | ||
| 247 | + ends_after, | ||
| 248 | + starts_before, | ||
| 249 | + ends_before, | ||
| 250 | + limit, | ||
| 251 | + offset, | ||
| 252 | + } = params; | ||
| 237 | // Window predicates and binds move together: each bound appends its | 253 | // Window predicates and binds move together: each bound appends its |
| 238 | // predicate with the placeholder number its bind position lands on. The | 254 | // predicate with the placeholder number its bind position lands on. The |
| 239 | // two visibility binds of the non-manager branch claim $1 and $2, so the | 255 | // two visibility binds of the non-manager branch claim $1 and $2, so the |
src/db/pomodoro.rsmodified+1 -1
| @@ -440,7 +440,7 @@ mod tests { | |||
| 440 | 440 | ||
| 441 | /// The whole unwindowed log — the read the old `list_for_user` gave — | 441 | /// The whole unwindowed log — the read the old `list_for_user` gave — |
| 442 | /// for tests that just want the sessions. | 442 | /// for tests that just want the sessions. |
| 443 | - async fn whole_log<'a>(user: &UserId, db: &Database) -> Vec<PomodoroSession> { | 443 | + async fn whole_log(user: &UserId, db: &Database) -> Vec<PomodoroSession> { |
| 444 | page_for_user(db, user, None, None, None, 0) | 444 | page_for_user(db, user, None, None, None, 0) |
| 445 | .await | 445 | .await |
| 446 | .unwrap() | 446 | .unwrap() |
src/service/appointment.rsmodified+96 -61
| @@ -32,6 +32,8 @@ use crate::domain::timestamp::Timestamp; | |||
| 32 | use crate::domain::user::UserId; | 32 | use crate::domain::user::UserId; |
| 33 | use crate::error::{AppError, ValidationError}; | 33 | use crate::error::{AppError, ValidationError}; |
| 34 | 34 | ||
| 35 | +pub use crate::db::appointment::RequesterListParams; | ||
| 36 | + | ||
| 35 | /// Request `slot`. Lands `pending`: publishing availability is not consent | 37 | /// Request `slot`. Lands `pending`: publishing availability is not consent |
| 36 | /// to a specific person and topic. | 38 | /// to a specific person and topic. |
| 37 | /// | 39 | /// |
| @@ -126,24 +128,9 @@ pub async fn read(db: &Database, id: &AppointmentId) -> Result<Option<Appointmen | |||
| 126 | pub async fn list_for_requester( | 128 | pub async fn list_for_requester( |
| 127 | db: &Database, | 129 | db: &Database, |
| 128 | requester: &UserId, | 130 | requester: &UserId, |
| 129 | - status: Option<AppointmentStatus>, | 131 | + params: RequesterListParams<'_>, |
| 130 | - starts_after: Option<Timestamp>, | ||
| 131 | - starts_before: Option<Timestamp>, | ||
| 132 | - teacher: Option<&UserId>, | ||
| 133 | - limit: Option<i64>, | ||
| 134 | - offset: i64, | ||
| 135 | ) -> Result<(Vec<Appointment>, i64), AppError> { | 132 | ) -> Result<(Vec<Appointment>, i64), AppError> { |
| 136 | - appointment::list_for_requester( | 133 | + appointment::list_for_requester(db, requester, params).await |
| 137 | - db, | ||
| 138 | - requester, | ||
| 139 | - status, | ||
| 140 | - starts_after, | ||
| 141 | - starts_before, | ||
| 142 | - teacher, | ||
| 143 | - limit, | ||
| 144 | - offset, | ||
| 145 | - ) | ||
| 146 | - .await | ||
| 147 | } | 134 | } |
| 148 | 135 | ||
| 149 | /// Every booking aimed at `teacher`, newest first — the request inbox, | 136 | /// Every booking aimed at `teacher`, newest first — the request inbox, |
| @@ -909,13 +896,17 @@ mod tests { | |||
| 909 | 896 | ||
| 910 | // Absent: all three, newest first — the shape today's callers see. | 897 | // Absent: all three, newest first — the shape today's callers see. |
| 911 | let (rows, total) = | 898 | let (rows, total) = |
| 912 | - list_for_requester(&db, &veli, None, None, None, None, None, 0).await.unwrap(); | 899 | + list_for_requester(&db, &veli, RequesterListParams::default()).await.unwrap(); |
| 913 | assert_eq!((rows.len(), total), (3, 3)); | 900 | assert_eq!((rows.len(), total), (3, 3)); |
| 914 | 901 | ||
| 915 | // status: only the approved one, and total is the filtered count. | 902 | // status: only the approved one, and total is the filtered count. |
| 916 | - let (rows, total) = list_for_requester(&db, &veli, Some(AppointmentStatus::Approved), None, None, None, | 903 | + let (rows, total) = list_for_requester( |
| 917 | - None, | 904 | + &db, |
| 918 | - 0, | 905 | + &veli, |
| 906 | + RequesterListParams { | ||
| 907 | + status: Some(AppointmentStatus::Approved), | ||
| 908 | + ..Default::default() | ||
| 909 | + }, | ||
| 919 | ) | 910 | ) |
| 920 | .await | 911 | .await |
| 921 | .unwrap(); | 912 | .unwrap(); |
| @@ -923,25 +914,41 @@ mod tests { | |||
| 923 | assert_eq!(rows[0].get_id(), approved.get_id()); | 914 | assert_eq!(rows[0].get_id(), approved.get_id()); |
| 924 | 915 | ||
| 925 | // No state matches: an empty page, not an error. | 916 | // No state matches: an empty page, not an error. |
| 926 | - let (rows, total) = list_for_requester(&db, &veli, Some(AppointmentStatus::Rejected), None, None, None, | 917 | + let (rows, total) = list_for_requester( |
| 927 | - None, | 918 | + &db, |
| 928 | - 0, | 919 | + &veli, |
| 920 | + RequesterListParams { | ||
| 921 | + status: Some(AppointmentStatus::Rejected), | ||
| 922 | + ..Default::default() | ||
| 923 | + }, | ||
| 929 | ) | 924 | ) |
| 930 | .await | 925 | .await |
| 931 | .unwrap(); | 926 | .unwrap(); |
| 932 | assert_eq!((rows.len(), total), (0, 0)); | 927 | assert_eq!((rows.len(), total), (0, 0)); |
| 933 | 928 | ||
| 934 | // teacher joins through the slot: only the bookings on ali's slots. | 929 | // teacher joins through the slot: only the bookings on ali's slots. |
| 935 | - let (rows, total) = | 930 | + let (rows, total) = list_for_requester( |
| 936 | - list_for_requester(&db, &veli, None, None, None, Some(&ali), None, 0).await.unwrap(); | 931 | + &db, |
| 932 | + &veli, | ||
| 933 | + RequesterListParams { | ||
| 934 | + teacher: Some(&ali), | ||
| 935 | + ..Default::default() | ||
| 936 | + }, | ||
| 937 | + ) | ||
| 938 | + .await | ||
| 939 | + .unwrap(); | ||
| 937 | assert_eq!((rows.len(), total), (2, 2)); | 940 | assert_eq!((rows.len(), total), (2, 2)); |
| 938 | assert!(rows.iter().all(|row| row.get_id() != approved.get_id())); | 941 | assert!(rows.iter().all(|row| row.get_id() != approved.get_id())); |
| 939 | 942 | ||
| 940 | // starts_after judges the effective start: the pending meeting (at | 943 | // starts_after judges the effective start: the pending meeting (at |
| 941 | // now + 60_000) is out; the approved and cancelled ones stay. | 944 | // now + 60_000) is out; the approved and cancelled ones stay. |
| 942 | - let (rows, total) = list_for_requester(&db, &veli, None, Some(at(now + 150_000)), None, None, | 945 | + let (rows, total) = list_for_requester( |
| 943 | - None, | 946 | + &db, |
| 944 | - 0, | 947 | + &veli, |
| 948 | + RequesterListParams { | ||
| 949 | + starts_after: Some(at(now + 150_000)), | ||
| 950 | + ..Default::default() | ||
| 951 | + }, | ||
| 945 | ) | 952 | ) |
| 946 | .await | 953 | .await |
| 947 | .unwrap(); | 954 | .unwrap(); |
| @@ -949,11 +956,28 @@ mod tests { | |||
| 949 | assert!(rows.iter().all(|row| row.get_id() != pending.get_id())); | 956 | assert!(rows.iter().all(|row| row.get_id() != pending.get_id())); |
| 950 | 957 | ||
| 951 | // The window applies after the filter; total stays the filtered count. | 958 | // The window applies after the filter; total stays the filtered count. |
| 952 | - let (page1, total) = | 959 | + let (page1, total) = list_for_requester( |
| 953 | - list_for_requester(&db, &veli, None, None, None, None, Some(1), 0).await.unwrap(); | 960 | + &db, |
| 961 | + &veli, | ||
| 962 | + RequesterListParams { | ||
| 963 | + limit: Some(1), | ||
| 964 | + ..Default::default() | ||
| 965 | + }, | ||
| 966 | + ) | ||
| 967 | + .await | ||
| 968 | + .unwrap(); | ||
| 954 | assert_eq!((page1.len(), total), (1, 3)); | 969 | assert_eq!((page1.len(), total), (1, 3)); |
| 955 | - let (page2, total) = | 970 | + let (page2, total) = list_for_requester( |
| 956 | - list_for_requester(&db, &veli, None, None, None, None, Some(1), 1).await.unwrap(); | 971 | + &db, |
| 972 | + &veli, | ||
| 973 | + RequesterListParams { | ||
| 974 | + limit: Some(1), | ||
| 975 | + offset: 1, | ||
| 976 | + ..Default::default() | ||
| 977 | + }, | ||
| 978 | + ) | ||
| 979 | + .await | ||
| 980 | + .unwrap(); | ||
| 957 | assert_eq!((page2.len(), total), (1, 3)); | 981 | assert_eq!((page2.len(), total), (1, 3)); |
| 958 | assert_ne!(page1[0].get_id(), page2[0].get_id()); | 982 | assert_ne!(page1[0].get_id(), page2[0].get_id()); |
| 959 | 983 | ||
| @@ -963,9 +987,13 @@ mod tests { | |||
| 963 | propose(&db, pending.get_id(), at(now + 500_000), at(now + 560_000), &ali) | 987 | propose(&db, pending.get_id(), at(now + 500_000), at(now + 560_000), &ali) |
| 964 | .await | 988 | .await |
| 965 | .unwrap(); | 989 | .unwrap(); |
| 966 | - let (rows, total) = list_for_requester(&db, &veli, None, Some(at(now + 400_000)), None, None, | 990 | + let (rows, total) = list_for_requester( |
| 967 | - None, | 991 | + &db, |
| 968 | - 0, | 992 | + &veli, |
| 993 | + RequesterListParams { | ||
| 994 | + starts_after: Some(at(now + 400_000)), | ||
| 995 | + ..Default::default() | ||
| 996 | + }, | ||
| 969 | ) | 997 | ) |
| 970 | .await | 998 | .await |
| 971 | .unwrap(); | 999 | .unwrap(); |
| @@ -1005,18 +1033,30 @@ mod tests { | |||
| 1005 | .await | 1033 | .await |
| 1006 | .unwrap(); | 1034 | .unwrap(); |
| 1007 | let (rows, total) = | 1035 | let (rows, total) = |
| 1008 | - list_for_requester(&db, &veli, None, None, None, None, None, 0).await.unwrap(); | 1036 | + list_for_requester(&db, &veli, RequesterListParams::default()).await.unwrap(); |
| 1009 | assert_eq!((rows.len(), total), (3, 3), "the windowless row still lists"); | 1037 | assert_eq!((rows.len(), total), (3, 3), "the windowless row still lists"); |
| 1010 | - let (rows, total) = list_for_requester(&db, &veli, None, Some(at(now)), None, None, | 1038 | + let (rows, total) = list_for_requester( |
| 1011 | - None, | 1039 | + &db, |
| 1012 | - 0, | 1040 | + &veli, |
| 1041 | + RequesterListParams { | ||
| 1042 | + starts_after: Some(at(now)), | ||
| 1043 | + ..Default::default() | ||
| 1044 | + }, | ||
| 1013 | ) | 1045 | ) |
| 1014 | .await | 1046 | .await |
| 1015 | .unwrap(); | 1047 | .unwrap(); |
| 1016 | assert_eq!((rows.len(), total), (2, 2), "the windowless row has no start to match"); | 1048 | assert_eq!((rows.len(), total), (2, 2), "the windowless row has no start to match"); |
| 1017 | assert!(rows.iter().all(|row| row.get_id() != cancelled.get_id())); | 1049 | assert!(rows.iter().all(|row| row.get_id() != cancelled.get_id())); |
| 1018 | - let (rows, total) = | 1050 | + let (rows, total) = list_for_requester( |
| 1019 | - list_for_requester(&db, &veli, None, None, None, Some(&ali), None, 0).await.unwrap(); | 1051 | + &db, |
| 1052 | + &veli, | ||
| 1053 | + RequesterListParams { | ||
| 1054 | + teacher: Some(&ali), | ||
| 1055 | + ..Default::default() | ||
| 1056 | + }, | ||
| 1057 | + ) | ||
| 1058 | + .await | ||
| 1059 | + .unwrap(); | ||
| 1020 | assert_eq!((rows.len(), total), (1, 1), "a windowless row names no teacher"); | 1060 | assert_eq!((rows.len(), total), (1, 1), "a windowless row names no teacher"); |
| 1021 | assert_eq!(rows[0].get_id(), pending.get_id()); | 1061 | assert_eq!(rows[0].get_id(), pending.get_id()); |
| 1022 | } | 1062 | } |
| @@ -1051,19 +1091,17 @@ mod tests { | |||
| 1051 | 1091 | ||
| 1052 | // Absent: both — the set is unchanged without the bound. | 1092 | // Absent: both — the set is unchanged without the bound. |
| 1053 | let (rows, total) = | 1093 | let (rows, total) = |
| 1054 | - list_for_requester(&db, &veli, None, None, None, None, None, 0).await.unwrap(); | 1094 | + list_for_requester(&db, &veli, RequesterListParams::default()).await.unwrap(); |
| 1055 | assert_eq!((rows.len(), total), (2, 2)); | 1095 | assert_eq!((rows.len(), total), (2, 2)); |
| 1056 | 1096 | ||
| 1057 | // before: only the early booking survives. | 1097 | // before: only the early booking survives. |
| 1058 | let (rows, total) = list_for_requester( | 1098 | let (rows, total) = list_for_requester( |
| 1059 | &db, | 1099 | &db, |
| 1060 | &veli, | 1100 | &veli, |
| 1061 | - None, | 1101 | + RequesterListParams { |
| 1062 | - None, | 1102 | + starts_before: Some(at(now + 150_000)), |
| 1063 | - Some(at(now + 150_000)), | 1103 | + ..Default::default() |
| 1064 | - None, | 1104 | + }, |
| 1065 | - None, | ||
| 1066 | - 0, | ||
| 1067 | ) | 1105 | ) |
| 1068 | .await | 1106 | .await |
| 1069 | .unwrap(); | 1107 | .unwrap(); |
| @@ -1076,12 +1114,10 @@ mod tests { | |||
| 1076 | let (rows, total) = list_for_requester( | 1114 | let (rows, total) = list_for_requester( |
| 1077 | &db, | 1115 | &db, |
| 1078 | &veli, | 1116 | &veli, |
| 1079 | - None, | 1117 | + RequesterListParams { |
| 1080 | - None, | 1118 | + starts_before: Some(at(now + 200_000)), |
| 1081 | - Some(at(now + 200_000)), | 1119 | + ..Default::default() |
| 1082 | - None, | 1120 | + }, |
| 1083 | - None, | ||
| 1084 | - 0, | ||
| 1085 | ) | 1121 | ) |
| 1086 | .await | 1122 | .await |
| 1087 | .unwrap(); | 1123 | .unwrap(); |
| @@ -1100,12 +1136,11 @@ mod tests { | |||
| 1100 | let (rows, total) = list_for_requester( | 1136 | let (rows, total) = list_for_requester( |
| 1101 | &db, | 1137 | &db, |
| 1102 | &veli, | 1138 | &veli, |
| 1103 | - None, | 1139 | + RequesterListParams { |
| 1104 | - Some(at(now + 150_000)), | 1140 | + starts_after: Some(at(now + 150_000)), |
| 1105 | - Some(at(now + 260_000)), | 1141 | + starts_before: Some(at(now + 260_000)), |
| 1106 | - None, | 1142 | + ..Default::default() |
| 1107 | - None, | 1143 | + }, |
| 1108 | - 0, | ||
| 1109 | ) | 1144 | ) |
| 1110 | .await | 1145 | .await |
| 1111 | .unwrap(); | 1146 | .unwrap(); |
src/service/exam.rsmodified+39 -41
| @@ -24,6 +24,8 @@ use crate::domain::user::{User, UserId}; | |||
| 24 | use crate::error::{AppError, ValidationError}; | 24 | use crate::error::{AppError, ValidationError}; |
| 25 | use crate::service::exam_attempt::require_open; | 25 | use crate::service::exam_attempt::require_open; |
| 26 | 26 | ||
| 27 | +pub use crate::db::exam::WindowedListParams; | ||
| 28 | + | ||
| 27 | /// Publish (or draft) an exam on one class×course instance, inside one term. | 29 | /// Publish (or draft) an exam on one class×course instance, inside one term. |
| 28 | /// | 30 | /// |
| 29 | /// Two refusals stand in front of the write. The instance's catalog course | 31 | /// Two refusals stand in front of the write. The instance's catalog course |
| @@ -122,25 +124,9 @@ pub async fn list_windowed( | |||
| 122 | db: &Database, | 124 | db: &Database, |
| 123 | visible: Option<&[ClassCourseId]>, | 125 | visible: Option<&[ClassCourseId]>, |
| 124 | managed: Option<&[ClassCourseId]>, | 126 | managed: Option<&[ClassCourseId]>, |
| 125 | - starts_after: Option<i64>, | 127 | + params: WindowedListParams, |
| 126 | - ends_after: Option<i64>, | ||
| 127 | - starts_before: Option<i64>, | ||
| 128 | - ends_before: Option<i64>, | ||
| 129 | - limit: Option<i64>, | ||
| 130 | - offset: i64, | ||
| 131 | ) -> Result<(Vec<Exam>, i64), AppError> { | 128 | ) -> Result<(Vec<Exam>, i64), AppError> { |
| 132 | - exam::list_windowed( | 129 | + exam::list_windowed(db, visible, managed, params).await |
| 133 | - db, | ||
| 134 | - visible, | ||
| 135 | - managed, | ||
| 136 | - starts_after, | ||
| 137 | - ends_after, | ||
| 138 | - starts_before, | ||
| 139 | - ends_before, | ||
| 140 | - limit, | ||
| 141 | - offset, | ||
| 142 | - ) | ||
| 143 | - .await | ||
| 144 | } | 130 | } |
| 145 | 131 | ||
| 146 | /// One instance's exams, newest first — the read behind | 132 | /// One instance's exams, newest first — the read behind |
| @@ -903,7 +889,7 @@ mod tests { | |||
| 903 | let other_draft = exam_shaped(&db, &b, sync(now + 3_600_000), true, "other-draft").await; | 889 | let other_draft = exam_shaped(&db, &b, sync(now + 3_600_000), true, "other-draft").await; |
| 904 | 890 | ||
| 905 | // Manager+: the whole table, newest first. | 891 | // Manager+: the whole table, newest first. |
| 906 | - let (rows, total) = list_windowed(&db, None, None, None, None, None, None, None, 0) | 892 | + let (rows, total) = list_windowed(&db, None, None, WindowedListParams::default()) |
| 907 | .await | 893 | .await |
| 908 | .unwrap(); | 894 | .unwrap(); |
| 909 | assert_eq!(total, 5); | 895 | assert_eq!(total, 5); |
| @@ -920,9 +906,17 @@ mod tests { | |||
| 920 | // unaliased `exam` (the audience join only exists on the other | 906 | // unaliased `exam` (the audience join only exists on the other |
| 921 | // branch). This exact call 500'd with "missing FROM-clause entry for | 907 | // branch). This exact call 500'd with "missing FROM-clause entry for |
| 922 | // table e" while the predicates were `e.`-qualified. | 908 | // table e" while the predicates were `e.`-qualified. |
| 923 | - let (rows, total) = list_windowed(&db, None, None, None, Some(now), None, None, None, 0) | 909 | + let (rows, total) = list_windowed( |
| 924 | - .await | 910 | + &db, |
| 925 | - .unwrap(); | 911 | + None, |
| 912 | + None, | ||
| 913 | + WindowedListParams { | ||
| 914 | + ends_after: Some(now), | ||
| 915 | + ..Default::default() | ||
| 916 | + }, | ||
| 917 | + ) | ||
| 918 | + .await | ||
| 919 | + .unwrap(); | ||
| 926 | assert_eq!(total, 3); | 920 | assert_eq!(total, 3); |
| 927 | let listed: Vec<_> = rows.iter().map(|exam| exam.get_id()).cloned().collect(); | 921 | let listed: Vec<_> = rows.iter().map(|exam| exam.get_id()).cloned().collect(); |
| 928 | assert_eq!( | 922 | assert_eq!( |
| @@ -932,10 +926,17 @@ mod tests { | |||
| 932 | .map(|exam| exam.get_id().clone()) | 926 | .map(|exam| exam.get_id().clone()) |
| 933 | .collect::<Vec<_>>() | 927 | .collect::<Vec<_>>() |
| 934 | ); | 928 | ); |
| 935 | - let (_, total) = | 929 | + let (_, total) = list_windowed( |
| 936 | - list_windowed(&db, None, None, None, None, Some(now + 45 * 60_000), None, None, 0) | 930 | + &db, |
| 937 | - .await | 931 | + None, |
| 938 | - .unwrap(); | 932 | + None, |
| 933 | + WindowedListParams { | ||
| 934 | + starts_before: Some(now + 45 * 60_000), | ||
| 935 | + ..Default::default() | ||
| 936 | + }, | ||
| 937 | + ) | ||
| 938 | + .await | ||
| 939 | + .unwrap(); | ||
| 939 | assert_eq!(total, 1, "starts_before keeps only the already-run exam"); | 940 | assert_eq!(total, 1, "starts_before keeps only the already-run exam"); |
| 940 | 941 | ||
| 941 | // A viewer of A who manages nothing: A's published exams only — both | 942 | // A viewer of A who manages nothing: A's published exams only — both |
| @@ -943,7 +944,7 @@ mod tests { | |||
| 943 | let visible = vec![a.get_id().clone()]; | 944 | let visible = vec![a.get_id().clone()]; |
| 944 | let nothing: Vec<ClassCourseId> = Vec::new(); | 945 | let nothing: Vec<ClassCourseId> = Vec::new(); |
| 945 | let (rows, total) = | 946 | let (rows, total) = |
| 946 | - list_windowed(&db, Some(&visible), Some(¬hing), None, None, None, None, None, 0) | 947 | + list_windowed(&db, Some(&visible), Some(¬hing), WindowedListParams::default()) |
| 947 | .await | 948 | .await |
| 948 | .unwrap(); | 949 | .unwrap(); |
| 949 | assert_eq!(total, 3); | 950 | assert_eq!(total, 3); |
| @@ -952,7 +953,7 @@ mod tests { | |||
| 952 | // A's teacher manages A: sees A's draft — and still not B's. | 953 | // A's teacher manages A: sees A's draft — and still not B's. |
| 953 | let manages_a = vec![a.get_id().clone()]; | 954 | let manages_a = vec![a.get_id().clone()]; |
| 954 | let (rows, total) = | 955 | let (rows, total) = |
| 955 | - list_windowed(&db, Some(&visible), Some(&manages_a), None, None, None, None, None, 0) | 956 | + list_windowed(&db, Some(&visible), Some(&manages_a), WindowedListParams::default()) |
| 956 | .await | 957 | .await |
| 957 | .unwrap(); | 958 | .unwrap(); |
| 958 | assert_eq!(total, 4); | 959 | assert_eq!(total, 4); |
| @@ -967,12 +968,10 @@ mod tests { | |||
| 967 | &db, | 968 | &db, |
| 968 | Some(&visible), | 969 | Some(&visible), |
| 969 | Some(&manages_a), | 970 | Some(&manages_a), |
| 970 | - None, | 971 | + WindowedListParams { |
| 971 | - Some(now), | 972 | + ends_after: Some(now), |
| 972 | - None, | 973 | + ..Default::default() |
| 973 | - None, | 974 | + }, |
| 974 | - None, | ||
| 975 | - 0, | ||
| 976 | ) | 975 | ) |
| 977 | .await | 976 | .await |
| 978 | .unwrap(); | 977 | .unwrap(); |
| @@ -985,12 +984,11 @@ mod tests { | |||
| 985 | &db, | 984 | &db, |
| 986 | Some(&visible), | 985 | Some(&visible), |
| 987 | Some(&manages_a), | 986 | Some(&manages_a), |
| 988 | - None, | 987 | + WindowedListParams { |
| 989 | - Some(now), | 988 | + ends_after: Some(now), |
| 990 | - None, | 989 | + limit: Some(1), |
| 991 | - None, | 990 | + ..Default::default() |
| 992 | - Some(1), | 991 | + }, |
| 993 | - 0, | ||
| 994 | ) | 992 | ) |
| 995 | .await | 993 | .await |
| 996 | .unwrap(); | 994 | .unwrap(); |
| @@ -1000,7 +998,7 @@ mod tests { | |||
| 1000 | // draft wall leaves them an empty page, not an error. | 998 | // draft wall leaves them an empty page, not an error. |
| 1001 | let sees_b = vec![b.get_id().clone()]; | 999 | let sees_b = vec![b.get_id().clone()]; |
| 1002 | let (rows, total) = | 1000 | let (rows, total) = |
| 1003 | - list_windowed(&db, Some(&sees_b), Some(¬hing), None, None, None, None, None, 0) | 1001 | + list_windowed(&db, Some(&sees_b), Some(¬hing), WindowedListParams::default()) |
| 1004 | .await | 1002 | .await |
| 1005 | .unwrap(); | 1003 | .unwrap(); |
| 1006 | assert!(rows.is_empty() && total == 0); | 1004 | assert!(rows.is_empty() && total == 0); |
src/web/appointments.rsmodified+8 -6
| @@ -819,12 +819,14 @@ async fn list_appointments( | |||
| 819 | service::appointment::list_for_requester( | 819 | service::appointment::list_for_requester( |
| 820 | &st.db, | 820 | &st.db, |
| 821 | user.get_id(), | 821 | user.get_id(), |
| 822 | - status, | 822 | + service::appointment::RequesterListParams { |
| 823 | - starts_after, | 823 | + status, |
| 824 | - starts_before, | 824 | + starts_after, |
| 825 | - narrow_to.as_ref(), | 825 | + starts_before, |
| 826 | - limit, | 826 | + teacher: narrow_to.as_ref(), |
| 827 | - offset, | 827 | + limit, |
| 828 | + offset, | ||
| 829 | + }, | ||
| 828 | ) | 830 | ) |
| 829 | .await? | 831 | .await? |
| 830 | }; | 832 | }; |
src/web/courses.rsmodified+23 -8
| @@ -438,18 +438,31 @@ struct CourseListFilter { | |||
| 438 | taught: Option<bool>, | 438 | taught: Option<bool>, |
| 439 | } | 439 | } |
| 440 | 440 | ||
| 441 | +/// The parsed [`CourseListFilter`] — one named field per query parameter, | ||
| 442 | +/// ready for [`visible_courses_filtered`]. | ||
| 443 | +#[derive(Debug)] | ||
| 444 | +struct ResolvedCourseFilter { | ||
| 445 | + kind: Option<CourseKind>, | ||
| 446 | + q: Option<String>, | ||
| 447 | + taught: Option<bool>, | ||
| 448 | +} | ||
| 449 | + | ||
| 441 | impl CourseListFilter { | 450 | impl CourseListFilter { |
| 442 | /// `kind` parses through the write-path validator, so an unknown or empty | 451 | /// `kind` parses through the write-path validator, so an unknown or empty |
| 443 | /// spelling is the same 400 a create/PATCH gets; `taught` needs no parse — | 452 | /// spelling is the same 400 a create/PATCH gets; `taught` needs no parse — |
| 444 | /// the query extractor itself 400s a `?taught=` that is not `true`/`false`, | 453 | /// the query extractor itself 400s a `?taught=` that is not `true`/`false`, |
| 445 | /// naming the field; `q` passes through raw — each arm trims, folds and | 454 | /// naming the field; `q` passes through raw — each arm trims, folds and |
| 446 | /// blanks it identically. | 455 | /// blanks it identically. |
| 447 | - fn resolve(self) -> Result<(Option<CourseKind>, Option<String>, Option<bool>), AppError> { | 456 | + fn resolve(self) -> Result<ResolvedCourseFilter, AppError> { |
| 448 | let kind = match self.kind { | 457 | let kind = match self.kind { |
| 449 | Some(kind) => Some(CourseKind::try_new(&kind)?), | 458 | Some(kind) => Some(CourseKind::try_new(&kind)?), |
| 450 | None => None, | 459 | None => None, |
| 451 | }; | 460 | }; |
| 452 | - Ok((kind, self.q, self.taught)) | 461 | + Ok(ResolvedCourseFilter { |
| 462 | + kind, | ||
| 463 | + q: self.q, | ||
| 464 | + taught: self.taught, | ||
| 465 | + }) | ||
| 453 | } | 466 | } |
| 454 | } | 467 | } |
| 455 | 468 | ||
| @@ -483,11 +496,13 @@ async fn list_courses( | |||
| 483 | Query(page): Query<PageParams>, | 496 | Query(page): Query<PageParams>, |
| 484 | ) -> Result<Json<Page<CourseResponse>>, AppError> { | 497 | ) -> Result<Json<Page<CourseResponse>>, AppError> { |
| 485 | let (limit, offset) = page.resolve()?; | 498 | let (limit, offset) = page.resolve()?; |
| 486 | - let (kind, q, taught) = filter.resolve()?; | 499 | + let filters = filter.resolve()?; |
| 487 | // The filters ride inside the visible set — SQL for a manager, the | 500 | // The filters ride inside the visible set — SQL for a manager, the |
| 488 | // union's tail in Rust below one — so total is the filtered length | 501 | // union's tail in Rust below one — so total is the filtered length |
| 489 | // before the window is cut. | 502 | // before the window is cut. |
| 490 | - let courses = visible_courses_filtered(&user, &st.db, kind, q.as_deref(), taught).await?; | 503 | + let courses = |
| 504 | + visible_courses_filtered(&user, &st.db, filters.kind, filters.q.as_deref(), filters.taught) | ||
| 505 | + .await?; | ||
| 491 | let total = courses.len() as i64; | 506 | let total = courses.len() as i64; |
| 492 | // Paged in the web layer: the visible set is a Rust union of two lists. | 507 | // Paged in the web layer: the visible set is a Rust union of two lists. |
| 493 | let window = paginate(&courses, limit, offset); | 508 | let window = paginate(&courses, limit, offset); |
| @@ -1009,16 +1024,16 @@ mod tests { | |||
| 1009 | "{kind:?}: {err:?}" | 1024 | "{kind:?}: {err:?}" |
| 1010 | ); | 1025 | ); |
| 1011 | } | 1026 | } |
| 1012 | - let (kind, q, taught) = CourseListFilter { | 1027 | + let resolved = CourseListFilter { |
| 1013 | kind: Some("club".to_string()), | 1028 | kind: Some("club".to_string()), |
| 1014 | q: Some(" ".to_string()), | 1029 | q: Some(" ".to_string()), |
| 1015 | taught: None, | 1030 | taught: None, |
| 1016 | } | 1031 | } |
| 1017 | .resolve() | 1032 | .resolve() |
| 1018 | .unwrap(); | 1033 | .unwrap(); |
| 1019 | - assert_eq!(kind.as_ref().map(CourseKind::as_str), Some("club")); | 1034 | + assert_eq!(resolved.kind.as_ref().map(CourseKind::as_str), Some("club")); |
| 1020 | - assert_eq!(q.as_deref(), Some(" ")); | 1035 | + assert_eq!(resolved.q.as_deref(), Some(" ")); |
| 1021 | - assert_eq!(taught, None); | 1036 | + assert_eq!(resolved.taught, None); |
| 1022 | } | 1037 | } |
| 1023 | 1038 | ||
| 1024 | /// Manager arm: kind and q narrow the catalog, total is the filtered | 1039 | /// Manager arm: kind and q narrow the catalog, total is the filtered |
src/web/exams/mod.rsmodified+10 -24
| @@ -255,22 +255,19 @@ async fn list_exams( | |||
| 255 | ) -> Result<Json<Page<ExamResponse>>, AppError> { | 255 | ) -> Result<Json<Page<ExamResponse>>, AppError> { |
| 256 | let (limit, offset) = page.resolve()?; | 256 | let (limit, offset) = page.resolve()?; |
| 257 | window.validate()?; | 257 | window.validate()?; |
| 258 | + let params = service::exam::WindowedListParams { | ||
| 259 | + starts_after: window.starts_after, | ||
| 260 | + ends_after: window.ends_after, | ||
| 261 | + starts_before: window.starts_before, | ||
| 262 | + ends_before: window.ends_before, | ||
| 263 | + limit, | ||
| 264 | + offset, | ||
| 265 | + }; | ||
| 258 | // Visibility and the draft rule ride the same WHERE as the page and the | 266 | // Visibility and the draft rule ride the same WHERE as the page and the |
| 259 | // count: a manager+ reads the whole table, everyone else the exams of | 267 | // count: a manager+ reads the whole table, everyone else the exams of |
| 260 | // their visible instances — a draft only where they manage the instance. | 268 | // their visible instances — a draft only where they manage the instance. |
| 261 | let (exams, total) = if user.get_role().at_least(Role::Manager) { | 269 | let (exams, total) = if user.get_role().at_least(Role::Manager) { |
| 262 | - service::exam::list_windowed( | 270 | + service::exam::list_windowed(&st.db, None, None, params).await? |
| 263 | - &st.db, | ||
| 264 | - None, | ||
| 265 | - None, | ||
| 266 | - window.starts_after, | ||
| 267 | - window.ends_after, | ||
| 268 | - window.starts_before, | ||
| 269 | - window.ends_before, | ||
| 270 | - limit, | ||
| 271 | - offset, | ||
| 272 | - ) | ||
| 273 | - .await? | ||
| 274 | } else { | 271 | } else { |
| 275 | let instances = visible_instances(&user, &st.db).await?; | 272 | let instances = visible_instances(&user, &st.db).await?; |
| 276 | let visible: Vec<_> = instances.iter().map(|(i, _)| i.get_id().clone()).collect(); | 273 | let visible: Vec<_> = instances.iter().map(|(i, _)| i.get_id().clone()).collect(); |
| @@ -281,18 +278,7 @@ async fn list_exams( | |||
| 281 | .filter(|(_, manages)| *manages) | 278 | .filter(|(_, manages)| *manages) |
| 282 | .map(|(instance, _)| instance.get_id().clone()) | 279 | .map(|(instance, _)| instance.get_id().clone()) |
| 283 | .collect(); | 280 | .collect(); |
| 284 | - service::exam::list_windowed( | 281 | + service::exam::list_windowed(&st.db, Some(&visible), Some(&managed), params).await? |
| 285 | - &st.db, | ||
| 286 | - Some(&visible), | ||
| 287 | - Some(&managed), | ||
| 288 | - window.starts_after, | ||
| 289 | - window.ends_after, | ||
| 290 | - window.starts_before, | ||
| 291 | - window.ends_before, | ||
| 292 | - limit, | ||
| 293 | - offset, | ||
| 294 | - ) | ||
| 295 | - .await? | ||
| 296 | }; | 282 | }; |
| 297 | let items = exams.iter().map(ExamResponse::new).collect(); | 283 | let items = exams.iter().map(ExamResponse::new).collect(); |
| 298 | Ok(Json(Page::new(items, total, limit, offset))) | 284 | Ok(Json(Page::new(items, total, limit, offset))) |