Browse Source

more data access functions

pbs
Jake Probst 5 years ago
parent
commit
30c97ce732
  1. 38
      src/login/dataaccess.rs

38
src/login/dataaccess.rs

@ -1,17 +1,28 @@
use std::default::Default;
use diesel::r2d2; use diesel::r2d2;
use diesel::prelude::*; use diesel::prelude::*;
use diesel::pg::PgConnection; use diesel::pg::PgConnection;
use libpso::character::settings;
use crate::models::*; use crate::models::*;
type ConnectionPool = r2d2::Pool<r2d2::ConnectionManager<PgConnection>>; type ConnectionPool = r2d2::Pool<r2d2::ConnectionManager<PgConnection>>;
pub trait DataAccess { pub trait DataAccess {
fn get_user_by_id(&self, id: u32) -> Option<UserAccount> {
fn get_user_by_id(&self, _id: u32) -> Option<UserAccount> {
unimplemented!();
}
fn get_user_by_name(&self, _username: String) -> Option<UserAccount> {
unimplemented!(); unimplemented!();
} }
fn get_user_by_name(&self, username: String) -> Option<UserAccount> {
fn get_user_settings_by_user(&self, _user: &UserAccount) -> Option<UserSettings> {
unimplemented!();
}
fn create_user_settings_by_user(&self, _user: &UserAccount) -> UserSettings {
unimplemented!(); unimplemented!();
} }
} }
@ -40,9 +51,24 @@ impl DataAccess for DBAccess {
}) })
.unwrap_or(None) .unwrap_or(None)
} }
}
fn get_user_settings_by_user(&self, user: &UserAccount) -> Option<UserSettings> {
self.connection_pool.get()
.ok()
.and_then(|conn| {
UserSettings::belonging_to(user).first::<UserSettings>(&conn).ok()
})
}
/*impl DataAccess for DatabaseAccess {
}*/
fn create_user_settings_by_user(&self, user: &UserAccount) -> UserSettings {
use elseware::schema::user_settings::dsl::user_settings;
self.connection_pool.get()
.map(|conn| {
let new_settings = NewUserSettings {
user_id: user.id,
settings: EUserSettings(settings::UserSettings::default())
};
diesel::insert_into(user_settings).values(&new_settings).get_result(&conn).unwrap()
}).unwrap()
}
}
Loading…
Cancel
Save