mirror of https://git.42l.fr/neil/sncf.git
57 lines
1.5 KiB
Rust
57 lines
1.5 KiB
Rust
use chrono::NaiveDateTime;
|
|
use chrono::Utc;
|
|
use diesel::prelude::*;
|
|
|
|
use crate::database::schema::form;
|
|
use crate::database::schema::form::dsl::*;
|
|
use crate::database::structs::Form;
|
|
use crate::DbConn;
|
|
|
|
#[table_name = "form"]
|
|
#[derive(Serialize, Insertable)]
|
|
pub struct InsertableForm {
|
|
pub created_at: NaiveDateTime,
|
|
pub lastvisit_at: NaiveDateTime,
|
|
pub token: String,
|
|
pub nc_username: String,
|
|
pub nc_password: String,
|
|
}
|
|
|
|
impl Form {
|
|
// gets a Form from a token.
|
|
// also updates lastvisit_at.
|
|
pub fn get_from_token(
|
|
i_token: &str,
|
|
conn: &DbConn,
|
|
) -> Result<Option<Form>, diesel::result::Error> {
|
|
if let Some(formdata) = form
|
|
.filter(token.eq(i_token))
|
|
.first::<Form>(conn)
|
|
.optional()?
|
|
{
|
|
match formdata.update_lastvisit(conn) {
|
|
Ok(_) => Ok(Some(formdata)),
|
|
Err(e) => Err(e),
|
|
}
|
|
} else {
|
|
Ok(None)
|
|
}
|
|
}
|
|
|
|
pub fn update_lastvisit(&self, conn: &DbConn) -> Result<usize, diesel::result::Error> {
|
|
diesel::update(form.find(self.id))
|
|
.set(lastvisit_at.eq(Utc::now().naive_utc()))
|
|
.execute(conn)
|
|
}
|
|
|
|
pub fn insert(
|
|
i_form: InsertableForm,
|
|
conn: &DbConn,
|
|
) -> Result<InsertableForm, diesel::result::Error> {
|
|
match diesel::insert_into(form).values(&i_form).execute(conn) {
|
|
Ok(_) => Ok(i_form),
|
|
Err(e) => Err(e),
|
|
}
|
|
}
|
|
}
|