| 1 | use crate::web::tenant_state::State; |
| 2 | use axum::Json; |
| 3 | use axum::extract::Path; |
| 4 | use axum::http::StatusCode; |
| 5 | use serde::Deserialize; |
| 6 | use utoipa::ToSchema; |
| 7 | use utoipa_axum::router::OpenApiRouter; |
| 8 | use utoipa_axum::routes; |
| 9 | |
| 10 | use crate::database::Database; |
| 11 | use crate::domain::course::Course; |
| 12 | use crate::domain::subject::{Subject, SubjectDescription, SubjectId, SubjectName}; |
| 13 | use crate::error::{AppError, ErrorResponse}; |
| 14 | use crate::service::course::{can_manage_course, can_view_course}; |
| 15 | use crate::service::subject; |
| 16 | use crate::state::AppState; |
| 17 | |
| 18 | use super::{CurrentUser, RequireTeacher, SubjectResponse}; |
| 19 | |
| 20 | pub fn routes() -> OpenApiRouter<AppState> { |
| 21 | OpenApiRouter::new().routes(routes!(get_subject, update_subject, delete_subject)) |
| 22 | } |
| 23 | |
| 24 | #[derive(Deserialize, ToSchema)] |
| 25 | struct UpdateSubject { |
| 26 | #[schema(max_length = 200)] |
| 27 | name: Option<String>, |
| 28 | #[schema(max_length = 2000)] |
| 29 | description: Option<String>, |
| 30 | } |
| 31 | |
| 32 | |
| 33 | |
| 34 | async fn subject_with_course(id: &str, db: &Database) -> Result<(Subject, Course), AppError> { |
| 35 | let subject = subject::read(db, &SubjectId::from_key(id)) |
| 36 | .await? |
| 37 | .ok_or(AppError::NotFound)?; |
| 38 | let course = crate::service::course::read(db, subject.get_course()) |
| 39 | .await? |
| 40 | .ok_or(AppError::NotFound)?; |
| 41 | Ok((subject, course)) |
| 42 | } |
| 43 | |
| 44 | |
| 45 | |
| 46 | #[utoipa::path( |
| 47 | get, |
| 48 | path = "/{id}", |
| 49 | tag = "subjects", |
| 50 | security(("session_cookie" = [])), |
| 51 | params(("id" = String, Path, description = "Subject id")), |
| 52 | responses( |
| 53 | (status = 200, description = "The subject", body = SubjectResponse), |
| 54 | (status = 401, description = "Not authenticated", body = ErrorResponse), |
| 55 | (status = 403, description = "Not reached by the course, and not its creator or a manager/admin", body = ErrorResponse), |
| 56 | (status = 404, description = "Not found", body = ErrorResponse), |
| 57 | ), |
| 58 | )] |
| 59 | async fn get_subject( |
| 60 | State(st): State<AppState>, |
| 61 | CurrentUser(user): CurrentUser, |
| 62 | Path(id): Path<String>, |
| 63 | ) -> Result<Json<SubjectResponse>, AppError> { |
| 64 | let (subject, course) = subject_with_course(&id, &st.db).await?; |
| 65 | if !can_view_course(&course, &user, &st.db).await? { |
| 66 | return Err(AppError::Forbidden( |
| 67 | "only a user this course reaches, its creator, or a manager/admin can view this subject", |
| 68 | )); |
| 69 | } |
| 70 | Ok(Json(SubjectResponse::new(&subject))) |
| 71 | } |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | #[utoipa::path( |
| 77 | patch, |
| 78 | path = "/{id}", |
| 79 | tag = "subjects", |
| 80 | security(("session_cookie" = [])), |
| 81 | params(("id" = String, Path, description = "Subject id")), |
| 82 | request_body = UpdateSubject, |
| 83 | responses( |
| 84 | (status = 200, description = "Updated subject", body = SubjectResponse), |
| 85 | (status = 400, description = "Invalid name or description", body = ErrorResponse), |
| 86 | (status = 401, description = "Not authenticated", body = ErrorResponse), |
| 87 | (status = 403, description = "Not the course creator (and not a manager/admin)", body = ErrorResponse), |
| 88 | (status = 404, description = "Not found", body = ErrorResponse), |
| 89 | (status = 422, description = "The body does not fit this request: a field has the wrong type, or a required field is missing"), |
| 90 | ), |
| 91 | )] |
| 92 | async fn update_subject( |
| 93 | State(st): State<AppState>, |
| 94 | RequireTeacher(user): RequireTeacher, |
| 95 | Path(id): Path<String>, |
| 96 | Json(req): Json<UpdateSubject>, |
| 97 | ) -> Result<Json<SubjectResponse>, AppError> { |
| 98 | let (subject, course) = subject_with_course(&id, &st.db).await?; |
| 99 | if !can_manage_course(&course, &user) { |
| 100 | return Err(AppError::Forbidden( |
| 101 | "only the course creator or a manager/admin can edit this subject", |
| 102 | )); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | let name = req.name.as_deref().map(SubjectName::try_new).transpose()?; |
| 109 | let description = req |
| 110 | .description |
| 111 | .as_deref() |
| 112 | .map(SubjectDescription::try_new) |
| 113 | .transpose()?; |
| 114 | |
| 115 | let updated = subject::update(&st.db, subject, name, description).await?; |
| 116 | Ok(Json(SubjectResponse::new(&updated))) |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | #[utoipa::path( |
| 130 | delete, |
| 131 | path = "/{id}", |
| 132 | tag = "subjects", |
| 133 | security(("session_cookie" = [])), |
| 134 | params(("id" = String, Path, description = "Subject id")), |
| 135 | responses( |
| 136 | (status = 204, description = "Deleted"), |
| 137 | (status = 401, description = "Not authenticated", body = ErrorResponse), |
| 138 | (status = 403, description = "Not the course creator (and not a manager/admin)", body = ErrorResponse), |
| 139 | (status = 404, description = "Not found", body = ErrorResponse), |
| 140 | (status = 409, description = "Exam questions or homework still reference this subject", body = ErrorResponse), |
| 141 | ), |
| 142 | )] |
| 143 | async fn delete_subject( |
| 144 | State(st): State<AppState>, |
| 145 | RequireTeacher(user): RequireTeacher, |
| 146 | Path(id): Path<String>, |
| 147 | ) -> Result<StatusCode, AppError> { |
| 148 | let (subject, course) = subject_with_course(&id, &st.db).await?; |
| 149 | if !can_manage_course(&course, &user) { |
| 150 | return Err(AppError::Forbidden( |
| 151 | "only the course creator or a manager/admin can delete this subject", |
| 152 | )); |
| 153 | } |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | subject::delete(&st.db, subject).await?; |
| 160 | Ok(StatusCode::NO_CONTENT) |
| 161 | } |