@@ -12,30 +12,114 @@ use como_domain::{
|
||||
responses::CreatedItemDto,
|
||||
ItemDto,
|
||||
},
|
||||
users::User,
|
||||
user::ContextUserExt,
|
||||
Context,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub struct DefaultItemService {}
|
||||
use crate::database::ConnectionPool;
|
||||
|
||||
pub struct DefaultItemService {
|
||||
pool: ConnectionPool,
|
||||
}
|
||||
|
||||
impl DefaultItemService {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
pub fn new(connection_pool: ConnectionPool) -> Self {
|
||||
Self {
|
||||
pool: connection_pool,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ItemService for DefaultItemService {
|
||||
async fn add_item(&self, _item: CreateItemDto, user: &User) -> anyhow::Result<CreatedItemDto> {
|
||||
todo!()
|
||||
async fn add_item(
|
||||
&self,
|
||||
context: &Context,
|
||||
item: CreateItemDto,
|
||||
) -> anyhow::Result<CreatedItemDto> {
|
||||
let state = serde_json::to_string(&como_domain::item::ItemState::Created {})?;
|
||||
let user_id = context.get_user_id().ok_or(anyhow::anyhow!("no user id"))?;
|
||||
|
||||
let rec = sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO items (id, title, description, state, project_id, user_id, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, now(), now())
|
||||
RETURNING id, title, description, state, project_id
|
||||
"#,
|
||||
Uuid::new_v4(),
|
||||
item.name,
|
||||
item.description,
|
||||
state,
|
||||
item.project_id,
|
||||
user_id,
|
||||
)
|
||||
.fetch_one(&self.pool)
|
||||
.await?;
|
||||
|
||||
Ok(CreatedItemDto {
|
||||
id: rec.id,
|
||||
title: rec.title,
|
||||
description: rec.description,
|
||||
state: como_domain::item::ItemState::Created {},
|
||||
project_id: rec.project_id,
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_item(&self, _query: GetItemQuery, user: &User) -> anyhow::Result<ItemDto> {
|
||||
todo!()
|
||||
async fn get_item(&self, context: &Context, query: GetItemQuery) -> anyhow::Result<ItemDto> {
|
||||
let user_id = context.get_user_id().ok_or(anyhow::anyhow!("no user id"))?;
|
||||
|
||||
let rec = sqlx::query!(
|
||||
r#"
|
||||
SELECT id, title, description, state, project_id
|
||||
FROM items
|
||||
WHERE id = $1 AND user_id = $2
|
||||
"#,
|
||||
query.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,
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_items(&self, _query: GetItemsQuery, user: &User) -> anyhow::Result<Vec<ItemDto>> {
|
||||
todo!()
|
||||
async fn get_items(
|
||||
&self,
|
||||
context: &Context,
|
||||
query: GetItemsQuery,
|
||||
) -> anyhow::Result<Vec<ItemDto>> {
|
||||
let user_id = context.get_user_id().ok_or(anyhow::anyhow!("no user id"))?;
|
||||
|
||||
let recs = sqlx::query!(
|
||||
r#"
|
||||
SELECT id, title, description, state, project_id
|
||||
FROM items
|
||||
WHERE user_id = $1 and project_id = $2
|
||||
LIMIT 500
|
||||
"#,
|
||||
user_id,
|
||||
query.project_id,
|
||||
)
|
||||
.fetch_all(&self.pool)
|
||||
.await?;
|
||||
|
||||
Ok(recs
|
||||
.into_iter()
|
||||
.map(|rec| ItemDto {
|
||||
id: rec.id,
|
||||
title: rec.title,
|
||||
description: rec.description,
|
||||
state: serde_json::from_str(&rec.state).unwrap(),
|
||||
project_id: rec.project_id,
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,8 +139,8 @@ impl MemoryItemService {
|
||||
impl ItemService for MemoryItemService {
|
||||
async fn add_item(
|
||||
&self,
|
||||
_context: &Context,
|
||||
create_item: CreateItemDto,
|
||||
user: &User,
|
||||
) -> anyhow::Result<CreatedItemDto> {
|
||||
if let Ok(mut item_store) = self.item_store.lock() {
|
||||
let item = ItemDto {
|
||||
@@ -75,7 +159,7 @@ impl ItemService for MemoryItemService {
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_item(&self, query: GetItemQuery, user: &User) -> anyhow::Result<ItemDto> {
|
||||
async fn get_item(&self, _context: &Context, query: GetItemQuery) -> anyhow::Result<ItemDto> {
|
||||
if let Ok(item_store) = self.item_store.lock() {
|
||||
let item = item_store
|
||||
.get(&query.item_id.to_string())
|
||||
@@ -86,7 +170,11 @@ impl ItemService for MemoryItemService {
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_items(&self, _query: GetItemsQuery, user: &User) -> anyhow::Result<Vec<ItemDto>> {
|
||||
async fn get_items(
|
||||
&self,
|
||||
_context: &Context,
|
||||
_query: GetItemsQuery,
|
||||
) -> anyhow::Result<Vec<ItemDto>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user