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