@@ -1,24 +1,22 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use crate::database::ConnectionPool;
|
||||
use async_trait::async_trait;
|
||||
use como_core::items::ItemService;
|
||||
use como_domain::{
|
||||
item::{
|
||||
queries::{GetItemQuery, GetItemsQuery},
|
||||
requests::CreateItemDto,
|
||||
requests::{CreateItemDto, UpdateItemDto},
|
||||
responses::CreatedItemDto,
|
||||
ItemDto,
|
||||
},
|
||||
user::ContextUserExt,
|
||||
Context,
|
||||
};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::database::ConnectionPool;
|
||||
|
||||
pub struct DefaultItemService {
|
||||
pool: ConnectionPool,
|
||||
}
|
||||
@@ -48,7 +46,7 @@ impl ItemService for DefaultItemService {
|
||||
RETURNING id, title, description, state, project_id
|
||||
"#,
|
||||
Uuid::new_v4(),
|
||||
item.name,
|
||||
item.title,
|
||||
item.description,
|
||||
state,
|
||||
item.project_id,
|
||||
@@ -121,6 +119,41 @@ impl ItemService for DefaultItemService {
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
async fn update_item(&self, context: &Context, item: UpdateItemDto) -> anyhow::Result<ItemDto> {
|
||||
let state = item.state.map(|s| serde_json::to_string(&s)).transpose()?;
|
||||
let user_id = context.get_user_id().ok_or(anyhow::anyhow!("no user id"))?;
|
||||
|
||||
let rec = sqlx::query!(
|
||||
r#"
|
||||
UPDATE items
|
||||
SET
|
||||
title = COALESCE($1, title),
|
||||
description = COALESCE($2, description),
|
||||
state = COALESCE($3, state),
|
||||
project_id = COALESCE($4, project_id),
|
||||
updated_at = now()
|
||||
WHERE id = $5 AND user_id = $6
|
||||
RETURNING id, title, description, state, project_id
|
||||
"#,
|
||||
item.title,
|
||||
item.description,
|
||||
state,
|
||||
item.project_id,
|
||||
item.id,
|
||||
user_id,
|
||||
)
|
||||
.fetch_one(&self.pool)
|
||||
.await?;
|
||||
|
||||
Ok(ItemDto {
|
||||
id: rec.id,
|
||||
title: rec.title,
|
||||
description: rec.description,
|
||||
state: serde_json::from_str(&rec.state)?,
|
||||
project_id: rec.project_id,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MemoryItemService {
|
||||
@@ -145,7 +178,7 @@ impl ItemService for MemoryItemService {
|
||||
if let Ok(mut item_store) = self.item_store.lock() {
|
||||
let item = ItemDto {
|
||||
id: Uuid::new_v4(),
|
||||
title: create_item.name,
|
||||
title: create_item.title,
|
||||
description: create_item.description,
|
||||
state: como_domain::item::ItemState::Created,
|
||||
project_id: create_item.project_id,
|
||||
@@ -177,4 +210,8 @@ impl ItemService for MemoryItemService {
|
||||
) -> anyhow::Result<Vec<ItemDto>> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
async fn update_item(&self, context: &Context, item: UpdateItemDto) -> anyhow::Result<ItemDto> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user