-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoperator.rs
More file actions
67 lines (56 loc) · 1.77 KB
/
Copy pathoperator.rs
File metadata and controls
67 lines (56 loc) · 1.77 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use chrono::Utc;
use std::ops::Deref;
use crate::database::content_folder::ContentFolderOperator;
use crate::database::operation::{Operation, OperationLog, OperationType, Table};
use crate::extractors::user::User;
use crate::state::AppState;
use crate::state::logger::LoggerError;
pub trait TableOperator: Deref<Target = DatabaseOperator> {
fn table(&self) -> Table;
fn log_create(
&self,
operation: impl Into<Operation>,
) -> impl std::future::Future<Output = Result<(), LoggerError>> {
self.log(self.table(), OperationType::Create, operation.into())
}
fn log_update(
&self,
operation: impl Into<Operation>,
) -> impl std::future::Future<Output = Result<(), LoggerError>> {
self.log(self.table(), OperationType::Update, operation.into())
}
fn log_delete(
&self,
operation: impl Into<Operation>,
) -> impl std::future::Future<Output = Result<(), LoggerError>> {
self.log(self.table(), OperationType::Delete, operation.into())
}
}
#[derive(Clone, Debug)]
pub struct DatabaseOperator {
pub state: AppState,
pub user: Option<User>,
}
impl DatabaseOperator {
pub fn new(state: AppState, user: Option<User>) -> Self {
Self { state, user }
}
pub async fn log(
&self,
table: Table,
operation_type: OperationType,
operation: Operation,
) -> Result<(), LoggerError> {
let operation = OperationLog {
user: self.user.clone(),
date: Utc::now(),
operation,
operation_type,
table,
};
self.state.logger.write(operation).await
}
pub fn content_folder<'a>(&'a self) -> ContentFolderOperator<'a> {
ContentFolderOperator { db: self }
}
}