use crate::{newtypes::LocalUserId, source::email_verification::*, traits::Crud}; use diesel::{ dsl::*, insert_into, result::Error, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl, }; impl Crud for EmailVerification { type Form = EmailVerificationForm; type IdType = i32; fn create(conn: &PgConnection, form: &EmailVerificationForm) -> Result { use crate::schema::email_verification::dsl::*; insert_into(email_verification) .values(form) .get_result::(conn) } fn read(conn: &PgConnection, id_: i32) -> Result { use crate::schema::email_verification::dsl::*; email_verification.find(id_).first::(conn) } fn update(conn: &PgConnection, id_: i32, form: &EmailVerificationForm) -> Result { use crate::schema::email_verification::dsl::*; diesel::update(email_verification.find(id_)) .set(form) .get_result::(conn) } fn delete(conn: &PgConnection, id_: i32) -> Result { use crate::schema::email_verification::dsl::*; diesel::delete(email_verification.find(id_)).execute(conn) } } impl EmailVerification { pub fn read_for_token(conn: &PgConnection, token: &str) -> Result { use crate::schema::email_verification::dsl::*; email_verification .filter(verification_token.eq(token)) .filter(published.gt(now - 7.days())) .first::(conn) } pub fn delete_old_tokens_for_local_user( conn: &PgConnection, local_user_id_: LocalUserId, ) -> Result { use crate::schema::email_verification::dsl::*; diesel::delete(email_verification.filter(local_user_id.eq(local_user_id_))).execute(conn) } }