From dcfeaf3e59f277b47012c5b20597b6d65080a3c5 Mon Sep 17 00:00:00 2001 From: ToBinio Date: Tue, 5 May 2026 10:52:05 +0200 Subject: [PATCH] handle tags while creating todo --- api/src/routes/todo.rs | 5 +++-- api/src/services/todos.rs | 45 ++++++++++++++++++++++----------------- api/tests/todos/add.rs | 21 ++++++++++++++++++ 3 files changed, 49 insertions(+), 22 deletions(-) diff --git a/api/src/routes/todo.rs b/api/src/routes/todo.rs index 9e8728b..f8b4506 100644 --- a/api/src/routes/todo.rs +++ b/api/src/routes/todo.rs @@ -8,7 +8,6 @@ use axum::{ }; use futures::Stream; use http::StatusCode; -use serde::Deserialize; use tokio_stream::{StreamExt, wrappers::BroadcastStream}; use tracing::warn; use types::Todo as TodoModel; @@ -108,7 +107,9 @@ async fn delete_todo( .await .map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "failed to delete tag"))?; - state.event_service.broadcast(events::Event::Tag(user.uuid)); + state + .event_service + .broadcast(events::Event::Todo(user.uuid)); Ok(Json(deleted_tag)) } diff --git a/api/src/services/todos.rs b/api/src/services/todos.rs index 0e53dfc..a5209f9 100644 --- a/api/src/services/todos.rs +++ b/api/src/services/todos.rs @@ -1,3 +1,4 @@ +use futures::FutureExt; use sea_orm::ActiveValue::{NotSet, Set}; use sea_orm::{ ColumnTrait, DatabaseConnection, DeleteResult, EntityTrait, QueryFilter, QueryOrder, @@ -7,7 +8,7 @@ use types::Todo as TodoModel; use uuid::Uuid; use crate::auth::User; -use crate::entities::{prelude::*, todo}; +use crate::entities::{prelude::*, todo, todo_tag}; #[derive(Debug, Deserialize)] pub struct AddTodoModel { @@ -59,16 +60,17 @@ impl TodoService { ) -> Result, sea_orm::DbErr> { Todo::find_by_id(id) .filter(todo::Column::OwnerId.eq(user.uuid)) - .one(db) + .find_with_related(TodoTag) + .all(db) .await - .map(|todo| { - todo.map(|todo| TodoModel { + .map(|todos| { + todos.first().map(|(todo, tags)| TodoModel { uuid: todo.id, - title: todo.title, - note: todo.note, + title: todo.title.to_string(), + note: todo.note.to_string(), time: None, - tags: vec![], - category: None, + tags: tags.iter().map(|tag| tag.tag_id).collect(), + category: todo.category_id, checks: vec![], }) }) @@ -95,24 +97,27 @@ impl TodoService { .unwrap_or(0); Todo::insert(todo::ActiveModel { - id: Set(todo.uuid), + id: Set(todo.uuid.clone()), owner_id: Set(user.uuid), title: Set(todo.title), note: Set(todo.note), category_id: Set(todo.category), order: Set(position), }) - .exec_with_returning(db) - .await - .map(|todo| TodoModel { - uuid: todo.id, - title: todo.title, - note: todo.note, - time: None, - tags: vec![], - category: None, - checks: vec![], - }) + .exec(db) + .await?; + + TodoTag::insert_many(todo.tags.iter().map(|tag| todo_tag::ActiveModel { + tag_id: Set(tag.clone()), + todo_id: Set(todo.uuid.clone()), + })) + .on_empty_do_nothing() + .exec(db) + .await?; + + Self::get_by_id(user, db, todo.uuid) + .await + .map(|todo| todo.unwrap()) } pub async fn update( diff --git a/api/tests/todos/add.rs b/api/tests/todos/add.rs index c9115cc..3bdde9f 100644 --- a/api/tests/todos/add.rs +++ b/api/tests/todos/add.rs @@ -306,6 +306,27 @@ async fn add_todo_with_labels() { let tag_uuid = Uuid::new_v4(); let category_uuid = Uuid::new_v4(); + client + .add_category_json(json!( + { + "uuid": category_uuid, + "name": "Test Category", + "color": "#233212", + "icon": "icon", + } + )) + .await; + + client + .add_tag_json(json!( + { + "uuid": tag_uuid, + "name": "Test Tag", + "color": "#233212", + } + )) + .await; + let todo = Todo { uuid: todo_uuid, title: "Test Todo".to_string(), -- 2.51.2