diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/users-service/Cargo.toml | 2 | ||||
-rw-r--r-- | crates/users-service/migrations/20250726161947_profile.sql | 5 | ||||
-rw-r--r-- | crates/users-service/src/server/manager.rs | 64 |
3 files changed, 61 insertions, 10 deletions
diff --git a/crates/users-service/Cargo.toml b/crates/users-service/Cargo.toml index 2bbfe28..84a49ab 100644 --- a/crates/users-service/Cargo.toml +++ b/crates/users-service/Cargo.toml @@ -15,7 +15,7 @@ config = { workspace = true, features = ["convert-case", "toml"] } futures-util.workspace = true nanoid.workspace = true prost.workspace = true -sellershut-core = { workspace = true, features = ["users", "serde"] } +sellershut-core = { workspace = true, features = ["users", "serde", "time"] } serde = { workspace = true, features = ["derive"] } serde_json.workspace = true sqlx = { workspace = true, features = ["macros", "migrate", "runtime-tokio", "time", "tls-rustls", "uuid"] } diff --git a/crates/users-service/migrations/20250726161947_profile.sql b/crates/users-service/migrations/20250726161947_profile.sql index 15822c8..1fcfe68 100644 --- a/crates/users-service/migrations/20250726161947_profile.sql +++ b/crates/users-service/migrations/20250726161947_profile.sql @@ -2,9 +2,10 @@ create table profile ( id text primary key, username varchar(30) not null, inbox text not null, - outbox text, + outbox text not null, local boolean not null, - avatar_url text, + avatar text, + email text, description text, user_type text not null check ( user_type IN ('PERSON', 'APPLICATION', 'GROUP', 'ORGANIZATION', 'SERVICE') diff --git a/crates/users-service/src/server/manager.rs b/crates/users-service/src/server/manager.rs index 6affb4a..6738123 100644 --- a/crates/users-service/src/server/manager.rs +++ b/crates/users-service/src/server/manager.rs @@ -1,15 +1,55 @@ use prost::Message; use sellershut_core::users::{ - CompleteUserRequest, CreateUserRequest, CreateUserResponse, User, + CompleteUserRequest, CreateUserRequest, CreateUserResponse, User, UserType, users_service_server::UsersService, }; use stack_up::redis::AsyncCommands; +use time::OffsetDateTime; use tonic::{Request, Response, Status, async_trait}; use tracing::{error, trace}; use uuid::Uuid; use crate::state::AppHandle; +struct DbUser { + id: String, + username: String, + inbox: String, + outbox: String, + email: Option<String>, + avatar: Option<String>, + created_at: OffsetDateTime, + updated_at: OffsetDateTime, + description: Option<String>, + user_type: String, + public_key: String, + local: bool, +} + +impl TryFrom<DbUser> for User { + type Error = Status; + + fn try_from(value: DbUser) -> Result<Self, Self::Error> { + let user_type = UserType::from_str_name(&value.user_type) + .ok_or_else(|| Status::internal("invalid user type"))? + .into(); + Ok(Self { + id: value.id, + email: value.email, + username: value.username, + avatar: value.avatar, + created_at: Some(value.created_at.into()), + updated_at: Some(value.updated_at.into()), + description: value.description, + user_type, + public_key: value.public_key, + inbox: value.inbox, + outbox: value.outbox, + local: value.local, + }) + } +} + #[async_trait] impl UsersService for AppHandle { #[doc = " Create a new user profile"] @@ -54,7 +94,8 @@ impl UsersService for AppHandle { let create_user = CreateUserRequest::decode(resp.as_ref()) .map_err(|_e| Status::data_loss("internal data corrupted"))?; - let user = sqlx::query!( + let user = sqlx::query_as!( + DbUser, "insert into profile ( @@ -63,12 +104,14 @@ impl UsersService for AppHandle { inbox, outbox, local, - avatar_url, + avatar, description, user_type, - public_key + public_key, + email ) - values ($1, $2, $3, $4, $5, $6, $7, $8, $9) + values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) + returning * ", request.id, request.username, @@ -79,7 +122,14 @@ impl UsersService for AppHandle { request.description, request.user_type.to_string(), request.public_key, - ); - todo!() + create_user.email, + ) + .fetch_one(&self.services.postgres) + .await + .map_err(|_e| Status::internal("storage error"))?; + + let user = User::try_from(user)?; + + Ok(Response::new(user)) } } |