-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
Showing
2 changed files
with
125 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use actix_web::web; | ||
use diesel::prelude::*; | ||
|
||
use crate::{ | ||
error::RouterError, | ||
models::{Account, User, UserName}, | ||
routers::user::EditableUser, | ||
DbPool, | ||
}; | ||
|
||
pub async fn profile_edit( | ||
user_id: web::ReqData<u32>, | ||
pool: web::Data<DbPool>, | ||
new_user: web::Json<EditableUser>, | ||
) -> Result<&'static str, RouterError> { | ||
use crate::schema::app_accounts::dsl::{app_accounts, id as account_id, username}; | ||
use crate::schema::app_user_names::dsl::{first_name, last_name, primary_name}; | ||
use crate::schema::app_users::dsl::*; | ||
|
||
let user_id = user_id.into_inner(); | ||
let new_user = new_user.into_inner(); | ||
|
||
web::block(move || { | ||
let mut conn = pool.get().unwrap(); | ||
|
||
// First find the account from id | ||
let account: Account = app_accounts | ||
.filter(account_id.eq(user_id as i32)) | ||
.get_result(&mut conn)?; | ||
|
||
let user: User = User::belonging_to(&account).get_result(&mut conn)?; | ||
|
||
// Now update the account username | ||
diesel::update(&account) | ||
.set(username.eq(new_user.username)) | ||
.execute(&mut conn)?; | ||
|
||
// And update the other data | ||
diesel::update(&user) | ||
.set(( | ||
birthday.eq(new_user.birthday), | ||
profile_image.eq(new_user.profile_image), | ||
)) | ||
.execute(&mut conn)?; | ||
|
||
// Also edit the primary name | ||
|
||
// First We get the user_names of the account | ||
// We assume that user has at least primary name | ||
let name = UserName::belonging_to(&account) | ||
.filter(primary_name.eq(true)) | ||
.first::<UserName>(&mut conn)?; | ||
|
||
// Now we update it | ||
diesel::update(&name) | ||
.set(( | ||
first_name.eq(new_user.first_name), | ||
last_name.eq(new_user.last_name), | ||
)) | ||
.execute(&mut conn)?; | ||
|
||
Ok("Edited") | ||
}) | ||
.await | ||
.unwrap() | ||
} |
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,10 +1,67 @@ | ||
use diesel::prelude::*; | ||
use actix_web::web; | ||
|
||
use crate::DbPool; | ||
use crate::{error::RouterError, routers::user::FullUserProfile, DbPool, models::{Account, User, Email, UserName}}; | ||
|
||
pub async fn profile_view( | ||
user_id: web::ReqData<u32>, | ||
pool: web::Data<DbPool>, | ||
) -> Result<web::Json<FullUserProfile>, RouterError> { | ||
todo!() | ||
use crate::schema::app_accounts::dsl::{app_accounts, id as account_id}; | ||
use crate::schema::app_user_names::dsl::primary_name; | ||
|
||
let user_id = user_id.into_inner(); | ||
|
||
web::block(move || { | ||
let mut conn = pool.get().unwrap(); | ||
|
||
// Get the account from user_id | ||
// which is unwraped from token | ||
let account: Account = app_accounts | ||
.filter(account_id.eq(user_id as i32)) | ||
.get_result(&mut conn)?; | ||
|
||
let user: User = User::belonging_to(&account).get_result(&mut conn)?; | ||
|
||
let email = Email::belonging_to(&account).first::<Email>(&mut conn)?; | ||
|
||
// Now get the user names | ||
let names = UserName::belonging_to(&account) | ||
.filter(primary_name.eq(true)) | ||
.load::<UserName>(&mut conn)?; | ||
|
||
// Is user have any names ? | ||
let names = if names.is_empty() { None } else { Some(names) }; | ||
|
||
let profile = match names { | ||
Some(names) => { | ||
// Its must be always > 1 element | ||
let name: &UserName = names.get(0).unwrap(); | ||
|
||
FullUserProfile { | ||
uuid: account.uuid.to_string(), | ||
email: email.email, | ||
username: account.username.to_owned(), | ||
first_name: Some(name.first_name.to_owned()), | ||
last_name: Some(name.last_name.to_owned()), | ||
birthday: user.clone().birthday, | ||
profile_image: user.clone().profile_image, | ||
} | ||
} | ||
|
||
None => FullUserProfile { | ||
uuid: account.uuid.to_string(), | ||
email: email.email, | ||
username: account.username.to_owned(), | ||
first_name: None, | ||
last_name: None, | ||
birthday: user.clone().birthday, | ||
profile_image: user.clone().profile_image, | ||
}, | ||
}; | ||
|
||
Ok(web::Json(profile)) | ||
}) | ||
.await | ||
.unwrap() | ||
} |