-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca10e29
commit d7a41db
Showing
8 changed files
with
165 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
crates/server/src/graphql/modules/user/mutation/user_update.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use async_graphql::{Context, InputObject, Result, SimpleObject}; | ||
use gabble::user::repository::UpdateUserDto; | ||
use pxid::graphql::Pxid; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::context::SharedContext; | ||
use crate::graphql::modules::user::types::{User, UserError, UserErrorCode}; | ||
|
||
#[derive(Debug, Default, InputObject)] | ||
pub struct UserUpdateInput { | ||
pub name: Option<String>, | ||
pub surname: Option<String>, | ||
} | ||
|
||
#[derive(Debug, Default, Deserialize, Serialize, SimpleObject)] | ||
pub struct UserUpdate { | ||
user: Option<User>, | ||
error: Option<UserError>, | ||
} | ||
|
||
impl UserUpdate { | ||
pub async fn exec(ctx: &Context<'_>, id: Pxid, input: UserUpdateInput) -> Result<Self> { | ||
let context = ctx.data_unchecked::<SharedContext>(); | ||
let dto = UpdateUserDto { | ||
name: input.name, | ||
surname: input.surname, | ||
}; | ||
|
||
match context.services.user.update(id.into_inner(), dto).await { | ||
Ok(user) => Ok(Self { | ||
user: Some(User::from(user)), | ||
error: None, | ||
}), | ||
Err(err) => Ok(Self { | ||
user: None, | ||
error: Some(UserError { | ||
code: UserErrorCode::Internal, | ||
message: format!("An error ocurred: {err}"), | ||
}), | ||
}), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
mod user_register; | ||
mod user_update; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::str::FromStr; | ||
|
||
use async_graphql::{Request, Variables}; | ||
use gabble::user::{ | ||
model::{Email, Password, Username}, | ||
service::CreateUserDto, | ||
}; | ||
use serde_json::json; | ||
|
||
use crate::TestUtil; | ||
|
||
#[tokio::test] | ||
async fn updates_a_user() { | ||
let test_util = TestUtil::new_cleared().await; | ||
let (context, schema) = test_util.parts(); | ||
|
||
let user = context | ||
.services | ||
.user | ||
.create(CreateUserDto { | ||
name: "Augustine".into(), | ||
surname: "Madu".into(), | ||
email: Email::from_str("augustine@gmail.com").unwrap(), | ||
username: Username::from_str("Cudi").unwrap(), | ||
password: Password::from_str("Password12##$$").unwrap(), | ||
}) | ||
.await | ||
.unwrap(); | ||
|
||
let mutation: &str = " | ||
mutation UpdateUser($id: Pxid, $payload: UserUpdateInput!) { | ||
userUpdate(id:$id, input: $payload) { | ||
user { | ||
id | ||
name | ||
surname | ||
} | ||
error { | ||
code | ||
message | ||
} | ||
} | ||
} | ||
"; | ||
|
||
let result = schema | ||
.execute( | ||
Request::new(mutation).variables(Variables::from_json(json!({ | ||
"id": user.id.to_string(), | ||
"payload": { | ||
"name": "John", | ||
"surname": "Appleseed", | ||
} | ||
}))), | ||
) | ||
.await; | ||
|
||
println!("{result:#?}"); | ||
|
||
let data = result.data.into_json().unwrap(); | ||
let user_update_user = &data["userUpdate"]["user"]; | ||
|
||
assert_eq!(user_update_user["name"], "John"); | ||
assert_eq!(user_update_user["surname"], "Appleseed"); | ||
} |