with memory db

This commit is contained in:
Kasper Juul Hermansen 2022-10-04 22:39:57 +02:00
parent c7f8dc6198
commit 1d4cda7c48
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
13 changed files with 326 additions and 14 deletions

View File

@ -1,11 +1,18 @@
use std::sync::Arc;
use async_trait::async_trait;
use como_domain::item::{requests::CreateItemDto, responses::CreatedItemDto};
use como_domain::item::{
queries::{GetItemQuery, GetItemsQuery},
requests::CreateItemDto,
responses::CreatedItemDto,
ItemDto,
};
pub type DynItemService = Arc<dyn ItemService + Send + Sync>;
#[async_trait]
pub trait ItemService {
async fn add_item(&self, item: CreateItemDto) -> anyhow::Result<CreatedItemDto>;
async fn get_item(&self, query: GetItemQuery) -> anyhow::Result<ItemDto>;
async fn get_items(&self, query: GetItemsQuery) -> anyhow::Result<Vec<ItemDto>>;
}

View File

@ -1,8 +1,15 @@
use std::sync::Arc;
use async_trait::async_trait;
use como_domain::projects::{
queries::{GetProjectQuery, GetProjectsQuery},
ProjectDto,
};
pub type DynProjectService = Arc<dyn ProjectService + Send + Sync>;
#[async_trait]
pub trait ProjectService {}
pub trait ProjectService {
async fn get_project(&self, query: GetProjectQuery) -> anyhow::Result<ProjectDto>;
async fn get_projects(&self, query: GetProjectsQuery) -> anyhow::Result<Vec<ProjectDto>>;
}

View File

@ -1,3 +1,4 @@
pub mod queries;
pub mod requests;
pub mod responses;
@ -13,7 +14,7 @@ pub enum ItemState {
Deleted,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject, SimpleObject)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject)]
pub struct ItemDto {
pub id: Uuid,
pub title: String,

View File

@ -0,0 +1,13 @@
use async_graphql::InputObject;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject)]
pub struct GetItemQuery {
pub item_id: Uuid,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject)]
pub struct GetItemsQuery {
pub user_id: Uuid,
}

View File

@ -1,10 +1,12 @@
pub mod queries;
pub mod requests;
pub mod responses;
use async_graphql::{InputObject, SimpleObject};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject, SimpleObject)]
pub struct ProjectDto {
pub id: Uuid,
pub name: String,

View File

@ -0,0 +1,14 @@
use async_graphql::InputObject;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject)]
pub struct GetProjectQuery {
pub project_id: Option<Uuid>,
pub item_id: Option<Uuid>,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject)]
pub struct GetProjectsQuery {
pub user_id: Uuid,
}

View File

@ -1,8 +1,21 @@
use async_graphql::{Context, EmptySubscription, Object, Schema};
use como_domain::item::{requests::CreateItemDto, responses::CreatedItemDto};
use como_domain::{
item::{
queries::{GetItemQuery, GetItemsQuery},
requests::CreateItemDto,
responses::CreatedItemDto,
ItemDto,
},
projects::{
queries::{GetProjectQuery, GetProjectsQuery},
ProjectDto,
},
};
use como_infrastructure::register::ServiceRegister;
use crate::items::{CreatedItem, Item};
pub type ComoSchema = Schema<QueryRoot, MutationRoot, EmptySubscription>;
pub struct MutationRoot;
@ -49,12 +62,14 @@ impl MutationRoot {
&self,
ctx: &Context<'_>,
item: CreateItemDto,
) -> anyhow::Result<CreatedItemDto> {
) -> anyhow::Result<CreatedItem> {
let services_register = ctx.data_unchecked::<ServiceRegister>();
let created_item = services_register.item_service.add_item(item).await?;
Ok(created_item)
Ok(CreatedItem {
id: created_item.id,
})
}
}
@ -62,7 +77,51 @@ pub struct QueryRoot;
#[Object]
impl QueryRoot {
async fn hello(&self, _ctx: &Context<'_>) -> String {
"hello".into()
// Items
async fn get_item(&self, ctx: &Context<'_>, query: GetItemQuery) -> anyhow::Result<Item> {
let item = ctx
.data_unchecked::<ServiceRegister>()
.item_service
.get_item(query)
.await?;
Ok(Item::from(item))
}
async fn get_items(
&self,
ctx: &Context<'_>,
query: GetItemsQuery,
) -> anyhow::Result<Vec<Item>> {
let items = ctx
.data_unchecked::<ServiceRegister>()
.item_service
.get_items(query)
.await?;
Ok(items.iter().map(|i| Item::from(i.clone())).collect())
}
// Projects
async fn get_project(
&self,
ctx: &Context<'_>,
query: GetProjectQuery,
) -> anyhow::Result<ProjectDto> {
ctx.data_unchecked::<ServiceRegister>()
.project_service
.get_project(query)
.await
}
async fn get_projects(
&self,
ctx: &Context<'_>,
query: GetProjectsQuery,
) -> anyhow::Result<Vec<ProjectDto>> {
ctx.data_unchecked::<ServiceRegister>()
.project_service
.get_projects(query)
.await
}
}

76
como_gql/src/items.rs Normal file
View File

@ -0,0 +1,76 @@
use async_graphql::{Context, Object};
use como_domain::{
item::{queries::GetItemQuery, ItemDto, ItemState},
projects::queries::GetProjectQuery,
};
use como_infrastructure::register::ServiceRegister;
use uuid::Uuid;
use crate::projects::Project;
pub struct CreatedItem {
pub id: Uuid,
}
#[Object]
impl CreatedItem {
pub async fn item(&self, ctx: &Context<'_>) -> anyhow::Result<Item> {
let item = ctx
.data_unchecked::<ServiceRegister>()
.item_service
.get_item(GetItemQuery { item_id: self.id })
.await?;
Ok(item.into())
}
}
pub struct Item {
pub id: Uuid,
pub title: String,
pub description: Option<String>,
pub state: ItemState,
}
#[Object]
impl Item {
pub async fn id(&self, _ctx: &Context<'_>) -> anyhow::Result<Uuid> {
return Ok(self.id);
}
pub async fn title(&self, _ctx: &Context<'_>) -> anyhow::Result<String> {
return Ok(self.title.clone());
}
pub async fn description(&self, _ctx: &Context<'_>) -> anyhow::Result<Option<String>> {
return Ok(self.description.clone());
}
pub async fn state(&self, _ctx: &Context<'_>) -> anyhow::Result<ItemState> {
return Ok(self.state);
}
pub async fn project(&self, ctx: &Context<'_>) -> anyhow::Result<Project> {
let project = ctx
.data_unchecked::<ServiceRegister>()
.project_service
.get_project(GetProjectQuery {
item_id: Some(self.id),
project_id: None,
})
.await?;
Ok(project.into())
}
}
impl From<ItemDto> for Item {
fn from(dto: ItemDto) -> Self {
Self {
id: dto.id,
title: dto.title,
description: dto.description,
state: dto.state,
}
}
}

View File

@ -9,6 +9,8 @@ use async_graphql::http::{playground_source, GraphQLPlaygroundConfig};
use graphql::ComoSchema;
pub mod graphql;
mod items;
mod projects;
pub async fn graphql_handler(
schema: Extension<ComoSchema>,

18
como_gql/src/projects.rs Normal file
View File

@ -0,0 +1,18 @@
use async_graphql::SimpleObject;
use como_domain::projects::ProjectDto;
use uuid::Uuid;
#[derive(SimpleObject)]
pub struct Project {
pub id: Uuid,
pub name: String,
}
impl From<ProjectDto> for Project {
fn from(dto: ProjectDto) -> Self {
Self {
id: dto.id,
name: dto.name,
}
}
}

View File

@ -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");

View File

@ -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!()
}
}

View File

@ -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!()
}
}