Skip to content

Commit

Permalink
New Edit View profile routers
Browse files Browse the repository at this point in the history
  • Loading branch information
nyzd committed Jan 27, 2024
1 parent f2f0c28 commit 310b60f
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 2 deletions.
66 changes: 66 additions & 0 deletions src/routers/profile/profile_edit.rs
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()
}
61 changes: 59 additions & 2 deletions src/routers/profile/profile_view.rs
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()
}

0 comments on commit 310b60f

Please sign in to comment.