From 922685873abce41687e9c6c52afa34e749da7abb Mon Sep 17 00:00:00 2001 From: TheAwiteb Date: Thu, 23 Feb 2023 17:39:13 +0300 Subject: [PATCH 1/2] `UpdateTodoSchema` that supports null values for unchanged fields --- src/api_docs.rs | 1 + src/schemas/todo/mod.rs | 3 ++- src/schemas/todo/update.rs | 23 +++++++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/schemas/todo/update.rs diff --git a/src/api_docs.rs b/src/api_docs.rs index ad06aab..f151f81 100644 --- a/src/api_docs.rs +++ b/src/api_docs.rs @@ -34,6 +34,7 @@ use utoipa::{Modify, OpenApi}; crate::schemas::todo::TodoSchema, crate::schemas::todo::TodoListSchema, crate::schemas::todo::TodoListMetaSchema, + crate::schemas::todo::UpdateTodoSchema, // Server metadata crate::schemas::server_metadata::ServerMetadataSchema, ) diff --git a/src/schemas/todo/mod.rs b/src/schemas/todo/mod.rs index a5fa5b7..222b120 100644 --- a/src/schemas/todo/mod.rs +++ b/src/schemas/todo/mod.rs @@ -1,8 +1,9 @@ mod content; mod list; +mod update; use actix_web::{body::BoxBody, Responder}; -pub use {content::*, list::*}; +pub use {content::*, list::*, update::*}; use entity::todo::Status as TodoStatus; use serde::{Deserialize, Serialize}; diff --git a/src/schemas/todo/update.rs b/src/schemas/todo/update.rs new file mode 100644 index 0000000..a32466a --- /dev/null +++ b/src/schemas/todo/update.rs @@ -0,0 +1,23 @@ +use entity::todo::Status as TodoStatus; +use serde::{Deserialize, Serialize}; +use utoipa::ToSchema; + +/// The schema used to update a todo, supports null values for unchanged fields +#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] +pub struct UpdateTodoSchema { + /// The title of the todo, can be `null` to keep the original title + #[schema(example = "Todo title")] + pub title: Option, + /// The status of the todo, can be `null` to keep the original status + #[schema(value_type = Option, example = "completed")] + pub status: Option, +} + +impl Default for UpdateTodoSchema { + fn default() -> Self { + Self { + title: Some("Todo title".to_string()), + status: None, + } + } +} From db0cb0cb20b9ffee515beac6183677ba53498f93 Mon Sep 17 00:00:00 2001 From: TheAwiteb Date: Thu, 23 Feb 2023 17:40:37 +0300 Subject: [PATCH 2/2] Use `UpdateTodoSchema` in update todo endpoint --- src/api/todo/update.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/api/todo/update.rs b/src/api/todo/update.rs index 8a5ed32..592fd5f 100644 --- a/src/api/todo/update.rs +++ b/src/api/todo/update.rs @@ -1,7 +1,7 @@ use crate::api::auth::utils::req_auth; use crate::api::todo::utils; use crate::errors::Result as ApiResult; -use crate::schemas::todo::{TodoContentSchema, TodoSchema}; +use crate::schemas::todo::{TodoSchema, UpdateTodoSchema}; use crate::schemas::{message::MessageSchema, traits::OpenApiExample}; use actix_web::{put, web, HttpRequest}; use sea_orm::DatabaseConnection; @@ -10,7 +10,7 @@ use uuid::Uuid; /// Update a single todo by uuid, only the title and status can be updated. #[utoipa::path( context_path = "/api/todos", - request_body = TodoContentSchema, + request_body = UpdateTodoSchema, params( ( "uuid", description = "The uuid of the todo", @@ -33,7 +33,7 @@ use uuid::Uuid; #[put("/{uuid}")] pub async fn update_todo( req: HttpRequest, - payload: web::Json, + payload: web::Json, uuid: web::Path, db: web::Data, ) -> ApiResult { @@ -42,8 +42,8 @@ pub async fn update_todo( let user = req_auth(req, db).await?; let todo = utils::find_todo_by_uuid(*uuid, user.id, db).await?; // If the title is not changed, then set it to None. Otherwise, set it to Some(payload.title) - let todo_title = todo.title.ne(&payload.title).then_some(payload.title); - utils::update_todo(todo, todo_title, Some(payload.status), db) + let todo_title = payload.title.filter(|title| title != &todo.title); + utils::update_todo(todo, todo_title, payload.status, db) .await .map(TodoSchema::from) }