feat: working projects and items

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2023-05-28 17:17:22 +02:00
parent 6dc0c24443
commit 12c7c8f6ee
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
10 changed files with 98 additions and 39 deletions

View File

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

View File

@ -20,4 +20,5 @@ pub struct ItemDto {
pub title: String, pub title: String,
pub description: Option<String>, pub description: Option<String>,
pub state: ItemState, pub state: ItemState,
pub project_id: Uuid,
} }

View File

@ -9,5 +9,5 @@ pub struct GetItemQuery {
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject)] #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject)]
pub struct GetItemsQuery { pub struct GetItemsQuery {
pub user_id: Uuid, pub project_id: Uuid,
} }

View File

@ -1,7 +1,11 @@
use async_graphql::InputObject; use async_graphql::InputObject;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject)] #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject)]
pub struct CreateItemDto { pub struct CreateItemDto {
pub name: String, pub name: String,
pub description: Option<String>,
pub project_id: Uuid,
} }

View File

@ -4,6 +4,5 @@ use uuid::Uuid;
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject)] #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject)]
pub struct GetProjectQuery { pub struct GetProjectQuery {
pub project_id: Option<Uuid>, pub project_id: Uuid,
pub item_id: Option<Uuid>,
} }

View File

@ -19,9 +19,11 @@ impl MutationRoot {
ctx: &Context<'_>, ctx: &Context<'_>,
item: CreateItemDto, item: CreateItemDto,
) -> anyhow::Result<CreatedItem> { ) -> anyhow::Result<CreatedItem> {
let user = ctx.data_unchecked::<User>();
let services_register = ctx.data_unchecked::<ServiceRegister>(); let services_register = ctx.data_unchecked::<ServiceRegister>();
let created_item = services_register.item_service.add_item(item).await?; let created_item = services_register.item_service.add_item(item, user).await?;
Ok(CreatedItem { Ok(CreatedItem {
id: created_item.id, id: created_item.id,
@ -51,10 +53,12 @@ pub struct QueryRoot;
#[Object] #[Object]
impl QueryRoot { impl QueryRoot {
async fn get_item(&self, ctx: &Context<'_>, query: GetItemQuery) -> anyhow::Result<Item> { async fn get_item(&self, ctx: &Context<'_>, query: GetItemQuery) -> anyhow::Result<Item> {
let user = ctx.data_unchecked::<User>();
let item = ctx let item = ctx
.data_unchecked::<ServiceRegister>() .data_unchecked::<ServiceRegister>()
.item_service .item_service
.get_item(query) .get_item(query, user)
.await?; .await?;
Ok(Item::from(item)) Ok(Item::from(item))
@ -65,10 +69,12 @@ impl QueryRoot {
ctx: &Context<'_>, ctx: &Context<'_>,
query: GetItemsQuery, query: GetItemsQuery,
) -> anyhow::Result<Vec<Item>> { ) -> anyhow::Result<Vec<Item>> {
let user = ctx.data_unchecked::<User>();
let items = ctx let items = ctx
.data_unchecked::<ServiceRegister>() .data_unchecked::<ServiceRegister>()
.item_service .item_service
.get_items(query) .get_items(query, user)
.await?; .await?;
Ok(items.iter().map(|i| Item::from(i.clone())).collect()) Ok(items.iter().map(|i| Item::from(i.clone())).collect())

View File

@ -2,6 +2,7 @@ use async_graphql::{Context, Object};
use como_domain::{ use como_domain::{
item::{queries::GetItemQuery, ItemDto, ItemState}, item::{queries::GetItemQuery, ItemDto, ItemState},
projects::queries::GetProjectQuery, projects::queries::GetProjectQuery,
users::User,
}; };
use como_infrastructure::register::ServiceRegister; use como_infrastructure::register::ServiceRegister;
use uuid::Uuid; use uuid::Uuid;
@ -15,10 +16,12 @@ pub struct CreatedItem {
#[Object] #[Object]
impl CreatedItem { impl CreatedItem {
pub async fn item(&self, ctx: &Context<'_>) -> anyhow::Result<Item> { pub async fn item(&self, ctx: &Context<'_>) -> anyhow::Result<Item> {
let user = ctx.data_unchecked::<User>();
let item = ctx let item = ctx
.data_unchecked::<ServiceRegister>() .data_unchecked::<ServiceRegister>()
.item_service .item_service
.get_item(GetItemQuery { item_id: self.id }) .get_item(GetItemQuery { item_id: self.id }, user)
.await?; .await?;
Ok(item.into()) Ok(item.into())
@ -30,6 +33,7 @@ pub struct Item {
pub title: String, pub title: String,
pub description: Option<String>, pub description: Option<String>,
pub state: ItemState, pub state: ItemState,
pub project_id: Uuid,
} }
#[Object] #[Object]
@ -55,13 +59,16 @@ impl Item {
.data_unchecked::<ServiceRegister>() .data_unchecked::<ServiceRegister>()
.project_service .project_service
.get_project(GetProjectQuery { .get_project(GetProjectQuery {
item_id: Some(self.id), project_id: self.project_id,
project_id: None,
}) })
.await?; .await?;
Ok(project.into()) Ok(project.into())
} }
pub async fn project_id(&self, _ctx: &Context<'_>) -> anyhow::Result<Uuid> {
return Ok(self.project_id);
}
} }
impl From<ItemDto> for Item { impl From<ItemDto> for Item {
@ -71,6 +78,7 @@ impl From<ItemDto> for Item {
title: dto.title, title: dto.title,
description: dto.description, description: dto.description,
state: dto.state, state: dto.state,
project_id: dto.project_id,
} }
} }
} }

View File

@ -1,13 +1,47 @@
use async_graphql::SimpleObject; use async_graphql::{Context, Object};
use como_domain::projects::ProjectDto; use como_domain::projects::ProjectDto;
use como_domain::users::User;
use como_infrastructure::register::ServiceRegister;
use uuid::Uuid; use uuid::Uuid;
#[derive(SimpleObject)] use crate::items::Item;
pub struct Project { pub struct Project {
pub id: Uuid, pub id: Uuid,
pub name: String, pub name: String,
} }
#[Object]
impl Project {
async fn id(&self) -> &Uuid {
&self.id
}
async fn name(&self) -> &String {
&self.name
}
async fn items(&self, ctx: &Context<'_>) -> anyhow::Result<Vec<Item>> {
let user = ctx.data_unchecked::<User>();
let items = ctx
.data_unchecked::<ServiceRegister>()
.item_service
.get_items(
como_domain::item::queries::GetItemsQuery {
project_id: self.id,
},
user,
)
.await?
.iter()
.map(|i| Item::from(i.clone()))
.collect::<Vec<_>>();
Ok(items)
}
}
impl From<ProjectDto> for Project { impl From<ProjectDto> for Project {
fn from(dto: ProjectDto) -> Self { fn from(dto: ProjectDto) -> Self {
Self { Self {

View File

@ -5,11 +5,14 @@ use std::{
use async_trait::async_trait; use async_trait::async_trait;
use como_core::items::ItemService; use como_core::items::ItemService;
use como_domain::item::{ use como_domain::{
queries::{GetItemQuery, GetItemsQuery}, item::{
requests::CreateItemDto, queries::{GetItemQuery, GetItemsQuery},
responses::CreatedItemDto, requests::CreateItemDto,
ItemDto, responses::CreatedItemDto,
ItemDto,
},
users::User,
}; };
use uuid::Uuid; use uuid::Uuid;
@ -23,15 +26,15 @@ impl DefaultItemService {
#[async_trait] #[async_trait]
impl ItemService for DefaultItemService { impl ItemService for DefaultItemService {
async fn add_item(&self, _item: CreateItemDto) -> anyhow::Result<CreatedItemDto> { async fn add_item(&self, _item: CreateItemDto, user: &User) -> anyhow::Result<CreatedItemDto> {
todo!() todo!()
} }
async fn get_item(&self, _query: GetItemQuery) -> anyhow::Result<ItemDto> { async fn get_item(&self, _query: GetItemQuery, user: &User) -> anyhow::Result<ItemDto> {
todo!() todo!()
} }
async fn get_items(&self, _query: GetItemsQuery) -> anyhow::Result<Vec<ItemDto>> { async fn get_items(&self, _query: GetItemsQuery, user: &User) -> anyhow::Result<Vec<ItemDto>> {
todo!() todo!()
} }
} }
@ -50,13 +53,18 @@ impl MemoryItemService {
#[async_trait] #[async_trait]
impl ItemService for MemoryItemService { impl ItemService for MemoryItemService {
async fn add_item(&self, create_item: CreateItemDto) -> anyhow::Result<CreatedItemDto> { async fn add_item(
&self,
create_item: CreateItemDto,
user: &User,
) -> anyhow::Result<CreatedItemDto> {
if let Ok(mut item_store) = self.item_store.lock() { if let Ok(mut item_store) = self.item_store.lock() {
let item = ItemDto { let item = ItemDto {
id: Uuid::new_v4(), id: Uuid::new_v4(),
title: create_item.name, title: create_item.name,
description: None, description: create_item.description,
state: como_domain::item::ItemState::Created, state: como_domain::item::ItemState::Created,
project_id: create_item.project_id,
}; };
item_store.insert(item.id.to_string(), item.clone()); item_store.insert(item.id.to_string(), item.clone());
@ -67,7 +75,7 @@ impl ItemService for MemoryItemService {
} }
} }
async fn get_item(&self, query: GetItemQuery) -> anyhow::Result<ItemDto> { async fn get_item(&self, query: GetItemQuery, user: &User) -> anyhow::Result<ItemDto> {
if let Ok(item_store) = self.item_store.lock() { if let Ok(item_store) = self.item_store.lock() {
let item = item_store let item = item_store
.get(&query.item_id.to_string()) .get(&query.item_id.to_string())
@ -78,7 +86,7 @@ impl ItemService for MemoryItemService {
} }
} }
async fn get_items(&self, _query: GetItemsQuery) -> anyhow::Result<Vec<ItemDto>> { async fn get_items(&self, _query: GetItemsQuery, user: &User) -> anyhow::Result<Vec<ItemDto>> {
todo!() todo!()
} }
} }

View File

@ -49,14 +49,10 @@ impl MemoryProjectService {
impl ProjectService for MemoryProjectService { impl ProjectService for MemoryProjectService {
async fn get_project(&self, query: GetProjectQuery) -> anyhow::Result<ProjectDto> { async fn get_project(&self, query: GetProjectQuery) -> anyhow::Result<ProjectDto> {
let ps = self.project_store.lock().await; let ps = self.project_store.lock().await;
if let Some(item_id) = query.item_id { Ok(ps
Ok(ps .get(&query.project_id.to_string())
.get(&item_id.to_string()) .ok_or(anyhow::anyhow!("could not find project"))?
.ok_or(anyhow::anyhow!("could not find project"))? .clone())
.clone())
} else {
Err(anyhow::anyhow!("could not find project"))
}
} }
async fn get_projects(&self, user: &User) -> anyhow::Result<Vec<ProjectDto>> { async fn get_projects(&self, user: &User) -> anyhow::Result<Vec<ProjectDto>> {
Ok(self Ok(self