1+ use chrono:: Utc ;
2+
3+ use std:: ops:: Deref ;
4+
15use crate :: database:: content_folder:: ContentFolderOperator ;
6+ use crate :: database:: operation:: { Operation , OperationLog , OperationType , Table } ;
27use crate :: extractors:: user:: User ;
38use 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 ) ]
637pub 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