Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/database/content_folder/folder_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct FolderView {

impl FolderView {
/// Loads the top-most folder view, which is not a folder and may not have parents.
pub async fn index(operator: &ContentFolderOperator) -> Result<Self, ContentFolderError> {
pub async fn index(operator: &ContentFolderOperator<'_>) -> Result<Self, ContentFolderError> {
let children = operator
.list()
.await?
Expand All @@ -37,7 +37,7 @@ impl FolderView {
/// - the requested ID does not exist
// TODO: optimize with custom query
pub async fn from_id(
operator: &ContentFolderOperator,
operator: &ContentFolderOperator<'_>,
id: i32,
) -> Result<Self, ContentFolderError> {
let list = operator.list().await?;
Expand Down
57 changes: 22 additions & 35 deletions src/database/content_folder/operator.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
use chrono::Utc;
use sea_orm::*;
use snafu::prelude::*;

use std::ops::Deref;
use std::str::FromStr;

use crate::database::operation::OperationLog;
use crate::database::operation::OperationType;
use crate::database::operation::Table;
use crate::database::operator::DatabaseOperator;
use crate::database::operator::{DatabaseOperator, TableOperator};
use crate::extractors::normalized_path::NormalizedPathComponent;
use crate::extractors::user::User;
use crate::state::AppState;

use super::*;

#[derive(Clone, Debug)]
pub struct ContentFolderOperator {
pub state: AppState,
pub user: Option<User>,
pub struct ContentFolderOperator<'a> {
pub db: &'a DatabaseOperator,
}

impl ContentFolderOperator {
pub fn new(state: AppState, user: Option<User>) -> Self {
Self { state, user }
impl Deref for ContentFolderOperator<'_> {
type Target = DatabaseOperator;

fn deref(&self) -> &DatabaseOperator {
self.db
}
}

pub fn db(&self) -> DatabaseOperator {
DatabaseOperator {
state: self.state.clone(),
user: self.user.clone(),
}
impl TableOperator for ContentFolderOperator<'_> {
fn table(&self) -> Table {
Table::ContentFolder
}
}

impl ContentFolderOperator<'_> {
/// List content folders
///
/// Should not fail, unless SQLite was corrupted for some reason.
Expand Down Expand Up @@ -106,24 +104,13 @@ impl ContentFolderOperator {
.context(IOSnafu)?;
}

let operation_log = OperationLog {
user: self.user.clone(),
date: Utc::now(),
operation: ContentFolderOperation::Create {
id: model.id,
name: model.name.to_string(),
parent: parent.as_ref().map(|x| (x.id, x.name.to_string())),
}
.into(),
operation_type: OperationType::Create,
table: Table::ContentFolder,
};

self.state
.logger
.write(operation_log)
.await
.context(LoggerSnafu)?;
self.log_create(ContentFolderOperation::Create {
id: model.id,
name: model.name.to_string(),
parent: parent.as_ref().map(|x| (x.id, x.name.to_string())),
})
.await
.context(LoggerSnafu)?;

Ok(model)
}
Expand Down
53 changes: 49 additions & 4 deletions src/database/operator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
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 {
Expand All @@ -13,10 +44,24 @@ impl DatabaseOperator {
Self { state, user }
}

pub fn content_folder(&self) -> ContentFolderOperator {
ContentFolderOperator {
state: self.state.clone(),
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 }
}
}
Loading