From 6dc0c24443a7bb7605a45af7b43a195f175c9881 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sun, 28 May 2023 16:25:25 +0200 Subject: [PATCH] feat: with create project Signed-off-by: kjuulh --- como_api/src/controllers/auth.rs | 8 +-- como_api/src/controllers/graphql.rs | 2 +- como_core/src/projects/mod.rs | 13 +++-- como_domain/src/projects/mod.rs | 3 ++ como_domain/src/projects/mutation.rs | 8 +++ como_domain/src/projects/queries.rs | 5 -- como_gql/src/graphql.rs | 50 +++++++++++-------- .../src/services/project_service.rs | 43 +++++++++++++--- 8 files changed, 92 insertions(+), 40 deletions(-) create mode 100644 como_domain/src/projects/mutation.rs diff --git a/como_api/src/controllers/auth.rs b/como_api/src/controllers/auth.rs index 662d7af..1d22fe8 100644 --- a/como_api/src/controllers/auth.rs +++ b/como_api/src/controllers/auth.rs @@ -95,7 +95,9 @@ impl AuthController { } } -pub struct UserFromSession {} +pub struct UserFromSession { + pub user: User, +} #[async_trait] impl FromRequestParts for UserFromSession @@ -123,7 +125,7 @@ where session_cookie ); // continue to decode the session cookie - let _user = + let user = if let Some(session) = store.load_session(session_cookie.to_owned()).await.unwrap() { if let Some(user) = session.get::("user") { tracing::debug!( @@ -146,6 +148,6 @@ where return Err((StatusCode::BAD_REQUEST, "No session found for cookie")); }; - Ok(UserFromSession {}) + Ok(UserFromSession { user }) } } diff --git a/como_api/src/controllers/graphql.rs b/como_api/src/controllers/graphql.rs index e790d1a..a6c441b 100644 --- a/como_api/src/controllers/graphql.rs +++ b/como_api/src/controllers/graphql.rs @@ -32,7 +32,7 @@ pub async fn graphql_handler( req: GraphQLRequest, ) -> Result { let req = req.into_inner(); - let req = req.data(user); + let req = req.data(user.user); Ok(schema.execute(req).await.into()) } diff --git a/como_core/src/projects/mod.rs b/como_core/src/projects/mod.rs index 4c7b737..3d128bd 100644 --- a/como_core/src/projects/mod.rs +++ b/como_core/src/projects/mod.rs @@ -1,9 +1,9 @@ use std::sync::Arc; use async_trait::async_trait; -use como_domain::projects::{ - queries::{GetProjectQuery, GetProjectsQuery}, - ProjectDto, +use como_domain::{ + projects::{mutation::CreateProjectMutation, queries::GetProjectQuery, ProjectDto}, + users::User, }; pub type DynProjectService = Arc; @@ -11,5 +11,10 @@ pub type DynProjectService = Arc; #[async_trait] pub trait ProjectService { async fn get_project(&self, query: GetProjectQuery) -> anyhow::Result; - async fn get_projects(&self, query: GetProjectsQuery) -> anyhow::Result>; + async fn get_projects(&self, user: &User) -> anyhow::Result>; + async fn create_project( + &self, + name: CreateProjectMutation, + user: &User, + ) -> anyhow::Result; } diff --git a/como_domain/src/projects/mod.rs b/como_domain/src/projects/mod.rs index 23692a7..c648179 100644 --- a/como_domain/src/projects/mod.rs +++ b/como_domain/src/projects/mod.rs @@ -1,3 +1,4 @@ +pub mod mutation; pub mod queries; pub mod requests; pub mod responses; @@ -11,4 +12,6 @@ pub struct ProjectDto { pub id: Uuid, pub name: String, pub description: Option, + + pub user_id: String, } diff --git a/como_domain/src/projects/mutation.rs b/como_domain/src/projects/mutation.rs new file mode 100644 index 0000000..65256fa --- /dev/null +++ b/como_domain/src/projects/mutation.rs @@ -0,0 +1,8 @@ +use async_graphql::InputObject; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject)] +pub struct CreateProjectMutation { + pub name: String, + pub description: Option, +} diff --git a/como_domain/src/projects/queries.rs b/como_domain/src/projects/queries.rs index 52e111f..7a67da5 100644 --- a/como_domain/src/projects/queries.rs +++ b/como_domain/src/projects/queries.rs @@ -7,8 +7,3 @@ pub struct GetProjectQuery { pub project_id: Option, pub item_id: Option, } - -#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject)] -pub struct GetProjectsQuery { - pub user_id: Uuid, -} diff --git a/como_gql/src/graphql.rs b/como_gql/src/graphql.rs index e039929..7c8384e 100644 --- a/como_gql/src/graphql.rs +++ b/como_gql/src/graphql.rs @@ -1,18 +1,12 @@ -use async_graphql::{Context, EmptySubscription, Object, Schema}; - -use como_domain::{ - item::{ - queries::{GetItemQuery, GetItemsQuery}, - requests::CreateItemDto, - }, - projects::{ - queries::{GetProjectQuery, GetProjectsQuery}, - ProjectDto, - }, -}; -use como_infrastructure::register::ServiceRegister; - use crate::items::{CreatedItem, Item}; +use async_graphql::{Context, EmptySubscription, Object, Schema}; +use como_domain::item::queries::{GetItemQuery, GetItemsQuery}; +use como_domain::item::requests::CreateItemDto; +use como_domain::projects::mutation::CreateProjectMutation; +use como_domain::projects::queries::GetProjectQuery; +use como_domain::projects::ProjectDto; +use como_domain::users::User; +use como_infrastructure::register::ServiceRegister; pub type ComoSchema = Schema; @@ -33,13 +27,29 @@ impl MutationRoot { id: created_item.id, }) } + + async fn create_project( + &self, + ctx: &Context<'_>, + request: CreateProjectMutation, + ) -> anyhow::Result { + let user = ctx.data_unchecked::(); + + let services_register = ctx.data_unchecked::(); + + let project = services_register + .project_service + .create_project(request, user) + .await?; + + Ok(project) + } } pub struct QueryRoot; #[Object] impl QueryRoot { - // Items async fn get_item(&self, ctx: &Context<'_>, query: GetItemQuery) -> anyhow::Result { let item = ctx .data_unchecked::() @@ -76,14 +86,12 @@ impl QueryRoot { .await } - async fn get_projects( - &self, - ctx: &Context<'_>, - query: GetProjectsQuery, - ) -> anyhow::Result> { + async fn get_projects(&self, ctx: &Context<'_>) -> anyhow::Result> { + let user = ctx.data_unchecked::(); + ctx.data_unchecked::() .project_service - .get_projects(query) + .get_projects(user) .await } } diff --git a/como_infrastructure/src/services/project_service.rs b/como_infrastructure/src/services/project_service.rs index 02f9004..845a4df 100644 --- a/como_infrastructure/src/services/project_service.rs +++ b/como_infrastructure/src/services/project_service.rs @@ -2,9 +2,9 @@ use std::{collections::HashMap, sync::Arc}; use axum::async_trait; use como_core::projects::ProjectService; -use como_domain::projects::{ - queries::{GetProjectQuery, GetProjectsQuery}, - ProjectDto, +use como_domain::{ + projects::{mutation::CreateProjectMutation, queries::GetProjectQuery, ProjectDto}, + users::User, }; use tokio::sync::Mutex; @@ -21,7 +21,14 @@ impl ProjectService for DefaultProjectService { async fn get_project(&self, _query: GetProjectQuery) -> anyhow::Result { todo!() } - async fn get_projects(&self, _query: GetProjectsQuery) -> anyhow::Result> { + async fn get_projects(&self, user: &User) -> anyhow::Result> { + todo!() + } + async fn create_project( + &self, + name: CreateProjectMutation, + user: &User, + ) -> anyhow::Result { todo!() } } @@ -51,7 +58,31 @@ impl ProjectService for MemoryProjectService { Err(anyhow::anyhow!("could not find project")) } } - async fn get_projects(&self, _query: GetProjectsQuery) -> anyhow::Result> { - todo!() + async fn get_projects(&self, user: &User) -> anyhow::Result> { + Ok(self + .project_store + .lock() + .await + .values() + .filter(|p| p.user_id == user.id) + .cloned() + .collect::<_>()) + } + + async fn create_project( + &self, + mutation: CreateProjectMutation, + user: &User, + ) -> anyhow::Result { + let mut ps = self.project_store.lock().await; + let project = ProjectDto { + id: uuid::Uuid::new_v4(), + name: mutation.name, + description: None, + user_id: user.id.clone(), + }; + + ps.insert(project.id.to_string(), project.clone()); + Ok(project) } }