1
0
Fork 0
mirror of https://git.42l.fr/neil/sncf.git synced 2024-05-04 06:53:12 +02:00

now using database pool for db. Fixes #21

This commit is contained in:
neil 2020-11-03 17:02:03 +01:00
parent f07c8f960f
commit 6760517c70
2 changed files with 16 additions and 12 deletions

View file

@ -9,12 +9,12 @@ use crate::DbConn;
#[table_name = "form"] #[table_name = "form"]
#[derive(Serialize, Insertable)] #[derive(Serialize, Insertable)]
pub struct InsertableForm<'b> { pub struct InsertableForm {
pub created_at: NaiveDateTime, pub created_at: NaiveDateTime,
pub lastvisit_at: NaiveDateTime, pub lastvisit_at: NaiveDateTime,
pub token: &'b str, pub token: String,
pub nc_username: &'b str, pub nc_username: String,
pub nc_password: &'b str, pub nc_password: String,
} }
impl Form { impl Form {
@ -45,9 +45,9 @@ impl Form {
} }
pub fn insert<'b>( pub fn insert<'b>(
i_form: InsertableForm<'b>, i_form: InsertableForm,
conn: &DbConn, conn: &DbConn,
) -> Result<InsertableForm<'b>, diesel::result::Error> { ) -> Result<InsertableForm, diesel::result::Error> {
match diesel::insert_into(form).values(&i_form).execute(conn) { match diesel::insert_into(form).values(&i_form).execute(conn) {
Ok(_) => Ok(i_form), Ok(_) => Ok(i_form),
Err(e) => Err(e), Err(e) => Err(e),

View file

@ -156,7 +156,8 @@ pub async fn forward_login(
})?; })?;
// check if the link exists in DB. if it does, update lastvisit_at. // check if the link exists in DB. if it does, update lastvisit_at.
let formdata = Form::get_from_token(&params.token, &conn) let formdata = web::block(move || Form::get_from_token(&params.token, &conn))
.await
.map_err(|e| { .map_err(|e| {
eprintln!("error_forwardlogin_db_get (diesel error): {}", e); eprintln!("error_forwardlogin_db_get (diesel error): {}", e);
crash(get_lang(&req), "error_forwardlogin_db_get") crash(get_lang(&req), "error_forwardlogin_db_get")
@ -237,17 +238,20 @@ pub async fn forward_register(
let token = gen_token(45); let token = gen_token(45);
let token_mv = token.clone();
// store the result in DB // store the result in DB
let form_result = Form::insert( let form_result = web::block(move || Form::insert(
InsertableForm { InsertableForm {
created_at: Utc::now().naive_utc(), created_at: Utc::now().naive_utc(),
lastvisit_at: Utc::now().naive_utc(), lastvisit_at: Utc::now().naive_utc(),
token: &token, token: token_mv,
nc_username: &nc_username, nc_username,
nc_password: &nc_password, nc_password,
}, },
&conn, &conn,
); ))
.await;
if form_result.is_err() { if form_result.is_err() {
return Err(crash(lang, "error_forwardregister_db")); return Err(crash(lang, "error_forwardregister_db"));