1
0
Fork 0
mirror of https://git.42l.fr/neil/sncf.git synced 2024-04-28 04:02:47 +02:00
sncf/src/database/methods.rs

57 lines
1.5 KiB
Rust
Raw Normal View History

2020-08-19 01:21:42 +02:00
use chrono::NaiveDateTime;
use chrono::Utc;
use diesel::prelude::*;
2020-08-22 16:34:28 +02:00
use crate::database::schema::form;
2020-08-19 01:21:42 +02:00
use crate::database::schema::form::dsl::*;
use crate::database::structs::Form;
use crate::DbConn;
2020-08-19 01:21:42 +02:00
#[table_name = "form"]
#[derive(Serialize, Insertable)]
pub struct InsertableForm<'b> {
pub created_at: NaiveDateTime,
pub lastvisit_at: NaiveDateTime,
pub token: &'b str,
pub nc_username: &'b str,
pub nc_password: &'b str,
}
impl Form {
// gets a Form from a token.
// also updates lastvisit_at.
pub fn get_from_token(
i_token: &str,
conn: &DbConn,
2020-08-19 01:21:42 +02:00
) -> Result<Option<Form>, diesel::result::Error> {
2020-08-22 16:34:28 +02:00
if let Some(formdata) = form
.filter(token.eq(i_token))
.first::<Form>(conn)
.optional()?
{
2020-08-19 01:21:42 +02:00
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> {
2020-08-19 01:21:42 +02:00
diesel::update(form.find(self.id))
.set(lastvisit_at.eq(Utc::now().naive_utc()))
.execute(conn)
}
2020-08-22 16:34:28 +02:00
pub fn insert<'b>(
i_form: InsertableForm<'b>,
conn: &DbConn,
2020-08-19 01:21:42 +02:00
) -> Result<InsertableForm<'b>, diesel::result::Error> {
match diesel::insert_into(form).values(&i_form).execute(conn) {
Ok(_) => Ok(i_form),
Err(e) => Err(e),
}
}
}