Skip to content

Commit

Permalink
Merge pull request #22 from TheAwiteb/null-update-value
Browse files Browse the repository at this point in the history
`UpdateTodoSchema` that supports null values for unchanged fields
  • Loading branch information
TheAwiteb authored Feb 23, 2023
2 parents fda88cf + db0cb0c commit 261fe64
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/api/todo/update.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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",
Expand All @@ -33,7 +33,7 @@ use uuid::Uuid;
#[put("/{uuid}")]
pub async fn update_todo(
req: HttpRequest,
payload: web::Json<TodoContentSchema>,
payload: web::Json<UpdateTodoSchema>,
uuid: web::Path<Uuid>,
db: web::Data<DatabaseConnection>,
) -> ApiResult<TodoSchema> {
Expand All @@ -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)
}
1 change: 1 addition & 0 deletions src/api_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
3 changes: 2 additions & 1 deletion src/schemas/todo/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
23 changes: 23 additions & 0 deletions src/schemas/todo/update.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
/// The status of the todo, can be `null` to keep the original status
#[schema(value_type = Option<String>, example = "completed")]
pub status: Option<TodoStatus>,
}

impl Default for UpdateTodoSchema {
fn default() -> Self {
Self {
title: Some("Todo title".to_string()),
status: None,
}
}
}

0 comments on commit 261fe64

Please sign in to comment.