-use crate::{newtypes::LocalUserId, source::email_verification::*, traits::Crud};
+use crate::{
+ newtypes::LocalUserId,
+ schema::email_verification::dsl::{
+ email_verification,
+ local_user_id,
+ published,
+ verification_token,
+ },
+ source::email_verification::{EmailVerification, EmailVerificationForm},
+ utils::{get_conn, DbPool},
+};
use diesel::{
- dsl::*,
+ dsl::{now, IntervalDsl},
insert_into,
result::Error,
ExpressionMethods,
- PgConnection,
QueryDsl,
- RunQueryDsl,
};
+use diesel_async::RunQueryDsl;
-impl Crud for EmailVerification {
- type Form = EmailVerificationForm;
- type IdType = i32;
- fn create(conn: &PgConnection, form: &EmailVerificationForm) -> Result<Self, Error> {
- use crate::schema::email_verification::dsl::*;
+impl EmailVerification {
+ pub async fn create(pool: &mut DbPool<'_>, form: &EmailVerificationForm) -> Result<Self, Error> {
+ let conn = &mut get_conn(pool).await?;
insert_into(email_verification)
.values(form)
.get_result::<Self>(conn)
+ .await
}
- fn read(conn: &PgConnection, id_: i32) -> Result<Self, Error> {
- use crate::schema::email_verification::dsl::*;
- email_verification.find(id_).first::<Self>(conn)
- }
-
- fn update(conn: &PgConnection, id_: i32, form: &EmailVerificationForm) -> Result<Self, Error> {
- use crate::schema::email_verification::dsl::*;
- diesel::update(email_verification.find(id_))
- .set(form)
- .get_result::<Self>(conn)
- }
-
- fn delete(conn: &PgConnection, id_: i32) -> Result<usize, Error> {
- 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<Self, Error> {
- use crate::schema::email_verification::dsl::*;
+ pub async fn read_for_token(pool: &mut DbPool<'_>, token: &str) -> Result<Self, Error> {
+ let conn = &mut get_conn(pool).await?;
email_verification
.filter(verification_token.eq(token))
.filter(published.gt(now - 7.days()))
.first::<Self>(conn)
+ .await
}
- pub fn delete_old_tokens_for_local_user(
- conn: &PgConnection,
+ pub async fn delete_old_tokens_for_local_user(
+ pool: &mut DbPool<'_>,
local_user_id_: LocalUserId,
) -> Result<usize, Error> {
- use crate::schema::email_verification::dsl::*;
- diesel::delete(email_verification.filter(local_user_id.eq(local_user_id_))).execute(conn)
+ let conn = &mut get_conn(pool).await?;
+ diesel::delete(email_verification.filter(local_user_id.eq(local_user_id_)))
+ .execute(conn)
+ .await
}
}