use crate::common::*; use crate::items::{CreatedItem, Item}; use crate::projects::Project; use async_graphql::{Context, EmptySubscription, Object, Schema}; use como_domain::item::queries::{GetItemQuery, GetItemsQuery}; use como_domain::item::requests::{CreateItemDto, UpdateItemDto}; use como_domain::projects::mutation::CreateProjectMutation; use como_domain::projects::queries::GetProjectQuery; use como_domain::projects::ProjectDto; pub type ComoSchema = Schema; pub struct MutationRoot; #[Object] impl MutationRoot { async fn create_item( &self, ctx: &Context<'_>, item: CreateItemDto, ) -> anyhow::Result { let created_item = item_service(ctx) .add_item(get_domain_context(ctx), item) .await?; Ok(CreatedItem { id: created_item.id, }) } async fn create_project( &self, ctx: &Context<'_>, request: CreateProjectMutation, ) -> anyhow::Result { let project = project_service(ctx) .create_project(get_domain_context(ctx), request) .await?; Ok(project) } async fn update_item(&self, ctx: &Context<'_>, item: UpdateItemDto) -> anyhow::Result { let updated_item = item_service(ctx) .update_item(get_domain_context(ctx), item) .await?; Ok(updated_item.into()) } } pub struct QueryRoot; #[Object] impl QueryRoot { async fn get_item(&self, ctx: &Context<'_>, query: GetItemQuery) -> anyhow::Result { let item = item_service(ctx) .get_item(get_domain_context(ctx), query) .await?; Ok(Item::from(item)) } async fn get_items( &self, ctx: &Context<'_>, query: GetItemsQuery, ) -> anyhow::Result> { let items = item_service(ctx) .get_items(get_domain_context(ctx), query) .await?; Ok(items.iter().map(|i| Item::from(i.clone())).collect()) } // Projects async fn get_project( &self, ctx: &Context<'_>, query: GetProjectQuery, ) -> anyhow::Result { project_service(ctx) .get_project(get_domain_context(ctx), query) .await .map(|p| p.into()) } async fn get_projects(&self, ctx: &Context<'_>) -> anyhow::Result> { project_service(ctx) .get_projects(get_domain_context(ctx)) .await .map(|p| p.into_iter().map(|p| p.into()).collect()) } }