feat: with items
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
parent
12c7c8f6ee
commit
534b2e4a23
80
Cargo.lock
generated
80
Cargo.lock
generated
@ -99,6 +99,12 @@ dependencies = [
|
|||||||
"alloc-no-stdlib",
|
"alloc-no-stdlib",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "android-tzdata"
|
||||||
|
version = "0.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "android_system_properties"
|
name = "android_system_properties"
|
||||||
version = "0.1.5"
|
version = "0.1.5"
|
||||||
@ -812,14 +818,17 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "chrono"
|
name = "chrono"
|
||||||
version = "0.4.24"
|
version = "0.4.26"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4e3c5919066adf22df73762e50cffcde3a758f2a848b113b586d1f86728b673b"
|
checksum = "ec837a71355b28f6556dbd569b37b3f363091c0bd4b2e735674521b4c5fd9bc5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"android-tzdata",
|
||||||
"iana-time-zone",
|
"iana-time-zone",
|
||||||
"num-integer",
|
"js-sys",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"serde",
|
"serde",
|
||||||
|
"time 0.1.45",
|
||||||
|
"wasm-bindgen",
|
||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -835,9 +844,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap"
|
name = "clap"
|
||||||
version = "4.3.0"
|
version = "4.3.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "93aae7a4192245f70fe75dd9157fc7b4a5bf53e88d30bd4396f7d8f9284d5acc"
|
checksum = "b4ed2379f8603fa2b7509891660e802b88c70a79a6427a70abb5968054de2c28"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap_builder",
|
"clap_builder",
|
||||||
"clap_derive",
|
"clap_derive",
|
||||||
@ -846,9 +855,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap_builder"
|
name = "clap_builder"
|
||||||
version = "4.3.0"
|
version = "4.3.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4f423e341edefb78c9caba2d9c7f7687d0e72e89df3ce3394554754393ac3990"
|
checksum = "72394f3339a76daf211e57d4bcb374410f3965dcc606dd0e03738c7888766980"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anstream",
|
"anstream",
|
||||||
"anstyle",
|
"anstyle",
|
||||||
@ -859,9 +868,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap_derive"
|
name = "clap_derive"
|
||||||
version = "4.3.0"
|
version = "4.3.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "191d9573962933b4027f932c600cd252ce27a8ad5979418fe78e43c07996f27b"
|
checksum = "59e9ef9a08ee1c0e1f2e162121665ac45ac3783b0f897db7244ae75ad9a8f65b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"heck",
|
"heck",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
@ -985,10 +994,12 @@ dependencies = [
|
|||||||
"async-sqlx-session",
|
"async-sqlx-session",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"axum",
|
"axum",
|
||||||
|
"chrono",
|
||||||
"clap",
|
"clap",
|
||||||
"como_core",
|
"como_core",
|
||||||
"como_domain",
|
"como_domain",
|
||||||
"rand_core",
|
"rand_core",
|
||||||
|
"serde_json",
|
||||||
"sqlx 0.6.3",
|
"sqlx 0.6.3",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tracing",
|
"tracing",
|
||||||
@ -1030,7 +1041,7 @@ dependencies = [
|
|||||||
"rand",
|
"rand",
|
||||||
"sha2 0.10.6",
|
"sha2 0.10.6",
|
||||||
"subtle",
|
"subtle",
|
||||||
"time",
|
"time 0.3.21",
|
||||||
"version_check",
|
"version_check",
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -1047,7 +1058,7 @@ dependencies = [
|
|||||||
"rand",
|
"rand",
|
||||||
"sha2 0.10.6",
|
"sha2 0.10.6",
|
||||||
"subtle",
|
"subtle",
|
||||||
"time",
|
"time 0.3.21",
|
||||||
"version_check",
|
"version_check",
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -1619,7 +1630,7 @@ dependencies = [
|
|||||||
"cfg-if 1.0.0",
|
"cfg-if 1.0.0",
|
||||||
"js-sys",
|
"js-sys",
|
||||||
"libc",
|
"libc",
|
||||||
"wasi",
|
"wasi 0.11.0+wasi-snapshot-preview1",
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -2153,12 +2164,12 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mio"
|
name = "mio"
|
||||||
version = "0.8.7"
|
version = "0.8.8"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "eebffdb73fe72e917997fad08bdbf31ac50b0fa91cec93e69a0662e4264d454c"
|
checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"wasi",
|
"wasi 0.11.0+wasi-snapshot-preview1",
|
||||||
"windows-sys 0.48.0",
|
"windows-sys 0.48.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -2315,9 +2326,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "once_cell"
|
name = "once_cell"
|
||||||
version = "1.17.1"
|
version = "1.17.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
|
checksum = "9670a07f94779e00908f3e686eab508878ebb390ba6e604d3a284c00e8d0487b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "opaque-debug"
|
name = "opaque-debug"
|
||||||
@ -2358,9 +2369,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "openssl"
|
name = "openssl"
|
||||||
version = "0.10.52"
|
version = "0.10.54"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "01b8574602df80f7b85fdfc5392fa884a4e3b3f4f35402c070ab34c3d3f78d56"
|
checksum = "69b3f656a17a6cbc115b5c7a40c616947d213ba182135b014d6051b73ab6f019"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
"cfg-if 1.0.0",
|
"cfg-if 1.0.0",
|
||||||
@ -2390,9 +2401,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "openssl-sys"
|
name = "openssl-sys"
|
||||||
version = "0.9.87"
|
version = "0.9.88"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8e17f59264b2809d77ae94f0e1ebabc434773f370d6ca667bd223ea10e06cc7e"
|
checksum = "c2ce0f250f34a308dcfdbb351f511359857d4ed2134ba715a4eadd46e1ffd617"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cc",
|
"cc",
|
||||||
"libc",
|
"libc",
|
||||||
@ -3277,7 +3288,7 @@ dependencies = [
|
|||||||
"num-bigint",
|
"num-bigint",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"time",
|
"time 0.3.21",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -3430,6 +3441,7 @@ dependencies = [
|
|||||||
"bitflags",
|
"bitflags",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
"bytes",
|
"bytes",
|
||||||
|
"chrono",
|
||||||
"crc 3.0.1",
|
"crc 3.0.1",
|
||||||
"crossbeam-queue",
|
"crossbeam-queue",
|
||||||
"dirs",
|
"dirs",
|
||||||
@ -3465,6 +3477,7 @@ dependencies = [
|
|||||||
"sqlx-rt 0.6.3",
|
"sqlx-rt 0.6.3",
|
||||||
"stringprep",
|
"stringprep",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
|
"time 0.3.21",
|
||||||
"tokio-stream",
|
"tokio-stream",
|
||||||
"url",
|
"url",
|
||||||
"uuid",
|
"uuid",
|
||||||
@ -3635,6 +3648,17 @@ dependencies = [
|
|||||||
"once_cell",
|
"once_cell",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "time"
|
||||||
|
version = "0.1.45"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"wasi 0.10.0+wasi-snapshot-preview1",
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "time"
|
name = "time"
|
||||||
version = "0.3.21"
|
version = "0.3.21"
|
||||||
@ -4106,6 +4130,12 @@ dependencies = [
|
|||||||
"try-lock",
|
"try-lock",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasi"
|
||||||
|
version = "0.10.0+wasi-snapshot-preview1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasi"
|
name = "wasi"
|
||||||
version = "0.11.0+wasi-snapshot-preview1"
|
version = "0.11.0+wasi-snapshot-preview1"
|
||||||
@ -4432,9 +4462,9 @@ checksum = "2a0956f1ba7c7909bfb66c2e9e4124ab6f6482560f6628b5aaeba39207c9aad9"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zitadel"
|
name = "zitadel"
|
||||||
version = "3.3.2"
|
version = "3.3.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f663404099529f467b6ee708371187191bd0ab94d4821ea2357a3fe74ce2b4c8"
|
checksum = "902f5f12d22c788c97d643110ff7c7e6a906049f96946e01835f360900f4549b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"axum",
|
"axum",
|
||||||
"axum-extra",
|
"axum-extra",
|
||||||
@ -4446,7 +4476,7 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"serde_urlencoded",
|
"serde_urlencoded",
|
||||||
"time",
|
"time 0.3.21",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tonic-build",
|
"tonic-build",
|
||||||
]
|
]
|
||||||
|
@ -7,6 +7,7 @@ members = [
|
|||||||
"como_gql",
|
"como_gql",
|
||||||
"como_api",
|
"como_api",
|
||||||
]
|
]
|
||||||
|
resolver = "2"
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
como_bin = { path = "./como_bin/" }
|
como_bin = { path = "./como_bin/" }
|
||||||
@ -34,7 +35,10 @@ sqlx = { version = "0.6.2", features = [
|
|||||||
"migrate",
|
"migrate",
|
||||||
"uuid",
|
"uuid",
|
||||||
"offline",
|
"offline",
|
||||||
|
"time",
|
||||||
|
"chrono",
|
||||||
] }
|
] }
|
||||||
|
chrono = { version = "0.4.26", features = ["serde"] }
|
||||||
|
|
||||||
tokio = { version = "1.28.2", features = ["full"] }
|
tokio = { version = "1.28.2", features = ["full"] }
|
||||||
|
|
||||||
|
@ -7,6 +7,8 @@ use async_graphql_axum::{GraphQLRequest, GraphQLResponse};
|
|||||||
use axum::response::Html;
|
use axum::response::Html;
|
||||||
use axum::{http::StatusCode, response::IntoResponse, routing::get, Extension, Router};
|
use axum::{http::StatusCode, response::IntoResponse, routing::get, Extension, Router};
|
||||||
|
|
||||||
|
use como_domain::user::ContextUserExt;
|
||||||
|
use como_domain::Context;
|
||||||
use como_gql::graphql::{ComoSchema, MutationRoot, QueryRoot};
|
use como_gql::graphql::{ComoSchema, MutationRoot, QueryRoot};
|
||||||
use como_infrastructure::register::ServiceRegister;
|
use como_infrastructure::register::ServiceRegister;
|
||||||
use tower::ServiceBuilder;
|
use tower::ServiceBuilder;
|
||||||
@ -32,7 +34,11 @@ pub async fn graphql_handler(
|
|||||||
req: GraphQLRequest,
|
req: GraphQLRequest,
|
||||||
) -> Result<GraphQLResponse, StatusCode> {
|
) -> Result<GraphQLResponse, StatusCode> {
|
||||||
let req = req.into_inner();
|
let req = req.into_inner();
|
||||||
let req = req.data(user.user);
|
let req = req.data(user.user.clone());
|
||||||
|
|
||||||
|
let context = Context::new();
|
||||||
|
let context = context.set_user_id(user.user.id.clone());
|
||||||
|
let req = req.data(context);
|
||||||
|
|
||||||
Ok(schema.execute(req).await.into())
|
Ok(schema.execute(req).await.into())
|
||||||
}
|
}
|
||||||
|
@ -8,14 +8,22 @@ use como_domain::{
|
|||||||
responses::CreatedItemDto,
|
responses::CreatedItemDto,
|
||||||
ItemDto,
|
ItemDto,
|
||||||
},
|
},
|
||||||
users::User,
|
Context,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub type DynItemService = Arc<dyn ItemService + Send + Sync>;
|
pub type DynItemService = Arc<dyn ItemService + Send + Sync>;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait ItemService {
|
pub trait ItemService {
|
||||||
async fn add_item(&self, item: CreateItemDto, user: &User) -> anyhow::Result<CreatedItemDto>;
|
async fn add_item(
|
||||||
async fn get_item(&self, query: GetItemQuery, user: &User) -> anyhow::Result<ItemDto>;
|
&self,
|
||||||
async fn get_items(&self, query: GetItemsQuery, user: &User) -> anyhow::Result<Vec<ItemDto>>;
|
context: &Context,
|
||||||
|
item: CreateItemDto,
|
||||||
|
) -> anyhow::Result<CreatedItemDto>;
|
||||||
|
async fn get_item(&self, context: &Context, query: GetItemQuery) -> anyhow::Result<ItemDto>;
|
||||||
|
async fn get_items(
|
||||||
|
&self,
|
||||||
|
context: &Context,
|
||||||
|
query: GetItemsQuery,
|
||||||
|
) -> anyhow::Result<Vec<ItemDto>>;
|
||||||
}
|
}
|
||||||
|
@ -3,18 +3,22 @@ use std::sync::Arc;
|
|||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use como_domain::{
|
use como_domain::{
|
||||||
projects::{mutation::CreateProjectMutation, queries::GetProjectQuery, ProjectDto},
|
projects::{mutation::CreateProjectMutation, queries::GetProjectQuery, ProjectDto},
|
||||||
users::User,
|
Context,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub type DynProjectService = Arc<dyn ProjectService + Send + Sync>;
|
pub type DynProjectService = Arc<dyn ProjectService + Send + Sync>;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait ProjectService {
|
pub trait ProjectService {
|
||||||
async fn get_project(&self, query: GetProjectQuery) -> anyhow::Result<ProjectDto>;
|
async fn get_project(
|
||||||
async fn get_projects(&self, user: &User) -> anyhow::Result<Vec<ProjectDto>>;
|
&self,
|
||||||
|
context: &Context,
|
||||||
|
query: GetProjectQuery,
|
||||||
|
) -> anyhow::Result<ProjectDto>;
|
||||||
|
async fn get_projects(&self, context: &Context) -> anyhow::Result<Vec<ProjectDto>>;
|
||||||
async fn create_project(
|
async fn create_project(
|
||||||
&self,
|
&self,
|
||||||
|
context: &Context,
|
||||||
name: CreateProjectMutation,
|
name: CreateProjectMutation,
|
||||||
user: &User,
|
|
||||||
) -> anyhow::Result<ProjectDto>;
|
) -> anyhow::Result<ProjectDto>;
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,21 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
use como_domain::Context;
|
||||||
|
|
||||||
pub type DynUserService = Arc<dyn UserService + Send + Sync>;
|
pub type DynUserService = Arc<dyn UserService + Send + Sync>;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait UserService {
|
pub trait UserService {
|
||||||
async fn add_user(&self, username: String, password: String) -> anyhow::Result<String>;
|
async fn add_user(
|
||||||
|
&self,
|
||||||
|
context: &Context,
|
||||||
|
username: String,
|
||||||
|
password: String,
|
||||||
|
) -> anyhow::Result<String>;
|
||||||
async fn validate_user(
|
async fn validate_user(
|
||||||
&self,
|
&self,
|
||||||
|
context: &Context,
|
||||||
username: String,
|
username: String,
|
||||||
password: String,
|
password: String,
|
||||||
) -> anyhow::Result<Option<String>>;
|
) -> anyhow::Result<Option<String>>;
|
||||||
|
37
como_domain/src/common/mod.rs
Normal file
37
como_domain/src/common/mod.rs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
pub mod user;
|
||||||
|
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Context {
|
||||||
|
values: BTreeMap<String, String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Context {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
values: Default::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_value(&self, key: impl Into<String>, value: impl Into<String>) -> Self {
|
||||||
|
let mut values = self.values.clone();
|
||||||
|
|
||||||
|
let _ = values.insert(key.into(), value.into());
|
||||||
|
|
||||||
|
Self { values }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_value_mut(
|
||||||
|
&mut self,
|
||||||
|
key: impl Into<String>,
|
||||||
|
value: impl Into<String>,
|
||||||
|
) -> &mut Self {
|
||||||
|
self.values.insert(key.into(), value.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get(&self, key: impl AsRef<str>) -> Option<&str> {
|
||||||
|
self.values.get(key.as_ref()).map(|s| s.as_str())
|
||||||
|
}
|
||||||
|
}
|
23
como_domain/src/common/user.rs
Normal file
23
como_domain/src/common/user.rs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
use crate::Context;
|
||||||
|
|
||||||
|
pub trait ContextUserExt {
|
||||||
|
fn set_user_id(&self, user_id: impl Into<String>) -> Context;
|
||||||
|
fn set_user_id_mut(&mut self, user_id: impl Into<String>) -> &mut Context;
|
||||||
|
fn get_user_id(&self) -> Option<String>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const USER_ID_KEY: &str = "user_id";
|
||||||
|
|
||||||
|
impl ContextUserExt for Context {
|
||||||
|
fn set_user_id(&self, user_id: impl Into<String>) -> Context {
|
||||||
|
self.with_value(USER_ID_KEY, user_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_user_id_mut(&mut self, user_id: impl Into<String>) -> &mut Context {
|
||||||
|
self.with_value_mut(USER_ID_KEY, user_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_user_id(&self) -> Option<String> {
|
||||||
|
self.get(USER_ID_KEY).map(|s| s.to_string())
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,6 @@
|
|||||||
|
pub mod common;
|
||||||
pub mod item;
|
pub mod item;
|
||||||
pub mod projects;
|
pub mod projects;
|
||||||
pub mod users;
|
pub mod users;
|
||||||
|
|
||||||
|
pub use common::*;
|
||||||
|
@ -5,7 +5,7 @@ use como_domain::item::requests::CreateItemDto;
|
|||||||
use como_domain::projects::mutation::CreateProjectMutation;
|
use como_domain::projects::mutation::CreateProjectMutation;
|
||||||
use como_domain::projects::queries::GetProjectQuery;
|
use como_domain::projects::queries::GetProjectQuery;
|
||||||
use como_domain::projects::ProjectDto;
|
use como_domain::projects::ProjectDto;
|
||||||
use como_domain::users::User;
|
|
||||||
use como_infrastructure::register::ServiceRegister;
|
use como_infrastructure::register::ServiceRegister;
|
||||||
|
|
||||||
pub type ComoSchema = Schema<QueryRoot, MutationRoot, EmptySubscription>;
|
pub type ComoSchema = Schema<QueryRoot, MutationRoot, EmptySubscription>;
|
||||||
@ -19,11 +19,14 @@ impl MutationRoot {
|
|||||||
ctx: &Context<'_>,
|
ctx: &Context<'_>,
|
||||||
item: CreateItemDto,
|
item: CreateItemDto,
|
||||||
) -> anyhow::Result<CreatedItem> {
|
) -> anyhow::Result<CreatedItem> {
|
||||||
let user = ctx.data_unchecked::<User>();
|
let context = ctx.data_unchecked::<como_domain::Context>();
|
||||||
|
|
||||||
let services_register = ctx.data_unchecked::<ServiceRegister>();
|
let services_register = ctx.data_unchecked::<ServiceRegister>();
|
||||||
|
|
||||||
let created_item = services_register.item_service.add_item(item, user).await?;
|
let created_item = services_register
|
||||||
|
.item_service
|
||||||
|
.add_item(context, item)
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(CreatedItem {
|
Ok(CreatedItem {
|
||||||
id: created_item.id,
|
id: created_item.id,
|
||||||
@ -35,13 +38,13 @@ impl MutationRoot {
|
|||||||
ctx: &Context<'_>,
|
ctx: &Context<'_>,
|
||||||
request: CreateProjectMutation,
|
request: CreateProjectMutation,
|
||||||
) -> anyhow::Result<ProjectDto> {
|
) -> anyhow::Result<ProjectDto> {
|
||||||
let user = ctx.data_unchecked::<User>();
|
let context = ctx.data_unchecked::<como_domain::Context>();
|
||||||
|
|
||||||
let services_register = ctx.data_unchecked::<ServiceRegister>();
|
let services_register = ctx.data_unchecked::<ServiceRegister>();
|
||||||
|
|
||||||
let project = services_register
|
let project = services_register
|
||||||
.project_service
|
.project_service
|
||||||
.create_project(request, user)
|
.create_project(context, request)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(project)
|
Ok(project)
|
||||||
@ -53,12 +56,12 @@ pub struct QueryRoot;
|
|||||||
#[Object]
|
#[Object]
|
||||||
impl QueryRoot {
|
impl QueryRoot {
|
||||||
async fn get_item(&self, ctx: &Context<'_>, query: GetItemQuery) -> anyhow::Result<Item> {
|
async fn get_item(&self, ctx: &Context<'_>, query: GetItemQuery) -> anyhow::Result<Item> {
|
||||||
let user = ctx.data_unchecked::<User>();
|
let context = ctx.data_unchecked::<como_domain::Context>();
|
||||||
|
|
||||||
let item = ctx
|
let item = ctx
|
||||||
.data_unchecked::<ServiceRegister>()
|
.data_unchecked::<ServiceRegister>()
|
||||||
.item_service
|
.item_service
|
||||||
.get_item(query, user)
|
.get_item(context, query)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(Item::from(item))
|
Ok(Item::from(item))
|
||||||
@ -69,12 +72,12 @@ impl QueryRoot {
|
|||||||
ctx: &Context<'_>,
|
ctx: &Context<'_>,
|
||||||
query: GetItemsQuery,
|
query: GetItemsQuery,
|
||||||
) -> anyhow::Result<Vec<Item>> {
|
) -> anyhow::Result<Vec<Item>> {
|
||||||
let user = ctx.data_unchecked::<User>();
|
let context = ctx.data_unchecked::<como_domain::Context>();
|
||||||
|
|
||||||
let items = ctx
|
let items = ctx
|
||||||
.data_unchecked::<ServiceRegister>()
|
.data_unchecked::<ServiceRegister>()
|
||||||
.item_service
|
.item_service
|
||||||
.get_items(query, user)
|
.get_items(context, query)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(items.iter().map(|i| Item::from(i.clone())).collect())
|
Ok(items.iter().map(|i| Item::from(i.clone())).collect())
|
||||||
@ -86,18 +89,20 @@ impl QueryRoot {
|
|||||||
ctx: &Context<'_>,
|
ctx: &Context<'_>,
|
||||||
query: GetProjectQuery,
|
query: GetProjectQuery,
|
||||||
) -> anyhow::Result<ProjectDto> {
|
) -> anyhow::Result<ProjectDto> {
|
||||||
|
let context = ctx.data_unchecked::<como_domain::Context>();
|
||||||
|
|
||||||
ctx.data_unchecked::<ServiceRegister>()
|
ctx.data_unchecked::<ServiceRegister>()
|
||||||
.project_service
|
.project_service
|
||||||
.get_project(query)
|
.get_project(context, query)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_projects(&self, ctx: &Context<'_>) -> anyhow::Result<Vec<ProjectDto>> {
|
async fn get_projects(&self, ctx: &Context<'_>) -> anyhow::Result<Vec<ProjectDto>> {
|
||||||
let user = ctx.data_unchecked::<User>();
|
let context = ctx.data_unchecked::<como_domain::Context>();
|
||||||
|
|
||||||
ctx.data_unchecked::<ServiceRegister>()
|
ctx.data_unchecked::<ServiceRegister>()
|
||||||
.project_service
|
.project_service
|
||||||
.get_projects(user)
|
.get_projects(context)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ use async_graphql::{Context, Object};
|
|||||||
use como_domain::{
|
use como_domain::{
|
||||||
item::{queries::GetItemQuery, ItemDto, ItemState},
|
item::{queries::GetItemQuery, ItemDto, ItemState},
|
||||||
projects::queries::GetProjectQuery,
|
projects::queries::GetProjectQuery,
|
||||||
users::User,
|
|
||||||
};
|
};
|
||||||
use como_infrastructure::register::ServiceRegister;
|
use como_infrastructure::register::ServiceRegister;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
@ -16,12 +15,12 @@ pub struct CreatedItem {
|
|||||||
#[Object]
|
#[Object]
|
||||||
impl CreatedItem {
|
impl CreatedItem {
|
||||||
pub async fn item(&self, ctx: &Context<'_>) -> anyhow::Result<Item> {
|
pub async fn item(&self, ctx: &Context<'_>) -> anyhow::Result<Item> {
|
||||||
let user = ctx.data_unchecked::<User>();
|
let context = ctx.data_unchecked::<como_domain::Context>();
|
||||||
|
|
||||||
let item = ctx
|
let item = ctx
|
||||||
.data_unchecked::<ServiceRegister>()
|
.data_unchecked::<ServiceRegister>()
|
||||||
.item_service
|
.item_service
|
||||||
.get_item(GetItemQuery { item_id: self.id }, user)
|
.get_item(context, GetItemQuery { item_id: self.id })
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(item.into())
|
Ok(item.into())
|
||||||
@ -55,12 +54,16 @@ impl Item {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn project(&self, ctx: &Context<'_>) -> anyhow::Result<Project> {
|
pub async fn project(&self, ctx: &Context<'_>) -> anyhow::Result<Project> {
|
||||||
|
let context = ctx.data_unchecked::<como_domain::Context>();
|
||||||
let project = ctx
|
let project = ctx
|
||||||
.data_unchecked::<ServiceRegister>()
|
.data_unchecked::<ServiceRegister>()
|
||||||
.project_service
|
.project_service
|
||||||
.get_project(GetProjectQuery {
|
.get_project(
|
||||||
project_id: self.project_id,
|
context,
|
||||||
})
|
GetProjectQuery {
|
||||||
|
project_id: self.project_id,
|
||||||
|
},
|
||||||
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(project.into())
|
Ok(project.into())
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use async_graphql::{Context, Object};
|
use async_graphql::{Context, Object};
|
||||||
use como_domain::projects::ProjectDto;
|
use como_domain::projects::ProjectDto;
|
||||||
use como_domain::users::User;
|
|
||||||
use como_infrastructure::register::ServiceRegister;
|
use como_infrastructure::register::ServiceRegister;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
@ -22,16 +22,16 @@ impl Project {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn items(&self, ctx: &Context<'_>) -> anyhow::Result<Vec<Item>> {
|
async fn items(&self, ctx: &Context<'_>) -> anyhow::Result<Vec<Item>> {
|
||||||
let user = ctx.data_unchecked::<User>();
|
let context = ctx.data_unchecked::<como_domain::Context>();
|
||||||
|
|
||||||
let items = ctx
|
let items = ctx
|
||||||
.data_unchecked::<ServiceRegister>()
|
.data_unchecked::<ServiceRegister>()
|
||||||
.item_service
|
.item_service
|
||||||
.get_items(
|
.get_items(
|
||||||
|
context,
|
||||||
como_domain::item::queries::GetItemsQuery {
|
como_domain::item::queries::GetItemsQuery {
|
||||||
project_id: self.id,
|
project_id: self.id,
|
||||||
},
|
},
|
||||||
user,
|
|
||||||
)
|
)
|
||||||
.await?
|
.await?
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -15,6 +15,8 @@ async-trait.workspace = true
|
|||||||
uuid.workspace = true
|
uuid.workspace = true
|
||||||
anyhow.workspace = true
|
anyhow.workspace = true
|
||||||
sqlx.workspace = true
|
sqlx.workspace = true
|
||||||
|
chrono.workspace = true
|
||||||
|
serde_json.workspace = true
|
||||||
|
|
||||||
async-sqlx-session.workspace = true
|
async-sqlx-session.workspace = true
|
||||||
|
|
||||||
|
@ -0,0 +1,10 @@
|
|||||||
|
-- Add migration script here
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS projects (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
name varchar not null,
|
||||||
|
description varchar default null,
|
||||||
|
user_id varchar not null,
|
||||||
|
created_at timestamp not null,
|
||||||
|
updated_at timestamp not null
|
||||||
|
);
|
17
como_infrastructure/migrations/20230603152353_items.sql
Normal file
17
como_infrastructure/migrations/20230603152353_items.sql
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
-- Add migration script here
|
||||||
|
|
||||||
|
create table if not exists items (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
title varchar not null,
|
||||||
|
description varchar default null,
|
||||||
|
state integer not null,
|
||||||
|
user_id varchar not null,
|
||||||
|
project_id UUID not null,
|
||||||
|
created_at timestamp not null,
|
||||||
|
updated_at timestamp not null,
|
||||||
|
CONSTRAINT fk_project
|
||||||
|
FOREIGN KEY(project_id)
|
||||||
|
REFERENCES projects(id)
|
||||||
|
ON DELETE CASCADE
|
||||||
|
)
|
||||||
|
|
@ -0,0 +1,4 @@
|
|||||||
|
-- Add migration script here
|
||||||
|
|
||||||
|
ALTER TABLE items ALTER COLUMN state TYPE varchar(255);
|
||||||
|
|
@ -1,17 +1,27 @@
|
|||||||
|
use clap::ValueEnum;
|
||||||
|
|
||||||
#[derive(clap::Parser)]
|
#[derive(clap::Parser)]
|
||||||
pub struct AppConfig {
|
pub struct AppConfig {
|
||||||
#[clap(long, env)]
|
#[clap(long, env)]
|
||||||
pub database_url: String,
|
pub database_url: String,
|
||||||
|
#[clap(long, env, default_value = "postgres")]
|
||||||
|
pub database_type: DatabaseType,
|
||||||
#[clap(long, env)]
|
#[clap(long, env)]
|
||||||
pub rust_log: String,
|
pub rust_log: String,
|
||||||
#[clap(long, env)]
|
#[clap(long, env)]
|
||||||
pub token_secret: String,
|
pub token_secret: String,
|
||||||
#[clap(long, env)]
|
#[clap(long, env, default_value = "3001")]
|
||||||
pub api_port: u32,
|
pub api_port: u32,
|
||||||
#[clap(long, env)]
|
#[clap(long, env, default_value = "true")]
|
||||||
pub run_migrations: bool,
|
pub run_migrations: bool,
|
||||||
#[clap(long, env)]
|
#[clap(long, env, default_value = "false")]
|
||||||
pub seed: bool,
|
pub seed: bool,
|
||||||
#[clap(long, env)]
|
#[clap(long, env)]
|
||||||
pub cors_origin: String,
|
pub cors_origin: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, ValueEnum)]
|
||||||
|
pub enum DatabaseType {
|
||||||
|
Postgres,
|
||||||
|
InMemory,
|
||||||
|
}
|
||||||
|
@ -5,10 +5,11 @@ use como_core::{items::DynItemService, projects::DynProjectService, users::DynUs
|
|||||||
use tracing::log::info;
|
use tracing::log::info;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
configs::AppConfig,
|
configs::{AppConfig, DatabaseType},
|
||||||
database::ConnectionPool,
|
database::ConnectionPool,
|
||||||
services::{
|
services::{
|
||||||
item_service::MemoryItemService, project_service::MemoryProjectService,
|
item_service::{DefaultItemService, MemoryItemService},
|
||||||
|
project_service::{DefaultProjectService, MemoryProjectService},
|
||||||
user_service::DefaultUserService,
|
user_service::DefaultUserService,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@ -25,20 +26,42 @@ impl ServiceRegister {
|
|||||||
pub async fn new(pool: ConnectionPool, config: Arc<AppConfig>) -> anyhow::Result<Self> {
|
pub async fn new(pool: ConnectionPool, config: Arc<AppConfig>) -> anyhow::Result<Self> {
|
||||||
info!("creating services");
|
info!("creating services");
|
||||||
|
|
||||||
let item_service = Arc::new(MemoryItemService::new()) as DynItemService;
|
let s = match config.database_type {
|
||||||
let project_service = Arc::new(MemoryProjectService::new()) as DynProjectService;
|
DatabaseType::Postgres => {
|
||||||
let user_service = Arc::new(DefaultUserService::new(pool.clone())) as DynUserService;
|
let item_service =
|
||||||
let store = PostgresSessionStore::new(&config.database_url).await?;
|
Arc::new(DefaultItemService::new(pool.clone())) as DynItemService;
|
||||||
|
let project_service =
|
||||||
|
Arc::new(DefaultProjectService::new(pool.clone())) as DynProjectService;
|
||||||
|
let user_service =
|
||||||
|
Arc::new(DefaultUserService::new(pool.clone())) as DynUserService;
|
||||||
|
let store = PostgresSessionStore::new(&config.database_url).await?;
|
||||||
|
store.migrate().await?;
|
||||||
|
|
||||||
store.migrate().await?;
|
Self {
|
||||||
|
item_service,
|
||||||
|
user_service,
|
||||||
|
project_service,
|
||||||
|
session_store: store,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DatabaseType::InMemory => {
|
||||||
|
let item_service = Arc::new(MemoryItemService::new()) as DynItemService;
|
||||||
|
let project_service = Arc::new(MemoryProjectService::new()) as DynProjectService;
|
||||||
|
let user_service =
|
||||||
|
Arc::new(DefaultUserService::new(pool.clone())) as DynUserService;
|
||||||
|
let store = PostgresSessionStore::new(&config.database_url).await?;
|
||||||
|
store.migrate().await?;
|
||||||
|
|
||||||
|
Self {
|
||||||
|
item_service,
|
||||||
|
user_service,
|
||||||
|
project_service,
|
||||||
|
session_store: store,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
info!("services created succesfully");
|
info!("services created succesfully");
|
||||||
|
|
||||||
Ok(Self {
|
Ok(s)
|
||||||
item_service,
|
|
||||||
user_service,
|
|
||||||
project_service,
|
|
||||||
session_store: store,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,30 +12,114 @@ use como_domain::{
|
|||||||
responses::CreatedItemDto,
|
responses::CreatedItemDto,
|
||||||
ItemDto,
|
ItemDto,
|
||||||
},
|
},
|
||||||
users::User,
|
user::ContextUserExt,
|
||||||
|
Context,
|
||||||
};
|
};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
pub struct DefaultItemService {}
|
use crate::database::ConnectionPool;
|
||||||
|
|
||||||
|
pub struct DefaultItemService {
|
||||||
|
pool: ConnectionPool,
|
||||||
|
}
|
||||||
|
|
||||||
impl DefaultItemService {
|
impl DefaultItemService {
|
||||||
pub fn new() -> Self {
|
pub fn new(connection_pool: ConnectionPool) -> Self {
|
||||||
Self {}
|
Self {
|
||||||
|
pool: connection_pool,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl ItemService for DefaultItemService {
|
impl ItemService for DefaultItemService {
|
||||||
async fn add_item(&self, _item: CreateItemDto, user: &User) -> anyhow::Result<CreatedItemDto> {
|
async fn add_item(
|
||||||
todo!()
|
&self,
|
||||||
|
context: &Context,
|
||||||
|
item: CreateItemDto,
|
||||||
|
) -> anyhow::Result<CreatedItemDto> {
|
||||||
|
let state = serde_json::to_string(&como_domain::item::ItemState::Created {})?;
|
||||||
|
let user_id = context.get_user_id().ok_or(anyhow::anyhow!("no user id"))?;
|
||||||
|
|
||||||
|
let rec = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
INSERT INTO items (id, title, description, state, project_id, user_id, created_at, updated_at)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, now(), now())
|
||||||
|
RETURNING id, title, description, state, project_id
|
||||||
|
"#,
|
||||||
|
Uuid::new_v4(),
|
||||||
|
item.name,
|
||||||
|
item.description,
|
||||||
|
state,
|
||||||
|
item.project_id,
|
||||||
|
user_id,
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(CreatedItemDto {
|
||||||
|
id: rec.id,
|
||||||
|
title: rec.title,
|
||||||
|
description: rec.description,
|
||||||
|
state: como_domain::item::ItemState::Created {},
|
||||||
|
project_id: rec.project_id,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_item(&self, _query: GetItemQuery, user: &User) -> anyhow::Result<ItemDto> {
|
async fn get_item(&self, context: &Context, query: GetItemQuery) -> anyhow::Result<ItemDto> {
|
||||||
todo!()
|
let user_id = context.get_user_id().ok_or(anyhow::anyhow!("no user id"))?;
|
||||||
|
|
||||||
|
let rec = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
SELECT id, title, description, state, project_id
|
||||||
|
FROM items
|
||||||
|
WHERE id = $1 AND user_id = $2
|
||||||
|
"#,
|
||||||
|
query.item_id,
|
||||||
|
user_id,
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(ItemDto {
|
||||||
|
id: rec.id,
|
||||||
|
title: rec.title,
|
||||||
|
description: rec.description,
|
||||||
|
state: serde_json::from_str(&rec.state)?,
|
||||||
|
project_id: rec.project_id,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_items(&self, _query: GetItemsQuery, user: &User) -> anyhow::Result<Vec<ItemDto>> {
|
async fn get_items(
|
||||||
todo!()
|
&self,
|
||||||
|
context: &Context,
|
||||||
|
query: GetItemsQuery,
|
||||||
|
) -> anyhow::Result<Vec<ItemDto>> {
|
||||||
|
let user_id = context.get_user_id().ok_or(anyhow::anyhow!("no user id"))?;
|
||||||
|
|
||||||
|
let recs = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
SELECT id, title, description, state, project_id
|
||||||
|
FROM items
|
||||||
|
WHERE user_id = $1 and project_id = $2
|
||||||
|
LIMIT 500
|
||||||
|
"#,
|
||||||
|
user_id,
|
||||||
|
query.project_id,
|
||||||
|
)
|
||||||
|
.fetch_all(&self.pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(recs
|
||||||
|
.into_iter()
|
||||||
|
.map(|rec| ItemDto {
|
||||||
|
id: rec.id,
|
||||||
|
title: rec.title,
|
||||||
|
description: rec.description,
|
||||||
|
state: serde_json::from_str(&rec.state).unwrap(),
|
||||||
|
project_id: rec.project_id,
|
||||||
|
})
|
||||||
|
.collect())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,8 +139,8 @@ impl MemoryItemService {
|
|||||||
impl ItemService for MemoryItemService {
|
impl ItemService for MemoryItemService {
|
||||||
async fn add_item(
|
async fn add_item(
|
||||||
&self,
|
&self,
|
||||||
|
_context: &Context,
|
||||||
create_item: CreateItemDto,
|
create_item: CreateItemDto,
|
||||||
user: &User,
|
|
||||||
) -> anyhow::Result<CreatedItemDto> {
|
) -> anyhow::Result<CreatedItemDto> {
|
||||||
if let Ok(mut item_store) = self.item_store.lock() {
|
if let Ok(mut item_store) = self.item_store.lock() {
|
||||||
let item = ItemDto {
|
let item = ItemDto {
|
||||||
@ -75,7 +159,7 @@ impl ItemService for MemoryItemService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_item(&self, query: GetItemQuery, user: &User) -> anyhow::Result<ItemDto> {
|
async fn get_item(&self, _context: &Context, query: GetItemQuery) -> anyhow::Result<ItemDto> {
|
||||||
if let Ok(item_store) = self.item_store.lock() {
|
if let Ok(item_store) = self.item_store.lock() {
|
||||||
let item = item_store
|
let item = item_store
|
||||||
.get(&query.item_id.to_string())
|
.get(&query.item_id.to_string())
|
||||||
@ -86,7 +170,11 @@ impl ItemService for MemoryItemService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_items(&self, _query: GetItemsQuery, user: &User) -> anyhow::Result<Vec<ItemDto>> {
|
async fn get_items(
|
||||||
|
&self,
|
||||||
|
_context: &Context,
|
||||||
|
_query: GetItemsQuery,
|
||||||
|
) -> anyhow::Result<Vec<ItemDto>> {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,32 +4,107 @@ use axum::async_trait;
|
|||||||
use como_core::projects::ProjectService;
|
use como_core::projects::ProjectService;
|
||||||
use como_domain::{
|
use como_domain::{
|
||||||
projects::{mutation::CreateProjectMutation, queries::GetProjectQuery, ProjectDto},
|
projects::{mutation::CreateProjectMutation, queries::GetProjectQuery, ProjectDto},
|
||||||
users::User,
|
user::ContextUserExt,
|
||||||
|
Context,
|
||||||
};
|
};
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
pub struct DefaultProjectService {}
|
use crate::database::ConnectionPool;
|
||||||
|
|
||||||
|
pub struct DefaultProjectService {
|
||||||
|
pool: ConnectionPool,
|
||||||
|
}
|
||||||
|
|
||||||
impl DefaultProjectService {
|
impl DefaultProjectService {
|
||||||
pub fn new() -> Self {
|
pub fn new(connection_pool: ConnectionPool) -> Self {
|
||||||
Self {}
|
Self {
|
||||||
|
pool: connection_pool,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl ProjectService for DefaultProjectService {
|
impl ProjectService for DefaultProjectService {
|
||||||
async fn get_project(&self, _query: GetProjectQuery) -> anyhow::Result<ProjectDto> {
|
async fn get_project(
|
||||||
todo!()
|
&self,
|
||||||
|
context: &Context,
|
||||||
|
query: GetProjectQuery,
|
||||||
|
) -> anyhow::Result<ProjectDto> {
|
||||||
|
let user_id = context.get_user_id().ok_or(anyhow::anyhow!("no user id"))?;
|
||||||
|
|
||||||
|
let rec = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
SELECT id, name, description, user_id
|
||||||
|
FROM projects
|
||||||
|
WHERE id = $1 and user_id = $2
|
||||||
|
"#,
|
||||||
|
query.project_id,
|
||||||
|
&user_id
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(ProjectDto {
|
||||||
|
id: rec.id,
|
||||||
|
name: rec.name,
|
||||||
|
description: rec.description,
|
||||||
|
user_id: rec.user_id,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
async fn get_projects(&self, user: &User) -> anyhow::Result<Vec<ProjectDto>> {
|
async fn get_projects(&self, context: &Context) -> anyhow::Result<Vec<ProjectDto>> {
|
||||||
todo!()
|
let user_id = context.get_user_id().ok_or(anyhow::anyhow!("no user id"))?;
|
||||||
|
|
||||||
|
let recs = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
SELECT id, name, description, user_id
|
||||||
|
FROM projects
|
||||||
|
WHERE user_id = $1
|
||||||
|
LIMIT 500
|
||||||
|
"#,
|
||||||
|
&user_id
|
||||||
|
)
|
||||||
|
.fetch_all(&self.pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(recs
|
||||||
|
.into_iter()
|
||||||
|
.map(|rec| ProjectDto {
|
||||||
|
id: rec.id,
|
||||||
|
name: rec.name,
|
||||||
|
description: rec.description,
|
||||||
|
user_id: rec.user_id,
|
||||||
|
})
|
||||||
|
.collect::<_>())
|
||||||
}
|
}
|
||||||
async fn create_project(
|
async fn create_project(
|
||||||
&self,
|
&self,
|
||||||
name: CreateProjectMutation,
|
context: &Context,
|
||||||
user: &User,
|
request: CreateProjectMutation,
|
||||||
) -> anyhow::Result<ProjectDto> {
|
) -> anyhow::Result<ProjectDto> {
|
||||||
todo!()
|
let user_id = context.get_user_id().ok_or(anyhow::anyhow!("no user id"))?;
|
||||||
|
|
||||||
|
let rec = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
INSERT INTO projects (id, name, description, user_id, created_at, updated_at)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6)
|
||||||
|
RETURNING id
|
||||||
|
"#,
|
||||||
|
uuid::Uuid::new_v4(),
|
||||||
|
request.name,
|
||||||
|
request.description,
|
||||||
|
&user_id,
|
||||||
|
chrono::Utc::now().naive_utc(),
|
||||||
|
chrono::Utc::now().naive_utc(),
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(ProjectDto {
|
||||||
|
id: rec.id,
|
||||||
|
name: request.name,
|
||||||
|
description: request.description,
|
||||||
|
user_id: user_id.clone(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,35 +122,43 @@ impl MemoryProjectService {
|
|||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl ProjectService for MemoryProjectService {
|
impl ProjectService for MemoryProjectService {
|
||||||
async fn get_project(&self, query: GetProjectQuery) -> anyhow::Result<ProjectDto> {
|
async fn get_project(
|
||||||
|
&self,
|
||||||
|
_context: &Context,
|
||||||
|
query: GetProjectQuery,
|
||||||
|
) -> anyhow::Result<ProjectDto> {
|
||||||
let ps = self.project_store.lock().await;
|
let ps = self.project_store.lock().await;
|
||||||
Ok(ps
|
Ok(ps
|
||||||
.get(&query.project_id.to_string())
|
.get(&query.project_id.to_string())
|
||||||
.ok_or(anyhow::anyhow!("could not find project"))?
|
.ok_or(anyhow::anyhow!("could not find project"))?
|
||||||
.clone())
|
.clone())
|
||||||
}
|
}
|
||||||
async fn get_projects(&self, user: &User) -> anyhow::Result<Vec<ProjectDto>> {
|
async fn get_projects(&self, context: &Context) -> anyhow::Result<Vec<ProjectDto>> {
|
||||||
|
let user_id = context.get_user_id().ok_or(anyhow::anyhow!("no user id"))?;
|
||||||
|
|
||||||
Ok(self
|
Ok(self
|
||||||
.project_store
|
.project_store
|
||||||
.lock()
|
.lock()
|
||||||
.await
|
.await
|
||||||
.values()
|
.values()
|
||||||
.filter(|p| p.user_id == user.id)
|
.filter(|p| p.user_id == user_id)
|
||||||
.cloned()
|
.cloned()
|
||||||
.collect::<_>())
|
.collect::<_>())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn create_project(
|
async fn create_project(
|
||||||
&self,
|
&self,
|
||||||
|
context: &Context,
|
||||||
mutation: CreateProjectMutation,
|
mutation: CreateProjectMutation,
|
||||||
user: &User,
|
|
||||||
) -> anyhow::Result<ProjectDto> {
|
) -> anyhow::Result<ProjectDto> {
|
||||||
|
let user_id = context.get_user_id().ok_or(anyhow::anyhow!("no user id"))?;
|
||||||
|
|
||||||
let mut ps = self.project_store.lock().await;
|
let mut ps = self.project_store.lock().await;
|
||||||
let project = ProjectDto {
|
let project = ProjectDto {
|
||||||
id: uuid::Uuid::new_v4(),
|
id: uuid::Uuid::new_v4(),
|
||||||
name: mutation.name,
|
name: mutation.name,
|
||||||
description: None,
|
description: None,
|
||||||
user_id: user.id.clone(),
|
user_id,
|
||||||
};
|
};
|
||||||
|
|
||||||
ps.insert(project.id.to_string(), project.clone());
|
ps.insert(project.id.to_string(), project.clone());
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use argon2::{password_hash::SaltString, Argon2, PasswordHash, PasswordHasher, PasswordVerifier};
|
use argon2::{password_hash::SaltString, Argon2, PasswordHash, PasswordHasher, PasswordVerifier};
|
||||||
use axum::async_trait;
|
use axum::async_trait;
|
||||||
use como_core::users::UserService;
|
use como_core::users::UserService;
|
||||||
|
use como_domain::Context;
|
||||||
use rand_core::OsRng;
|
use rand_core::OsRng;
|
||||||
|
|
||||||
use crate::database::ConnectionPool;
|
use crate::database::ConnectionPool;
|
||||||
@ -14,7 +15,7 @@ impl DefaultUserService {
|
|||||||
Self { pool }
|
Self { pool }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hash_password(&self, password: String) -> anyhow::Result<String> {
|
fn hash_password(&self, _context: &Context, password: String) -> anyhow::Result<String> {
|
||||||
let salt = SaltString::generate(&mut OsRng);
|
let salt = SaltString::generate(&mut OsRng);
|
||||||
let argon2 = Argon2::default();
|
let argon2 = Argon2::default();
|
||||||
|
|
||||||
@ -26,7 +27,12 @@ impl DefaultUserService {
|
|||||||
Ok(password_hash)
|
Ok(password_hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate_password(&self, password: String, hashed_password: String) -> anyhow::Result<bool> {
|
fn validate_password(
|
||||||
|
&self,
|
||||||
|
_context: &Context,
|
||||||
|
password: String,
|
||||||
|
hashed_password: String,
|
||||||
|
) -> anyhow::Result<bool> {
|
||||||
let argon2 = Argon2::default();
|
let argon2 = Argon2::default();
|
||||||
|
|
||||||
let parsed_hash = PasswordHash::new(&hashed_password).map_err(|e| anyhow::anyhow!(e))?;
|
let parsed_hash = PasswordHash::new(&hashed_password).map_err(|e| anyhow::anyhow!(e))?;
|
||||||
@ -39,8 +45,13 @@ impl DefaultUserService {
|
|||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl UserService for DefaultUserService {
|
impl UserService for DefaultUserService {
|
||||||
async fn add_user(&self, username: String, password: String) -> anyhow::Result<String> {
|
async fn add_user(
|
||||||
let hashed_password = self.hash_password(password)?;
|
&self,
|
||||||
|
context: &Context,
|
||||||
|
username: String,
|
||||||
|
password: String,
|
||||||
|
) -> anyhow::Result<String> {
|
||||||
|
let hashed_password = self.hash_password(context, password)?;
|
||||||
|
|
||||||
let rec = sqlx::query!(
|
let rec = sqlx::query!(
|
||||||
r#"
|
r#"
|
||||||
@ -59,6 +70,7 @@ impl UserService for DefaultUserService {
|
|||||||
|
|
||||||
async fn validate_user(
|
async fn validate_user(
|
||||||
&self,
|
&self,
|
||||||
|
context: &Context,
|
||||||
username: String,
|
username: String,
|
||||||
password: String,
|
password: String,
|
||||||
) -> anyhow::Result<Option<String>> {
|
) -> anyhow::Result<Option<String>> {
|
||||||
@ -73,7 +85,7 @@ impl UserService for DefaultUserService {
|
|||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
match rec {
|
match rec {
|
||||||
Some(user) => match self.validate_password(password, user.password_hash)? {
|
Some(user) => match self.validate_password(context, password, user.password_hash)? {
|
||||||
true => Ok(Some(user.id.to_string())),
|
true => Ok(Some(user.id.to_string())),
|
||||||
false => Ok(None),
|
false => Ok(None),
|
||||||
},
|
},
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"query": "\n SELECT * from users\n where username=$1\n ",
|
"query": "\n SELECT id, title, description, state, project_id\n FROM items\n WHERE user_id = $1 and project_id = $2\n LIMIT 500\n ",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [
|
"columns": [
|
||||||
{
|
{
|
||||||
@ -9,25 +9,38 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ordinal": 1,
|
"ordinal": 1,
|
||||||
"name": "username",
|
"name": "title",
|
||||||
"type_info": "Varchar"
|
"type_info": "Varchar"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ordinal": 2,
|
"ordinal": 2,
|
||||||
"name": "password_hash",
|
"name": "description",
|
||||||
"type_info": "Varchar"
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 3,
|
||||||
|
"name": "state",
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 4,
|
||||||
|
"name": "project_id",
|
||||||
|
"type_info": "Uuid"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"Left": [
|
"Left": [
|
||||||
"Text"
|
"Text",
|
||||||
|
"Uuid"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"nullable": [
|
"nullable": [
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
false
|
false
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"hash": "d3f222cf6c3d9816705426fdbed3b13cb575bb432eb1f33676c0b414e67aecaf"
|
"hash": "bd2407ffb9637afcff3ffe1101e7c1920b8cf0be423ab0313d14acc9c76e0f93"
|
||||||
}
|
}
|
@ -17,3 +17,10 @@ scripts:
|
|||||||
type: shell
|
type: shell
|
||||||
migrate_como:
|
migrate_como:
|
||||||
type: shell
|
type: shell
|
||||||
|
new_migration:
|
||||||
|
type: shell
|
||||||
|
args:
|
||||||
|
name:
|
||||||
|
type: "env"
|
||||||
|
key: "name"
|
||||||
|
|
||||||
|
2
rust-toolchain.toml
Normal file
2
rust-toolchain.toml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
[toolchain]
|
||||||
|
channel = "nightly"
|
5
scripts/new_migration.sh
Executable file
5
scripts/new_migration.sh
Executable file
@ -0,0 +1,5 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
export $(cat .env | xargs)
|
||||||
|
|
||||||
|
cargo sqlx migrate add --source como_infrastructure/migrations $name
|
Loading…
Reference in New Issue
Block a user