A simple to-do app focused on tasks that can be completed within a specific time span.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

update tag

tobinio ced9d6e4 b897c42c

+55 -14
+53 -12
api/src/routes/tags.rs
··· 4 4 use http::StatusCode; 5 5 use sea_orm::ActiveValue::Set; 6 6 use sea_orm::{ColumnTrait, EntityTrait, ModelTrait, QueryFilter}; 7 + use tracing::warn; 7 8 use types::{HexColor, Tag as TagModel}; 8 9 use uuid::Uuid; 9 10 ··· 24 25 user: User, 25 26 Json(tag): Json<TagModel>, 26 27 ) -> Result<Json<TagModel>, (StatusCode, &'static str)> { 27 - let new_tag = Tag::insert(tag::ActiveModel { 28 + let existing_tag = Tag::find_by_id(tag.uuid) 29 + .one(&state.db_connection) 30 + .await 31 + .map_err(|e| { 32 + warn!("failed to find tag: {}", e); 33 + (StatusCode::INTERNAL_SERVER_ERROR, "failed to find tag") 34 + })?; 35 + 36 + let tag = tag::ActiveModel { 28 37 id: Set(tag.uuid), 29 38 owner_id: Set(user.uuid), 30 39 name: Set(tag.name), 31 40 color: Set(tag.color.as_str().to_string()), 32 - }) 33 - .exec_with_returning(&state.db_connection) 34 - .await 35 - .map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "failed to add tag"))?; 41 + }; 42 + 43 + let new_tag = match existing_tag { 44 + Some(existing_tag) => { 45 + Tag::update(tag) 46 + .exec(&state.db_connection) 47 + .await 48 + .map_err(|e| { 49 + warn!("failed to add tag: {}", e); 50 + (StatusCode::INTERNAL_SERVER_ERROR, "failed to add tag") 51 + })?; 52 + 53 + TagModel { 54 + uuid: existing_tag.id, 55 + name: existing_tag.name, 56 + color: HexColor::from_str(&existing_tag.color).unwrap(), 57 + } 58 + } 59 + None => { 60 + let new_tag = Tag::insert(tag) 61 + .exec_with_returning(&state.db_connection) 62 + .await 63 + .map_err(|e| { 64 + warn!("failed to add tag: {}", e); 65 + (StatusCode::INTERNAL_SERVER_ERROR, "failed to add tag") 66 + })?; 36 67 37 - Ok(Json(TagModel { 38 - uuid: new_tag.id, 39 - name: new_tag.name, 40 - color: HexColor::from_str(&new_tag.color).unwrap(), 41 - })) 68 + TagModel { 69 + uuid: new_tag.id, 70 + name: new_tag.name, 71 + color: HexColor::from_str(&new_tag.color).unwrap(), 72 + } 73 + } 74 + }; 75 + 76 + Ok(Json(new_tag)) 42 77 } 43 78 44 79 async fn get_all_tags( ··· 49 84 .filter(tag::Column::OwnerId.eq(user.uuid)) 50 85 .all(&state.db_connection) 51 86 .await 52 - .map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "failed to fetch tags"))?; 87 + .map_err(|e| { 88 + warn!("failed to fetch tag: {}", e); 89 + (StatusCode::INTERNAL_SERVER_ERROR, "failed to fetch tags") 90 + })?; 53 91 54 92 Ok(Json( 55 93 tags.into_iter() ··· 71 109 .filter(tag::Column::OwnerId.eq(user.uuid)) 72 110 .one(&state.db_connection) 73 111 .await 74 - .map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "failed to fetch tags"))?; 112 + .map_err(|e| { 113 + warn!("failed to fetch tag: {}", e); 114 + (StatusCode::INTERNAL_SERVER_ERROR, "failed to fetch tags") 115 + })?; 75 116 76 117 let Some(tag) = deleted_tag else { 77 118 return Err((StatusCode::NOT_FOUND, "tag not found"));
+1 -1
api/tests/categories/update.rs
··· 68 68 } 69 69 )) 70 70 .await; 71 - assert_eq!(response.status().as_str(), "400"); 71 + assert_eq!(response.status().as_str(), "422"); 72 72 73 73 let categories = client.get_categories_json().await; 74 74 assert_eq!(categories.len(), 1);
+1 -1
api/tests/tags/update.rs
··· 63 63 } 64 64 )) 65 65 .await; 66 - assert_eq!(response.status().as_str(), "400"); 66 + assert_eq!(response.status().as_str(), "422"); 67 67 68 68 let tags = client.get_tags_json().await; 69 69 assert_eq!(tags.len(), 1);