From f8541bf13775d9848e264dde4c574ee83cf0ee49 Mon Sep 17 00:00:00 2001 From: Esteban Borai Date: Fri, 12 Jul 2024 19:29:51 -0400 Subject: [PATCH] feat(client): introduce "UserRegister" for client (#206) --- Cargo.lock | 3 ++ crates/client/Cargo.toml | 4 +++ crates/client/src/lib.rs | 20 +++++++++++ crates/client/src/modules/auth/mod.rs | 32 +++++++++++++++++ .../src/modules/auth/token_create/mod.rs | 18 +++++++--- .../auth/user_register/UserRegister.gql | 17 +++++++++ .../src/modules/auth/user_register/mod.rs | 35 +++++++++++++++++++ crates/client/src/modules/mod.rs | 2 ++ crates/web/src/views/login.rs | 8 +++-- 9 files changed, 132 insertions(+), 7 deletions(-) create mode 100644 crates/client/src/modules/auth/user_register/UserRegister.gql create mode 100644 crates/client/src/modules/auth/user_register/mod.rs diff --git a/Cargo.lock b/Cargo.lock index f8193ee..0aa2131 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -876,7 +876,10 @@ dependencies = [ name = "client" version = "0.1.0" dependencies = [ + "chrono", + "core", "graphql_client", + "pxid", "reqwest", "serde", ] diff --git a/crates/client/Cargo.toml b/crates/client/Cargo.toml index 3ea21d9..47a5d82 100644 --- a/crates/client/Cargo.toml +++ b/crates/client/Cargo.toml @@ -9,6 +9,10 @@ name = "townhall_client" path = "src/lib.rs" [dependencies] +chrono = { workspace = true } graphql_client = { workspace = true, features = ["reqwest"] } +pxid = { workspace = true, features = ["serde"] } reqwest = { version = "0.11", features = ["blocking", "json"] } serde = { workspace = true } + +core = { path = "../core" } diff --git a/crates/client/src/lib.rs b/crates/client/src/lib.rs index 0794314..71a8a21 100644 --- a/crates/client/src/lib.rs +++ b/crates/client/src/lib.rs @@ -1,3 +1,23 @@ mod modules; pub use modules::*; + +use auth::AuthClient; + +pub struct Client { + pub auth: AuthClient, +} + +impl Default for Client { + fn default() -> Self { + Self::new() + } +} + +impl Client { + pub fn new() -> Self { + Self { + auth: AuthClient::new(), + } + } +} diff --git a/crates/client/src/modules/auth/mod.rs b/crates/client/src/modules/auth/mod.rs index 0900409..36b97d9 100644 --- a/crates/client/src/modules/auth/mod.rs +++ b/crates/client/src/modules/auth/mod.rs @@ -1 +1,33 @@ pub mod token_create; +pub mod user_register; + +use reqwest::Client; + +pub struct AuthClient { + client: Client, +} + +impl Default for AuthClient { + fn default() -> Self { + Self::new() + } +} + +impl AuthClient { + pub fn new() -> Self { + Self { + client: Client::new(), + } + } + + pub async fn token_create(&self, email: String, password: String) -> token_create::TokenCreate { + token_create::token_create(&self.client, email, password).await + } + + pub async fn user_register( + &self, + input: user_register::UserRegisterInput, + ) -> user_register::UserRegister { + user_register::user_register(&self.client, input).await + } +} diff --git a/crates/client/src/modules/auth/token_create/mod.rs b/crates/client/src/modules/auth/token_create/mod.rs index 61fb7a8..769ae57 100644 --- a/crates/client/src/modules/auth/token_create/mod.rs +++ b/crates/client/src/modules/auth/token_create/mod.rs @@ -2,20 +2,28 @@ use graphql_client::reqwest::post_graphql; use graphql_client::GraphQLQuery; use reqwest::Client; +use token_create::{TokenCreateTokenCreateError, TokenCreateTokenCreateToken}; + #[derive(GraphQLQuery)] #[graphql( response_derives = "Debug", schema_path = "schema.json", query_path = "src/modules/auth/token_create/TokenCreate.gql" )] -pub struct TokenCreate; +pub struct TokenCreate { + pub token: Option, + pub error: Option, +} -pub async fn token_create(email: String, password: String) -> token_create::TokenCreateTokenCreate { - let client = Client::builder().build().unwrap(); +pub async fn token_create(client: &Client, email: String, password: String) -> TokenCreate { let variables = token_create::Variables { email, password }; - let res = post_graphql::(&client, "http://127.0.0.1:7878/graphql", variables) + let res = post_graphql::(client, "http://127.0.0.1:7878/graphql", variables) .await .unwrap(); + let data = res.data.unwrap().token_create; - res.data.unwrap().token_create + TokenCreate { + token: data.token, + error: data.error, + } } diff --git a/crates/client/src/modules/auth/user_register/UserRegister.gql b/crates/client/src/modules/auth/user_register/UserRegister.gql new file mode 100644 index 0000000..a885ae7 --- /dev/null +++ b/crates/client/src/modules/auth/user_register/UserRegister.gql @@ -0,0 +1,17 @@ +mutation UserRegister($input: UserRegisterInput!) { + userRegister(input: $input) { + user { + id + name + surname + username + email + createdAt + updatedAt + } + error { + code + message + } + } +} diff --git a/crates/client/src/modules/auth/user_register/mod.rs b/crates/client/src/modules/auth/user_register/mod.rs new file mode 100644 index 0000000..9b7dd3b --- /dev/null +++ b/crates/client/src/modules/auth/user_register/mod.rs @@ -0,0 +1,35 @@ +use graphql_client::reqwest::post_graphql; +use graphql_client::GraphQLQuery; +use pxid::Pxid; +use reqwest::Client; + +use townhall::user::model::Email; +use user_register::{UserRegisterUserRegisterError, UserRegisterUserRegisterUser}; + +pub use crate::auth::user_register::user_register::UserRegisterInput; + +use crate::DateTime; + +#[derive(GraphQLQuery)] +#[graphql( + response_derives = "Debug", + schema_path = "schema.json", + query_path = "src/modules/auth/user_register/UserRegister.gql" +)] +pub struct UserRegister { + pub user: Option, + pub error: Option, +} + +pub async fn user_register(client: &Client, input: UserRegisterInput) -> UserRegister { + let variables = user_register::Variables { input }; + let res = post_graphql::(client, "http://127.0.0.1:7878/graphql", variables) + .await + .unwrap(); + let data = res.data.unwrap().user_register; + + UserRegister { + user: data.user, + error: data.error, + } +} diff --git a/crates/client/src/modules/mod.rs b/crates/client/src/modules/mod.rs index 0e4a05d..2e5a125 100644 --- a/crates/client/src/modules/mod.rs +++ b/crates/client/src/modules/mod.rs @@ -1 +1,3 @@ pub mod auth; + +pub(crate) type DateTime = chrono::DateTime; diff --git a/crates/web/src/views/login.rs b/crates/web/src/views/login.rs index 2af5e76..1f66f68 100644 --- a/crates/web/src/views/login.rs +++ b/crates/web/src/views/login.rs @@ -1,6 +1,6 @@ use leptos::{component, create_action, create_signal, view, IntoView, Show, SignalGet, SignalSet}; -use townhall_client::auth::token_create::token_create; +use townhall_client::Client; use crate::components::text_field::{TextField, TextFieldType}; @@ -9,7 +9,11 @@ pub fn Login() -> impl IntoView { let (error_getter, error_setter) = create_signal::>(None); let submit = create_action(move |_| async move { - let res = token_create("john@test.com".into(), "12345".into()).await; + let client = Client::new(); + let res = client + .auth + .token_create("john@test.com".into(), "12345".into()) + .await; if let Some(ref error) = res.error { error_setter.set(Some(error.message.to_owned()));