Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Commit

Permalink
feat(settings): add /settings get and update
Browse files Browse the repository at this point in the history
now supports only displayName change HOW-69
  • Loading branch information
sergeysova committed Aug 15, 2019
1 parent 91f9298 commit 1fe2cd4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ fn run() -> Result<(), failure::Error> {
fn create_server(db_url: String) -> Result<(), failure::Error> {
env_logger::init();
use self::app_state::DbExecutor;
use actix_base::{SyncArbiter, System};

let cpus = num_cpus::get();
let system = System::new("htc-server");
Expand Down
11 changes: 10 additions & 1 deletion src/models/user.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::schema::users;
use crate::views::{EncodableUserPrivate, EncodableUserPublic};
use crate::views::{EncodableUserPrivate, EncodableUserPublic, UserSettings};
use diesel::prelude::*;

#[derive(Queryable, Associations, Identifiable, Debug)]
Expand Down Expand Up @@ -34,6 +34,15 @@ impl User {
}
}

pub fn encodable_settings(self) -> UserSettings {
let User { display_name, .. } = self;

UserSettings {
display_name,
gravatar_email: None,
}
}

pub fn find_by_id(conn: &PgConnection, user_id: i32) -> Option<Self> {
use crate::schema::users::dsl::*;

Expand Down
46 changes: 36 additions & 10 deletions src/routes/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,52 @@ pub fn create(
.responder()
}

fn update(state: State<AppState>, update: Json<AccountUpdate>) -> FutureResponse<HttpResponse> {
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct R {
user: views::EncodableUserPrivate,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Update {
display_name: Option<String>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct SettingsResponse {
settings: views::UserSettings,
}

fn update(
state: State<AppState>,
auth: Auth,
update: Json<Update>,
) -> FutureResponse<HttpResponse> {
state
.pg
.send(update.0)
.send(AccountUpdate {
requester_id: auth.user.id,
display_name: update.display_name.clone(),
})
.from_err()
.and_then(|res| match res {
Ok(user) => Ok(answer_success!(
Ok,
R {
user: user.encodable_private()
SettingsResponse {
settings: user.encodable_settings()
}
)),
Err(err) => Ok(err.error_response()),
})
.responder()
}

pub fn settings(auth: Auth) -> FutureResponse<HttpResponse> {
futures::future::ok(answer_success!(
Ok,
SettingsResponse {
settings: auth.user.encodable_settings()
}
))
.responder()
}

/// POST /account/session
pub fn login(
login_data: Json<SessionCreate>,
Expand Down Expand Up @@ -105,7 +128,10 @@ pub fn scope(scope: Scope<AppState>) -> Scope<AppState> {
scope
.resource("/", |r| {
r.post().with(self::create);
r.get().with(self::update);
})
.resource("/settings/", |r| {
r.get().with(self::settings);
r.put().with(self::update);
})
.resource("/session/", |r| {
r.post().with(self::login);
Expand Down
8 changes: 8 additions & 0 deletions src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ pub struct EncodableUserPublic {
pub display_name: Option<String>,
pub id: i32,
}

/// User settings to communicate with frontend
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UserSettings {
pub display_name: Option<String>,
pub gravatar_email: Option<String>,
}

0 comments on commit 1fe2cd4

Please sign in to comment.