| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | use axum::Json; |
| 23 | use axum::extract::Path; |
| 24 | use axum::http::StatusCode; |
| 25 | use serde::{Deserialize, Serialize}; |
| 26 | use utoipa::ToSchema; |
| 27 | use utoipa_axum::router::OpenApiRouter; |
| 28 | use utoipa_axum::routes; |
| 29 | |
| 30 | use crate::database::Database; |
| 31 | use crate::domain::class_course::{ClassCourse, ClassCourseId}; |
| 32 | use crate::domain::course_offering::{CourseOffering, CourseOfferingId}; |
| 33 | use crate::domain::user::User; |
| 34 | use crate::error::{AppError, ErrorResponse}; |
| 35 | use crate::service; |
| 36 | use crate::state::AppState; |
| 37 | use crate::web::dto::SubjectResponse; |
| 38 | use crate::web::tenant_state::State; |
| 39 | |
| 40 | use super::{CurrentUser, RequireManager, RequireTeacher}; |
| 41 | |
| 42 | |
| 43 | pub fn offering_routes() -> OpenApiRouter<AppState> { |
| 44 | OpenApiRouter::new() |
| 45 | .routes(routes!(list_offering_subjects, add_offering_subject)) |
| 46 | .routes(routes!(remove_offering_subject)) |
| 47 | } |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | pub fn instance_routes() -> OpenApiRouter<AppState> { |
| 53 | OpenApiRouter::new() |
| 54 | .routes(routes!(list_instance_subjects, add_instance_subject)) |
| 55 | .routes(routes!(remove_instance_subject)) |
| 56 | .routes(routes!(reset_instance_subjects)) |
| 57 | } |
| 58 | |
| 59 | #[derive(Deserialize, ToSchema)] |
| 60 | struct AddSubject { |
| 61 | |
| 62 | |
| 63 | #[schema(example = "019732e3-7b00-7000-8000-00000000dead")] |
| 64 | subject: String, |
| 65 | } |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | #[derive(Serialize, ToSchema)] |
| 71 | struct ResolvedSubjects { |
| 72 | |
| 73 | subjects: Vec<SubjectResponse>, |
| 74 | |
| 75 | |
| 76 | subjects_inherited: bool, |
| 77 | } |
| 78 | |
| 79 | async fn offering_or_404(key: &str, db: &Database) -> Result<CourseOffering, AppError> { |
| 80 | service::course_offering::read(db, &CourseOfferingId::from_key(key)) |
| 81 | .await? |
| 82 | .ok_or(AppError::NotFound) |
| 83 | } |
| 84 | |
| 85 | async fn instance_or_404(key: &str, db: &Database) -> Result<ClassCourse, AppError> { |
| 86 | service::class_course::read(db, &ClassCourseId::from_key(key)) |
| 87 | .await? |
| 88 | .ok_or(AppError::NotFound) |
| 89 | } |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | async fn gated_instance(key: &str, user: &User, db: &Database) -> Result<ClassCourse, AppError> { |
| 95 | let instance = instance_or_404(key, db).await?; |
| 96 | service::class_course::ensure_instance_teacher(db, user, instance.get_id()).await?; |
| 97 | service::class_course::require_open(db, instance.get_id()).await?; |
| 98 | Ok(instance) |
| 99 | } |
| 100 | |
| 101 | |
| 102 | |
| 103 | #[utoipa::path( |
| 104 | get, |
| 105 | path = "/{id}/subjects", |
| 106 | tag = "offerings", |
| 107 | security(("session_cookie" = [])), |
| 108 | params(("id" = String, Path, description = "Offering id")), |
| 109 | responses( |
| 110 | (status = 200, description = "The offering's selected subjects", body = [SubjectResponse]), |
| 111 | (status = 401, description = "Not authenticated", body = ErrorResponse), |
| 112 | (status = 404, description = "Offering not found", body = ErrorResponse), |
| 113 | ), |
| 114 | )] |
| 115 | async fn list_offering_subjects( |
| 116 | State(st): State<AppState>, |
| 117 | CurrentUser(_user): CurrentUser, |
| 118 | Path(id): Path<String>, |
| 119 | ) -> Result<Json<Vec<SubjectResponse>>, AppError> { |
| 120 | let offering = offering_or_404(&id, &st.db).await?; |
| 121 | let subjects = service::offering_subject::resolved_for_offering(&st.db, offering.get_id()) |
| 122 | .await?; |
| 123 | Ok(Json(subjects.iter().map(SubjectResponse::new).collect())) |
| 124 | } |
| 125 | |
| 126 | |
| 127 | |
| 128 | #[utoipa::path( |
| 129 | post, |
| 130 | path = "/{id}/subjects", |
| 131 | tag = "offerings", |
| 132 | security(("session_cookie" = [])), |
| 133 | params(("id" = String, Path, description = "Offering id")), |
| 134 | request_body = AddSubject, |
| 135 | responses( |
| 136 | (status = 201, description = "Subject selected", body = SubjectResponse), |
| 137 | (status = 200, description = "Already selected — no-op success", body = SubjectResponse), |
| 138 | (status = 400, description = "Unknown subject, or one from another course", body = ErrorResponse), |
| 139 | (status = 401, description = "Not authenticated", body = ErrorResponse), |
| 140 | (status = 403, description = "Requires manager role or higher", body = ErrorResponse), |
| 141 | (status = 404, description = "Offering not found", body = ErrorResponse), |
| 142 | (status = 422, description = "The body does not fit this request: `subject` is missing or malformed"), |
| 143 | ), |
| 144 | )] |
| 145 | async fn add_offering_subject( |
| 146 | State(st): State<AppState>, |
| 147 | RequireManager(_user): RequireManager, |
| 148 | Path(id): Path<String>, |
| 149 | Json(req): Json<AddSubject>, |
| 150 | ) -> Result<(StatusCode, Json<SubjectResponse>), AppError> { |
| 151 | let offering = offering_or_404(&id, &st.db).await?; |
| 152 | let (subject, added) = |
| 153 | service::offering_subject::add_to_offering(&st.db, offering.get_id(), &req.subject) |
| 154 | .await?; |
| 155 | let code = if added { |
| 156 | StatusCode::CREATED |
| 157 | } else { |
| 158 | StatusCode::OK |
| 159 | }; |
| 160 | Ok((code, Json(SubjectResponse::new(&subject)))) |
| 161 | } |
| 162 | |
| 163 | |
| 164 | |
| 165 | #[utoipa::path( |
| 166 | delete, |
| 167 | path = "/{id}/subjects/{subject}", |
| 168 | tag = "offerings", |
| 169 | security(("session_cookie" = [])), |
| 170 | params( |
| 171 | ("id" = String, Path, description = "Offering id"), |
| 172 | ("subject" = String, Path, description = "Subject id"), |
| 173 | ), |
| 174 | responses( |
| 175 | (status = 204, description = "Dropped"), |
| 176 | (status = 401, description = "Not authenticated", body = ErrorResponse), |
| 177 | (status = 403, description = "Requires manager role or higher", body = ErrorResponse), |
| 178 | (status = 404, description = "Offering not found, or subject not selected", body = ErrorResponse), |
| 179 | ), |
| 180 | )] |
| 181 | async fn remove_offering_subject( |
| 182 | State(st): State<AppState>, |
| 183 | RequireManager(_user): RequireManager, |
| 184 | Path((id, subject)): Path<(String, String)>, |
| 185 | ) -> Result<StatusCode, AppError> { |
| 186 | let offering = offering_or_404(&id, &st.db).await?; |
| 187 | service::offering_subject::remove_from_offering(&st.db, offering.get_id(), &subject).await?; |
| 188 | Ok(StatusCode::NO_CONTENT) |
| 189 | } |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | #[utoipa::path( |
| 196 | get, |
| 197 | path = "/{id}/subjects", |
| 198 | tag = "instances", |
| 199 | security(("session_cookie" = [])), |
| 200 | params(("id" = String, Path, description = "Instance (class×course) id")), |
| 201 | responses( |
| 202 | (status = 200, description = "The resolved subject set and whether it is inherited", body = ResolvedSubjects), |
| 203 | (status = 401, description = "Not authenticated", body = ErrorResponse), |
| 204 | (status = 404, description = "Instance not found", body = ErrorResponse), |
| 205 | ), |
| 206 | )] |
| 207 | async fn list_instance_subjects( |
| 208 | State(st): State<AppState>, |
| 209 | CurrentUser(_user): CurrentUser, |
| 210 | Path(id): Path<String>, |
| 211 | ) -> Result<Json<ResolvedSubjects>, AppError> { |
| 212 | let instance = instance_or_404(&id, &st.db).await?; |
| 213 | let subjects = service::offering_subject::resolved_for_instance(&st.db, &instance).await?; |
| 214 | Ok(Json(ResolvedSubjects { |
| 215 | subjects: subjects.iter().map(SubjectResponse::new).collect(), |
| 216 | subjects_inherited: instance.subjects_inherited(), |
| 217 | })) |
| 218 | } |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | #[utoipa::path( |
| 226 | post, |
| 227 | path = "/{id}/subjects", |
| 228 | tag = "instances", |
| 229 | security(("session_cookie" = [])), |
| 230 | params(("id" = String, Path, description = "Instance (class×course) id")), |
| 231 | request_body = AddSubject, |
| 232 | responses( |
| 233 | (status = 201, description = "Subject selected — the section now owns its set", body = SubjectResponse), |
| 234 | (status = 200, description = "Already selected — no-op success", body = SubjectResponse), |
| 235 | (status = 400, description = "Unknown subject, or one from another course", body = ErrorResponse), |
| 236 | (status = 401, description = "Not authenticated", body = ErrorResponse), |
| 237 | (status = 403, description = "Not this instance's teacher, its şube's homeroom teacher, or a manager/admin", body = ErrorResponse), |
| 238 | (status = 404, description = "Instance not found", body = ErrorResponse), |
| 239 | (status = 409, description = "This instance's academic year is archived — past years are read-only", body = ErrorResponse), |
| 240 | (status = 422, description = "The body does not fit this request: `subject` is missing or malformed"), |
| 241 | ), |
| 242 | )] |
| 243 | async fn add_instance_subject( |
| 244 | State(st): State<AppState>, |
| 245 | RequireTeacher(user): RequireTeacher, |
| 246 | Path(id): Path<String>, |
| 247 | Json(req): Json<AddSubject>, |
| 248 | ) -> Result<(StatusCode, Json<SubjectResponse>), AppError> { |
| 249 | let instance = gated_instance(&id, &user, &st.db).await?; |
| 250 | let (subject, added) = |
| 251 | service::offering_subject::add_to_instance(&st.db, &instance, &req.subject).await?; |
| 252 | let code = if added { |
| 253 | StatusCode::CREATED |
| 254 | } else { |
| 255 | StatusCode::OK |
| 256 | }; |
| 257 | Ok((code, Json(SubjectResponse::new(&subject)))) |
| 258 | } |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | #[utoipa::path( |
| 265 | delete, |
| 266 | path = "/{id}/subjects/{subject}", |
| 267 | tag = "instances", |
| 268 | security(("session_cookie" = [])), |
| 269 | params( |
| 270 | ("id" = String, Path, description = "Instance (class×course) id"), |
| 271 | ("subject" = String, Path, description = "Subject id"), |
| 272 | ), |
| 273 | responses( |
| 274 | (status = 204, description = "Dropped — the section keeps its own set"), |
| 275 | (status = 401, description = "Not authenticated", body = ErrorResponse), |
| 276 | (status = 403, description = "Not this instance's teacher, its şube's homeroom teacher, or a manager/admin", body = ErrorResponse), |
| 277 | (status = 404, description = "Instance not found, or subject not in the section's own set", body = ErrorResponse), |
| 278 | (status = 409, description = "This instance's academic year is archived — past years are read-only", body = ErrorResponse), |
| 279 | ), |
| 280 | )] |
| 281 | async fn remove_instance_subject( |
| 282 | State(st): State<AppState>, |
| 283 | RequireTeacher(user): RequireTeacher, |
| 284 | Path((id, subject)): Path<(String, String)>, |
| 285 | ) -> Result<StatusCode, AppError> { |
| 286 | let instance = gated_instance(&id, &user, &st.db).await?; |
| 287 | service::offering_subject::remove_from_instance(&st.db, &instance, &subject).await?; |
| 288 | Ok(StatusCode::NO_CONTENT) |
| 289 | } |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | #[utoipa::path( |
| 296 | delete, |
| 297 | path = "/{id}/subjects", |
| 298 | tag = "instances", |
| 299 | security(("session_cookie" = [])), |
| 300 | params(("id" = String, Path, description = "Instance (class×course) id")), |
| 301 | responses( |
| 302 | (status = 204, description = "Own set swept — the offering's set applies again"), |
| 303 | (status = 401, description = "Not authenticated", body = ErrorResponse), |
| 304 | (status = 403, description = "Not this instance's teacher, its şube's homeroom teacher, or a manager/admin", body = ErrorResponse), |
| 305 | (status = 404, description = "Instance not found", body = ErrorResponse), |
| 306 | (status = 409, description = "This instance's academic year is archived — past years are read-only", body = ErrorResponse), |
| 307 | ), |
| 308 | )] |
| 309 | async fn reset_instance_subjects( |
| 310 | State(st): State<AppState>, |
| 311 | RequireTeacher(user): RequireTeacher, |
| 312 | Path(id): Path<String>, |
| 313 | ) -> Result<StatusCode, AppError> { |
| 314 | let instance = gated_instance(&id, &user, &st.db).await?; |
| 315 | service::offering_subject::reset_instance(&st.db, &instance).await?; |
| 316 | Ok(StatusCode::NO_CONTENT) |
| 317 | } |