2023-06-04 14:42:38 +02:00
|
|
|
use crate::common::*;
|
2023-05-28 16:25:25 +02:00
|
|
|
use crate::items::{CreatedItem, Item};
|
2022-10-04 11:07:14 +02:00
|
|
|
use async_graphql::{Context, EmptySubscription, Object, Schema};
|
2023-05-28 16:25:25 +02:00
|
|
|
use como_domain::item::queries::{GetItemQuery, GetItemsQuery};
|
2023-06-04 14:42:38 +02:00
|
|
|
use como_domain::item::requests::{CreateItemDto, UpdateItemDto};
|
2023-05-28 16:25:25 +02:00
|
|
|
use como_domain::projects::mutation::CreateProjectMutation;
|
|
|
|
use como_domain::projects::queries::GetProjectQuery;
|
|
|
|
use como_domain::projects::ProjectDto;
|
2023-06-04 11:02:51 +02:00
|
|
|
|
2022-10-04 12:06:00 +02:00
|
|
|
pub type ComoSchema = Schema<QueryRoot, MutationRoot, EmptySubscription>;
|
2022-10-02 14:15:45 +02:00
|
|
|
|
|
|
|
pub struct MutationRoot;
|
|
|
|
|
|
|
|
#[Object]
|
|
|
|
impl MutationRoot {
|
2022-10-03 23:00:31 +02:00
|
|
|
async fn create_item(
|
|
|
|
&self,
|
|
|
|
ctx: &Context<'_>,
|
|
|
|
item: CreateItemDto,
|
2022-10-04 22:39:57 +02:00
|
|
|
) -> anyhow::Result<CreatedItem> {
|
2023-06-04 14:42:38 +02:00
|
|
|
let created_item = item_service(ctx)
|
|
|
|
.add_item(get_domain_context(ctx), item)
|
2023-06-04 11:02:51 +02:00
|
|
|
.await?;
|
2022-10-04 11:06:48 +02:00
|
|
|
|
2022-10-04 22:39:57 +02:00
|
|
|
Ok(CreatedItem {
|
|
|
|
id: created_item.id,
|
|
|
|
})
|
2022-10-03 23:00:31 +02:00
|
|
|
}
|
2023-05-28 16:25:25 +02:00
|
|
|
|
|
|
|
async fn create_project(
|
|
|
|
&self,
|
|
|
|
ctx: &Context<'_>,
|
|
|
|
request: CreateProjectMutation,
|
|
|
|
) -> anyhow::Result<ProjectDto> {
|
2023-06-04 14:42:38 +02:00
|
|
|
let project = project_service(ctx)
|
|
|
|
.create_project(get_domain_context(ctx), request)
|
|
|
|
.await?;
|
2023-05-28 16:25:25 +02:00
|
|
|
|
2023-06-04 14:42:38 +02:00
|
|
|
Ok(project)
|
|
|
|
}
|
2023-05-28 16:25:25 +02:00
|
|
|
|
2023-06-04 14:42:38 +02:00
|
|
|
async fn update_item(&self, ctx: &Context<'_>, item: UpdateItemDto) -> anyhow::Result<Item> {
|
|
|
|
let updated_item = item_service(ctx)
|
|
|
|
.update_item(get_domain_context(ctx), item)
|
2023-05-28 16:25:25 +02:00
|
|
|
.await?;
|
|
|
|
|
2023-06-04 14:42:38 +02:00
|
|
|
Ok(updated_item.into())
|
2023-05-28 16:25:25 +02:00
|
|
|
}
|
2022-10-02 14:15:45 +02:00
|
|
|
}
|
2022-10-02 12:12:08 +02:00
|
|
|
|
|
|
|
pub struct QueryRoot;
|
|
|
|
|
|
|
|
#[Object]
|
|
|
|
impl QueryRoot {
|
2022-10-04 22:39:57 +02:00
|
|
|
async fn get_item(&self, ctx: &Context<'_>, query: GetItemQuery) -> anyhow::Result<Item> {
|
2023-06-04 14:42:38 +02:00
|
|
|
let item = item_service(ctx)
|
|
|
|
.get_item(get_domain_context(ctx), query)
|
2022-10-04 22:39:57 +02:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(Item::from(item))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_items(
|
|
|
|
&self,
|
|
|
|
ctx: &Context<'_>,
|
|
|
|
query: GetItemsQuery,
|
|
|
|
) -> anyhow::Result<Vec<Item>> {
|
2023-06-04 14:42:38 +02:00
|
|
|
let items = item_service(ctx)
|
|
|
|
.get_items(get_domain_context(ctx), query)
|
2022-10-04 22:39:57 +02:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(items.iter().map(|i| Item::from(i.clone())).collect())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Projects
|
|
|
|
async fn get_project(
|
|
|
|
&self,
|
|
|
|
ctx: &Context<'_>,
|
|
|
|
query: GetProjectQuery,
|
|
|
|
) -> anyhow::Result<ProjectDto> {
|
2023-06-04 14:42:38 +02:00
|
|
|
project_service(ctx)
|
|
|
|
.get_project(get_domain_context(ctx), query)
|
2022-10-04 22:39:57 +02:00
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2023-05-28 16:25:25 +02:00
|
|
|
async fn get_projects(&self, ctx: &Context<'_>) -> anyhow::Result<Vec<ProjectDto>> {
|
2023-06-04 14:42:38 +02:00
|
|
|
project_service(ctx)
|
|
|
|
.get_projects(get_domain_context(ctx))
|
2022-10-04 22:39:57 +02:00
|
|
|
.await
|
2022-10-04 11:06:48 +02:00
|
|
|
}
|
2022-10-02 12:12:08 +02:00
|
|
|
}
|