-
Notifications
You must be signed in to change notification settings - Fork 1
Add content folders #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Gabatxo1312
wants to merge
1
commit into
angrynode:axum
Choose a base branch
from
Gabatxo1312:add-content-folders
base: axum
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| use chrono::Utc; | ||
| use sea_orm::entity::prelude::*; | ||
| use sea_orm::*; | ||
| use snafu::prelude::*; | ||
|
|
||
| use crate::database::category::{self, CategoryError, CategoryOperator}; | ||
| use crate::database::operation::{Operation, OperationId, OperationLog, OperationType, Table}; | ||
| use crate::extractors::user::User; | ||
| use crate::routes::content_folder::ContentFolderForm; | ||
| use crate::state::AppState; | ||
| use crate::state::logger::LoggerError; | ||
|
|
||
| /// A content folder to store associated files. | ||
| /// | ||
| /// Each content folder has a name and an associated path on disk, a Category | ||
| /// and it can have an Parent Content Folder (None if it's the first folder | ||
| /// in category) | ||
| #[sea_orm::model] | ||
| #[derive(DeriveEntityModel, Clone, Debug, PartialEq, Eq)] | ||
| #[sea_orm(table_name = "content_folder")] | ||
| pub struct Model { | ||
| #[sea_orm(primary_key)] | ||
| pub id: i32, | ||
| pub name: String, | ||
| #[sea_orm(unique)] | ||
| pub path: String, | ||
| pub category_id: i32, | ||
| #[sea_orm(belongs_to, from = "category_id", to = "id")] | ||
| pub category: HasOne<category::Entity>, | ||
| pub parent_id: Option<i32>, | ||
| #[sea_orm(self_ref, relation_enum = "Parent", from = "parent_id", to = "id")] | ||
| pub parent: HasOne<Entity>, | ||
| } | ||
|
|
||
| #[async_trait::async_trait] | ||
| impl ActiveModelBehavior for ActiveModel {} | ||
|
|
||
| #[derive(Debug, Snafu)] | ||
| #[snafu(visibility(pub))] | ||
| pub enum ContentFolderError { | ||
| #[snafu(display("There is already a content folder called `{name}`"))] | ||
| NameTaken { name: String }, | ||
| #[snafu(display("There is already a content folder in dir `{path}`"))] | ||
| PathTaken { path: String }, | ||
| #[snafu(display("The Content Folder (Path: {path}) does not exist"))] | ||
| NotFound { path: String }, | ||
| #[snafu(display("Database error"))] | ||
| DB { source: sea_orm::DbErr }, | ||
| #[snafu(display("Failed to save the operation log"))] | ||
| Logger { source: LoggerError }, | ||
| #[snafu(display("Category operation failed"))] | ||
| Category { source: CategoryError }, | ||
| } | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct ContentFolderOperator { | ||
| pub state: AppState, | ||
| pub user: Option<User>, | ||
| } | ||
|
|
||
| impl ContentFolderOperator { | ||
| pub fn new(state: AppState, user: Option<User>) -> Self { | ||
| Self { state, user } | ||
| } | ||
|
|
||
| /// list All child folders for 1 folder | ||
| /// | ||
| /// Should not fail, unless SQLite was corrupted for some reason. | ||
| pub async fn list_child_folders( | ||
| &self, | ||
| content_folder_id: i32, | ||
| ) -> Result<Vec<Model>, ContentFolderError> { | ||
| Entity::find() | ||
| .filter(Column::ParentId.eq(content_folder_id)) | ||
| .all(&self.state.database) | ||
| .await | ||
| .context(DBSnafu) | ||
| } | ||
|
|
||
| /// Find one Content Folder by path | ||
| /// | ||
| /// Should not fail, unless SQLite was corrupted for some reason. | ||
| pub async fn find_by_path(&self, path: String) -> Result<Model, ContentFolderError> { | ||
| let content_folder = Entity::find_by_path(path.clone()) | ||
| .one(&self.state.database) | ||
| .await | ||
| .context(DBSnafu)?; | ||
|
|
||
| match content_folder { | ||
| Some(category) => Ok(category), | ||
| None => Err(ContentFolderError::NotFound { path }), | ||
| } | ||
| } | ||
|
|
||
| /// Find one Content Folder by ID | ||
| /// | ||
| /// Should not fail, unless SQLite was corrupted for some reason. | ||
| pub async fn find_by_id(&self, id: i32) -> Result<Model, ContentFolderError> { | ||
| let content_folder = Entity::find_by_id(id) | ||
| .one(&self.state.database) | ||
| .await | ||
| .context(DBSnafu)?; | ||
|
|
||
| match content_folder { | ||
| Some(category) => Ok(category), | ||
| None => Err(ContentFolderError::NotFound { | ||
| path: id.to_string(), | ||
| }), | ||
| } | ||
| } | ||
|
|
||
| /// Create a new content folder | ||
| /// | ||
| /// Fails if: | ||
| /// | ||
| /// - name or path is already taken (they should be unique in one folder) | ||
| /// - path parent directory does not exist (to avoid completely wrong paths) | ||
| pub async fn create( | ||
| &self, | ||
| f: &ContentFolderForm, | ||
| user: Option<User>, | ||
| ) -> Result<Model, ContentFolderError> { | ||
| // Check duplicates in same folder | ||
| let list = if let Some(parent_id) = f.parent_id { | ||
| self.list_child_folders(parent_id).await? | ||
| } else { | ||
| let category = CategoryOperator::new(self.state.clone(), None); | ||
| category | ||
| .list_folders(f.category_id) | ||
| .await | ||
| .context(CategorySnafu)? | ||
| }; | ||
|
|
||
| if list.iter().any(|x| x.name == f.name) { | ||
| return Err(ContentFolderError::NameTaken { | ||
| name: f.name.clone(), | ||
| }); | ||
| } | ||
|
|
||
| if list.iter().any(|x| x.path == f.path) { | ||
| return Err(ContentFolderError::PathTaken { | ||
| path: f.path.clone(), | ||
| }); | ||
| } | ||
|
|
||
| let model = ActiveModel { | ||
| name: Set(f.name.clone()), | ||
| path: Set(f.path.clone()), | ||
| category_id: Set(f.category_id), | ||
| parent_id: Set(f.parent_id), | ||
| ..Default::default() | ||
| } | ||
| .save(&self.state.database) | ||
| .await | ||
| .context(DBSnafu)?; | ||
|
|
||
| // Should not fail | ||
| let model = model.try_into_model().unwrap(); | ||
|
|
||
| let operation_log = OperationLog { | ||
| user, | ||
| date: Utc::now(), | ||
| table: Table::ContentFolder, | ||
| operation: OperationType::Create, | ||
| operation_id: OperationId { | ||
| object_id: model.id.to_owned(), | ||
| name: f.name.to_string(), | ||
| }, | ||
| operation_form: Some(Operation::ContentFolder(f.clone())), | ||
| }; | ||
|
|
||
| self.state | ||
| .logger | ||
| .write(operation_log) | ||
| .await | ||
| .context(LoggerSnafu)?; | ||
|
|
||
| Ok(model) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| // sea_orm example: https://github.com/SeaQL/sea-orm/blob/master/examples/axum_example/ | ||
| pub mod category; | ||
| pub mod content_folder; | ||
| pub mod operation; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| use sea_orm_migration::{prelude::*, schema::*}; | ||
|
|
||
| use super::m20251110_01_create_table_category::Category; | ||
|
|
||
| #[derive(DeriveMigrationName)] | ||
| pub struct Migration; | ||
|
|
||
| #[async_trait::async_trait] | ||
| impl MigrationTrait for Migration { | ||
| async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
| manager | ||
| .create_table( | ||
| Table::create() | ||
| .table(ContentFolder::Table) | ||
| .if_not_exists() | ||
| .col(pk_auto(ContentFolder::Id)) | ||
| .col(string(ContentFolder::Name)) | ||
| .col(string(ContentFolder::Path)) | ||
| .col( | ||
| ColumnDef::new(ContentFolder::CategoryId) | ||
| .integer() | ||
| .not_null(), | ||
| ) | ||
| .foreign_key( | ||
| ForeignKey::create() | ||
| .name("fk-content-file-category_id") | ||
| .from(ContentFolder::Table, ContentFolder::CategoryId) | ||
| .to(Category::Table, Category::Id), | ||
| ) | ||
| .col(ColumnDef::new(ContentFolder::ParentId).integer()) | ||
| .foreign_key( | ||
| ForeignKey::create() | ||
| .name("fk-content-folder-parent_id") | ||
| .from(ContentFolder::ParentId, ContentFolder::ParentId) | ||
| .to(ContentFolder::Table, ContentFolder::Id), | ||
| ) | ||
| .to_owned(), | ||
| ) | ||
| .await | ||
| } | ||
|
|
||
| async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
| manager | ||
| .drop_table(Table::drop().table(ContentFolder::Table).to_owned()) | ||
| .await | ||
| } | ||
| } | ||
|
|
||
| #[derive(DeriveIden)] | ||
| pub enum ContentFolder { | ||
| Table, | ||
| Id, | ||
| Name, | ||
| Path, | ||
| CategoryId, | ||
| ParentId, | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should make sure we cannot build a Model with a name that contains
/. So this should be aNormalizedPathComponent.