with memory db
This commit is contained in:
@@ -1,6 +1,17 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use axum::async_trait;
|
||||
use como_core::items::ItemService;
|
||||
use como_domain::item::{requests::CreateItemDto, responses::CreatedItemDto};
|
||||
use como_domain::item::{
|
||||
queries::{GetItemQuery, GetItemsQuery},
|
||||
requests::CreateItemDto,
|
||||
responses::CreatedItemDto,
|
||||
ItemDto,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub struct DefaultItemService {}
|
||||
|
||||
@@ -15,4 +26,59 @@ impl ItemService for DefaultItemService {
|
||||
async fn add_item(&self, _item: CreateItemDto) -> anyhow::Result<CreatedItemDto> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
async fn get_item(&self, _query: GetItemQuery) -> anyhow::Result<ItemDto> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
async fn get_items(&self, _query: GetItemsQuery) -> anyhow::Result<Vec<ItemDto>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MemoryItemService {
|
||||
item_store: Arc<Mutex<HashMap<String, ItemDto>>>,
|
||||
}
|
||||
|
||||
impl MemoryItemService {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
item_store: Arc::new(Mutex::new(HashMap::new())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ItemService for MemoryItemService {
|
||||
async fn add_item(&self, create_item: CreateItemDto) -> anyhow::Result<CreatedItemDto> {
|
||||
if let Ok(mut item_store) = self.item_store.lock() {
|
||||
let item = ItemDto {
|
||||
id: Uuid::new_v4(),
|
||||
title: create_item.name,
|
||||
description: None,
|
||||
state: como_domain::item::ItemState::Created,
|
||||
};
|
||||
|
||||
item_store.insert(item.id.to_string(), item.clone());
|
||||
|
||||
return Ok(item);
|
||||
} else {
|
||||
Err(anyhow::anyhow!("could not unlock item_store"))
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_item(&self, query: GetItemQuery) -> anyhow::Result<ItemDto> {
|
||||
if let Ok(item_store) = self.item_store.lock() {
|
||||
let item = item_store
|
||||
.get(&query.item_id.to_string())
|
||||
.ok_or(anyhow::anyhow!("could not find item"))?;
|
||||
return Ok(item.clone());
|
||||
} else {
|
||||
Err(anyhow::anyhow!("could not unlock item_store"))
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_items(&self, _query: GetItemsQuery) -> anyhow::Result<Vec<ItemDto>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user