Skip to content

Commit

Permalink
Use UpdateTodoSchema in update todo endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAwiteb committed Feb 23, 2023
1 parent 9226858 commit db0cb0c
Showing 1 changed file with 5 additions and 5 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)
}

0 comments on commit db0cb0c

Please sign in to comment.