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 async_trait::async_trait;
use como_domain::item::{
queries::{GetItemQuery, GetItemsQuery},
requests::CreateItemDto,
responses::CreatedItemDto,
ItemDto,
use como_domain::{
item::{
queries::{GetItemQuery, GetItemsQuery},
requests::CreateItemDto,
responses::CreatedItemDto,
ItemDto,
},
users::User,
};
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>>;
async fn add_item(&self, item: CreateItemDto, user: &User) -> anyhow::Result<CreatedItemDto>;
async fn get_item(&self, query: GetItemQuery, user: &User) -> anyhow::Result<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 description: Option<String>,
pub state: ItemState,
pub project_id: Uuid,
}

View File

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

View File

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

View File

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

View File

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

View File

@ -5,11 +5,14 @@ use std::{
use async_trait::async_trait;
use como_core::items::ItemService;
use como_domain::item::{
queries::{GetItemQuery, GetItemsQuery},
requests::CreateItemDto,
responses::CreatedItemDto,
ItemDto,
use como_domain::{
item::{
queries::{GetItemQuery, GetItemsQuery},
requests::CreateItemDto,
responses::CreatedItemDto,
ItemDto,
},
users::User,
};
use uuid::Uuid;
@ -23,15 +26,15 @@ impl DefaultItemService {
#[async_trait]
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!()
}
async fn get_item(&self, _query: GetItemQuery) -> anyhow::Result<ItemDto> {
async fn get_item(&self, _query: GetItemQuery, user: &User) -> anyhow::Result<ItemDto> {
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!()
}
}
@ -50,13 +53,18 @@ impl MemoryItemService {
#[async_trait]
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() {
let item = ItemDto {
id: Uuid::new_v4(),
title: create_item.name,
description: None,
description: create_item.description,
state: como_domain::item::ItemState::Created,
project_id: create_item.project_id,
};
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() {
let item = item_store
.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!()
}
}

View File

@ -49,14 +49,10 @@ impl MemoryProjectService {
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"))
}
Ok(ps
.get(&query.project_id.to_string())
.ok_or(anyhow::anyhow!("could not find project"))?
.clone())
}
async fn get_projects(&self, user: &User) -> anyhow::Result<Vec<ProjectDto>> {
Ok(self