feat: with create project

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2023-05-28 16:25:25 +02:00
parent c81a988061
commit 6dc0c24443
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
8 changed files with 92 additions and 40 deletions

View File

@ -95,7 +95,9 @@ impl AuthController {
}
}
pub struct UserFromSession {}
pub struct UserFromSession {
pub user: User,
}
#[async_trait]
impl<S> FromRequestParts<S> 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>("user") {
tracing::debug!(
@ -146,6 +148,6 @@ where
return Err((StatusCode::BAD_REQUEST, "No session found for cookie"));
};
Ok(UserFromSession {})
Ok(UserFromSession { user })
}
}

View File

@ -32,7 +32,7 @@ pub async fn graphql_handler(
req: GraphQLRequest,
) -> Result<GraphQLResponse, StatusCode> {
let req = req.into_inner();
let req = req.data(user);
let req = req.data(user.user);
Ok(schema.execute(req).await.into())
}

View File

@ -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<dyn ProjectService + Send + Sync>;
@ -11,5 +11,10 @@ pub type DynProjectService = Arc<dyn ProjectService + Send + Sync>;
#[async_trait]
pub trait ProjectService {
async fn get_project(&self, query: GetProjectQuery) -> anyhow::Result<ProjectDto>;
async fn get_projects(&self, query: GetProjectsQuery) -> anyhow::Result<Vec<ProjectDto>>;
async fn get_projects(&self, user: &User) -> anyhow::Result<Vec<ProjectDto>>;
async fn create_project(
&self,
name: CreateProjectMutation,
user: &User,
) -> anyhow::Result<ProjectDto>;
}

View File

@ -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<String>,
pub user_id: String,
}

View File

@ -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<String>,
}

View File

@ -7,8 +7,3 @@ pub struct GetProjectQuery {
pub project_id: Option<Uuid>,
pub item_id: Option<Uuid>,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject)]
pub struct GetProjectsQuery {
pub user_id: Uuid,
}

View File

@ -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<QueryRoot, MutationRoot, EmptySubscription>;
@ -33,13 +27,29 @@ impl MutationRoot {
id: created_item.id,
})
}
async fn create_project(
&self,
ctx: &Context<'_>,
request: CreateProjectMutation,
) -> anyhow::Result<ProjectDto> {
let user = ctx.data_unchecked::<User>();
let services_register = ctx.data_unchecked::<ServiceRegister>();
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<Item> {
let item = ctx
.data_unchecked::<ServiceRegister>()
@ -76,14 +86,12 @@ impl QueryRoot {
.await
}
async fn get_projects(
&self,
ctx: &Context<'_>,
query: GetProjectsQuery,
) -> anyhow::Result<Vec<ProjectDto>> {
async fn get_projects(&self, ctx: &Context<'_>) -> anyhow::Result<Vec<ProjectDto>> {
let user = ctx.data_unchecked::<User>();
ctx.data_unchecked::<ServiceRegister>()
.project_service
.get_projects(query)
.get_projects(user)
.await
}
}

View File

@ -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<ProjectDto> {
todo!()
}
async fn get_projects(&self, _query: GetProjectsQuery) -> anyhow::Result<Vec<ProjectDto>> {
async fn get_projects(&self, user: &User) -> anyhow::Result<Vec<ProjectDto>> {
todo!()
}
async fn create_project(
&self,
name: CreateProjectMutation,
user: &User,
) -> anyhow::Result<ProjectDto> {
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<Vec<ProjectDto>> {
todo!()
async fn get_projects(&self, user: &User) -> anyhow::Result<Vec<ProjectDto>> {
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<ProjectDto> {
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)
}
}