with memory db
This commit is contained in:
@@ -7,7 +7,8 @@ use crate::{
|
||||
configs::AppConfig,
|
||||
database::ConnectionPool,
|
||||
services::{
|
||||
item_service::DefaultItemService, project_service::DefaultProjectService,
|
||||
item_service::{DefaultItemService, MemoryItemService},
|
||||
project_service::{DefaultProjectService, MemoryProjectService},
|
||||
user_service::DefaultUserService,
|
||||
},
|
||||
};
|
||||
@@ -23,8 +24,8 @@ impl ServiceRegister {
|
||||
pub fn new(pool: ConnectionPool, _config: Arc<AppConfig>) -> Self {
|
||||
info!("creating services");
|
||||
|
||||
let item_service = Arc::new(DefaultItemService::new()) as DynItemService;
|
||||
let project_service = Arc::new(DefaultProjectService::new()) as DynProjectService;
|
||||
let item_service = Arc::new(MemoryItemService::new()) as DynItemService;
|
||||
let project_service = Arc::new(MemoryProjectService::new()) as DynProjectService;
|
||||
let user_service = Arc::new(DefaultUserService::new(pool.clone())) as DynUserService;
|
||||
|
||||
info!("services created succesfully");
|
||||
|
@@ -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!()
|
||||
}
|
||||
}
|
||||
|
@@ -1,4 +1,12 @@
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use axum::async_trait;
|
||||
use como_core::projects::ProjectService;
|
||||
use como_domain::projects::{
|
||||
queries::{GetProjectQuery, GetProjectsQuery},
|
||||
ProjectDto,
|
||||
};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
pub struct DefaultProjectService {}
|
||||
|
||||
@@ -8,4 +16,42 @@ impl DefaultProjectService {
|
||||
}
|
||||
}
|
||||
|
||||
impl ProjectService for DefaultProjectService {}
|
||||
#[async_trait]
|
||||
impl ProjectService for DefaultProjectService {
|
||||
async fn get_project(&self, _query: GetProjectQuery) -> anyhow::Result<ProjectDto> {
|
||||
todo!()
|
||||
}
|
||||
async fn get_projects(&self, _query: GetProjectsQuery) -> anyhow::Result<Vec<ProjectDto>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MemoryProjectService {
|
||||
project_store: Arc<Mutex<HashMap<String, ProjectDto>>>,
|
||||
}
|
||||
|
||||
impl MemoryProjectService {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
project_store: Arc::new(Mutex::new(HashMap::new())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ProjectService for MemoryProjectService {
|
||||
async fn get_project(&self, query: GetProjectQuery) -> anyhow::Result<ProjectDto> {
|
||||
let ps = self.project_store.lock().await;
|
||||
if let Some(item_id) = query.item_id {
|
||||
Ok(ps
|
||||
.get(&item_id.to_string())
|
||||
.ok_or(anyhow::anyhow!("could not find project"))?
|
||||
.clone())
|
||||
} else {
|
||||
Err(anyhow::anyhow!("could not find project"))
|
||||
}
|
||||
}
|
||||
async fn get_projects(&self, _query: GetProjectsQuery) -> anyhow::Result<Vec<ProjectDto>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user