Skip to content

Commit 343dd48

Browse files
committed
feat: Common logging interface via TableOperator trait
1 parent ef29535 commit 343dd48

2 files changed

Lines changed: 62 additions & 22 deletions

File tree

src/database/content_folder/operator.rs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
use chrono::Utc;
21
use sea_orm::*;
32
use snafu::prelude::*;
43

54
use std::ops::Deref;
65
use std::str::FromStr;
76

8-
use crate::database::operation::OperationLog;
9-
use crate::database::operation::OperationType;
107
use crate::database::operation::Table;
11-
use crate::database::operator::DatabaseOperator;
8+
use crate::database::operator::{DatabaseOperator, TableOperator};
129
use crate::extractors::normalized_path::NormalizedPathComponent;
1310

1411
use super::*;
@@ -26,6 +23,12 @@ impl Deref for ContentFolderOperator<'_> {
2623
}
2724
}
2825

26+
impl TableOperator for ContentFolderOperator<'_> {
27+
fn table(&self) -> Table {
28+
Table::ContentFolder
29+
}
30+
}
31+
2932
impl ContentFolderOperator<'_> {
3033
/// List content folders
3134
///
@@ -101,24 +104,13 @@ impl ContentFolderOperator<'_> {
101104
.context(IOSnafu)?;
102105
}
103106

104-
let operation_log = OperationLog {
105-
user: self.user.clone(),
106-
date: Utc::now(),
107-
operation: ContentFolderOperation::Create {
108-
id: model.id,
109-
name: model.name.to_string(),
110-
parent: parent.as_ref().map(|x| (x.id, x.name.to_string())),
111-
}
112-
.into(),
113-
operation_type: OperationType::Create,
114-
table: Table::ContentFolder,
115-
};
116-
117-
self.state
118-
.logger
119-
.write(operation_log)
120-
.await
121-
.context(LoggerSnafu)?;
107+
self.log_create(ContentFolderOperation::Create {
108+
id: model.id,
109+
name: model.name.to_string(),
110+
parent: parent.as_ref().map(|x| (x.id, x.name.to_string())),
111+
})
112+
.await
113+
.context(LoggerSnafu)?;
122114

123115
Ok(model)
124116
}

src/database/operator.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
1+
use chrono::Utc;
2+
3+
use std::ops::Deref;
4+
15
use crate::database::content_folder::ContentFolderOperator;
6+
use crate::database::operation::{Operation, OperationLog, OperationType, Table};
27
use crate::extractors::user::User;
38
use crate::state::AppState;
9+
use crate::state::logger::LoggerError;
10+
11+
pub trait TableOperator: Deref<Target = DatabaseOperator> {
12+
fn table(&self) -> Table;
13+
14+
fn log_create(
15+
&self,
16+
operation: impl Into<Operation>,
17+
) -> impl std::future::Future<Output = Result<(), LoggerError>> {
18+
self.log(self.table(), OperationType::Create, operation.into())
19+
}
20+
21+
fn log_update(
22+
&self,
23+
operation: impl Into<Operation>,
24+
) -> impl std::future::Future<Output = Result<(), LoggerError>> {
25+
self.log(self.table(), OperationType::Update, operation.into())
26+
}
27+
28+
fn log_delete(
29+
&self,
30+
operation: impl Into<Operation>,
31+
) -> impl std::future::Future<Output = Result<(), LoggerError>> {
32+
self.log(self.table(), OperationType::Delete, operation.into())
33+
}
34+
}
435

536
#[derive(Clone, Debug)]
637
pub struct DatabaseOperator {
@@ -13,6 +44,23 @@ impl DatabaseOperator {
1344
Self { state, user }
1445
}
1546

47+
pub async fn log(
48+
&self,
49+
table: Table,
50+
operation_type: OperationType,
51+
operation: Operation,
52+
) -> Result<(), LoggerError> {
53+
let operation = OperationLog {
54+
user: self.user.clone(),
55+
date: Utc::now(),
56+
operation,
57+
operation_type,
58+
table,
59+
};
60+
61+
self.state.logger.write(operation).await
62+
}
63+
1664
pub fn content_folder<'a>(&'a self) -> ContentFolderOperator<'a> {
1765
ContentFolderOperator { db: self }
1866
}

0 commit comments

Comments
 (0)