forked from Txio-labs/txio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathotp_repository.rs
More file actions
47 lines (38 loc) · 1.23 KB
/
Copy pathotp_repository.rs
File metadata and controls
47 lines (38 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use mongodb::{Client, Collection};
use crate::model::otp::OTP;
use crate::utils::error::AppError;
use mongodb::bson::doc;
#[derive(Clone)]
pub struct OTPRepository {
collection: Collection<OTP>,
}
impl OTPRepository {
pub fn new(db: &Client) -> Self {
let collection = db.database("txio_db").collection("otps");
Self { collection }
}
pub async fn save(&self, otp: &OTP) -> Result<OTP, AppError> {
let result = self.collection
.insert_one(otp, None)
.await?;
let mut otp_with_id = otp.clone();
if let Some(inserted_id) = result.inserted_id.as_object_id() {
otp_with_id.id = Some(inserted_id);
}
Ok(otp_with_id)
}
pub async fn find_by_email(&self, email: &str) -> Result<OTP, AppError> {
let otp = self
.collection
.find_one(doc! { "email": email }, None)
.await?
.ok_or(AppError::NotFound("OTP not found for email".to_string()))?;
Ok(otp)
}
pub async fn delete_by_email(&self, email: &str) -> Result<(), AppError> {
self.collection
.delete_many(doc! { "email": email }, None)
.await?;
Ok(())
}
}