Skip to content

Commit 822ef09

Browse files
committed
feat: add Content Folder and category show
1 parent 63dbc35 commit 822ef09

21 files changed

+925
-12
lines changed

src/database/category.rs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::state::logger::LoggerError;
1414
///
1515
/// Each category has a name and an associated path on disk, where
1616
/// symlinks to the content will be created.
17+
#[sea_orm::model]
1718
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
1819
#[sea_orm(table_name = "category")]
1920
pub struct Model {
@@ -23,11 +24,10 @@ pub struct Model {
2324
pub name: String,
2425
#[sea_orm(unique)]
2526
pub path: String,
27+
#[sea_orm(has_many)]
28+
pub content_folders: HasMany<super::content_folder::Entity>,
2629
}
2730

28-
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
29-
pub enum Relation {}
30-
3131
#[async_trait::async_trait]
3232
impl ActiveModelBehavior for ActiveModel {}
3333

@@ -44,8 +44,8 @@ pub enum CategoryError {
4444
IO { source: std::io::Error },
4545
#[snafu(display("Database error"))]
4646
DB { source: sea_orm::DbErr },
47-
#[snafu(display("The category (ID: {id}) does not exist"))]
48-
NotFound { id: i32 },
47+
#[snafu(display("The category ({details}) does not exist"))]
48+
NotFound { details: String },
4949
#[snafu(display("Failed to save the operation log"))]
5050
Logger { source: LoggerError },
5151
}
@@ -71,6 +71,37 @@ impl CategoryOperator {
7171
.context(DBSnafu)
7272
}
7373

74+
/// Find one category by ID
75+
pub async fn find_by_id(&self, id: i32) -> Result<Model, CategoryError> {
76+
let category = Entity::find_by_id(id)
77+
.one(&self.state.database)
78+
.await
79+
.context(DBSnafu)?;
80+
81+
match category {
82+
Some(category) => Ok(category),
83+
None => Err(CategoryError::NotFound {
84+
details: format!("ID: {}", id),
85+
}),
86+
}
87+
}
88+
89+
/// Find one category by Name
90+
pub async fn find_by_name(&self, name: String) -> Result<Model, CategoryError> {
91+
let category = Entity::find()
92+
.filter(Column::Name.contains(name.clone()))
93+
.one(&self.state.database)
94+
.await
95+
.context(DBSnafu)?;
96+
97+
match category {
98+
Some(category) => Ok(category),
99+
None => Err(CategoryError::NotFound {
100+
details: format!("NAME: {}", name),
101+
}),
102+
}
103+
}
104+
74105
/// Delete a category
75106
pub async fn delete(&self, id: i32, user: Option<User>) -> Result<String, CategoryError> {
76107
let db = &self.state.database;
@@ -101,7 +132,9 @@ impl CategoryOperator {
101132

102133
Ok(category_clone.name)
103134
}
104-
None => Err(CategoryError::NotFound { id }),
135+
None => Err(CategoryError::NotFound {
136+
details: format!("ID: {}", id),
137+
}),
105138
}
106139
}
107140

@@ -139,9 +172,11 @@ impl CategoryOperator {
139172
});
140173
}
141174

175+
// Normalized path to avoid trailing slash
176+
let normalized_path = dir.components().collect::<Utf8PathBuf>();
142177
let model = ActiveModel {
143178
name: Set(f.name.clone()),
144-
path: Set(f.path.clone()),
179+
path: Set(normalized_path.into_string()),
145180
..Default::default()
146181
}
147182
.save(&self.state.database)

src/database/content_folder.rs

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
use chrono::Utc;
2+
use sea_orm::entity::prelude::*;
3+
use sea_orm::*;
4+
use snafu::prelude::*;
5+
6+
use crate::database::category;
7+
use crate::database::operation::{Operation, OperationId, OperationLog, OperationType, Table};
8+
use crate::extractors::user::User;
9+
use crate::routes::content_folder::ContentFolderForm;
10+
use crate::state::AppState;
11+
use crate::state::logger::LoggerError;
12+
13+
/// A content folder to store associated files.
14+
///
15+
/// Each content folder has a name and an associated path on disk, a Category
16+
/// and it can have an Parent Content Folder (None if it's the first folder
17+
/// in category)
18+
#[sea_orm::model]
19+
#[derive(DeriveEntityModel, Clone, Debug, PartialEq, Eq)]
20+
#[sea_orm(table_name = "content_folder")]
21+
pub struct Model {
22+
#[sea_orm(primary_key)]
23+
pub id: i32,
24+
pub name: String,
25+
#[sea_orm(unique)]
26+
pub path: String,
27+
pub category_id: i32,
28+
#[sea_orm(belongs_to, from = "category_id", to = "id")]
29+
pub category: HasOne<category::Entity>,
30+
pub parent_id: Option<i32>,
31+
#[sea_orm(self_ref, relation_enum = "Parent", from = "parent_id", to = "id")]
32+
pub parent: HasOne<Entity>,
33+
}
34+
35+
#[async_trait::async_trait]
36+
impl ActiveModelBehavior for ActiveModel {}
37+
38+
#[derive(Debug, Snafu)]
39+
#[snafu(visibility(pub))]
40+
pub enum ContentFolderError {
41+
#[snafu(display("There is already a content folder called `{name}`"))]
42+
NameTaken { name: String },
43+
#[snafu(display("There is already a content folder in dir `{path}`"))]
44+
PathTaken { path: String },
45+
#[snafu(display("The Content Folder (Path: {path}) does not exist"))]
46+
NotFound { path: String },
47+
#[snafu(display("Database error"))]
48+
DB { source: sea_orm::DbErr },
49+
#[snafu(display("Failed to save the operation log"))]
50+
Logger { source: LoggerError },
51+
}
52+
53+
#[derive(Clone, Debug)]
54+
pub struct ContentFolderOperator {
55+
pub state: AppState,
56+
pub user: Option<User>,
57+
}
58+
59+
impl ContentFolderOperator {
60+
pub fn new(state: AppState, user: Option<User>) -> Self {
61+
Self { state, user }
62+
}
63+
64+
/// List Content Folders
65+
///
66+
/// Should not fail, unless SQLite was corrupted for some reason.
67+
pub async fn list_by_parent_and_category(
68+
&self,
69+
parent_id: Option<i32>,
70+
category_id: i32,
71+
) -> Result<Vec<Model>, ContentFolderError> {
72+
let mut query = Entity::find().filter(Column::CategoryId.eq(category_id));
73+
// parent_id can be None when it's the first folder in categories
74+
match parent_id {
75+
Some(parent_id) => query = query.filter(Column::ParentId.eq(parent_id)),
76+
None => query = query.filter(Column::ParentId.is_null()),
77+
}
78+
79+
query.all(&self.state.database).await.context(DBSnafu)
80+
}
81+
82+
/// Find one Content Folder by path
83+
///
84+
/// Should not fail, unless SQLite was corrupted for some reason.
85+
pub async fn find_by_path(&self, path: String) -> Result<Model, ContentFolderError> {
86+
let content_folder = Entity::find_by_path(path.clone())
87+
.one(&self.state.database)
88+
.await
89+
.context(DBSnafu)?;
90+
91+
match content_folder {
92+
Some(category) => Ok(category),
93+
None => Err(ContentFolderError::NotFound { path }),
94+
}
95+
}
96+
97+
/// Find one Content Folder by ID
98+
///
99+
/// Should not fail, unless SQLite was corrupted for some reason.
100+
pub async fn find_by_id(&self, id: i32) -> Result<Model, ContentFolderError> {
101+
let content_folder = Entity::find_by_id(id)
102+
.one(&self.state.database)
103+
.await
104+
.context(DBSnafu)?;
105+
106+
match content_folder {
107+
Some(category) => Ok(category),
108+
None => Err(ContentFolderError::NotFound {
109+
path: id.to_string(),
110+
}),
111+
}
112+
}
113+
114+
/// Create a new content folder
115+
///
116+
/// Fails if:
117+
///
118+
/// - name or path is already taken (they should be unique in one folder)
119+
/// - path parent directory does not exist (to avoid completely wrong paths)
120+
pub async fn create(
121+
&self,
122+
f: &ContentFolderForm,
123+
user: Option<User>,
124+
) -> Result<Model, ContentFolderError> {
125+
// Check duplicates in same folder
126+
let list = self
127+
.list_by_parent_and_category(f.parent_id, f.category_id)
128+
.await?;
129+
130+
if list.iter().any(|x| x.name == f.name) {
131+
return Err(ContentFolderError::NameTaken {
132+
name: f.name.clone(),
133+
});
134+
}
135+
136+
if list.iter().any(|x| x.path == f.path) {
137+
return Err(ContentFolderError::PathTaken {
138+
path: f.path.clone(),
139+
});
140+
}
141+
142+
let model = ActiveModel {
143+
name: Set(f.name.clone()),
144+
path: Set(f.path.clone()),
145+
category_id: Set(f.category_id),
146+
parent_id: Set(f.parent_id),
147+
..Default::default()
148+
}
149+
.save(&self.state.database)
150+
.await
151+
.context(DBSnafu)?;
152+
153+
// Should not fail
154+
let model = model.try_into_model().unwrap();
155+
156+
let operation_log = OperationLog {
157+
user,
158+
date: Utc::now(),
159+
table: Table::ContentFolder,
160+
operation: OperationType::Create,
161+
operation_id: OperationId {
162+
object_id: model.id.to_owned(),
163+
name: f.name.to_string(),
164+
},
165+
operation_form: Some(Operation::ContentFolder(f.clone())),
166+
};
167+
168+
self.state
169+
.logger
170+
.write(operation_log)
171+
.await
172+
.context(LoggerSnafu)?;
173+
174+
Ok(model)
175+
}
176+
}

src/database/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
// sea_orm example: https://github.com/SeaQL/sea-orm/blob/master/examples/axum_example/
22
pub mod category;
3+
pub mod content_folder;
34
pub mod operation;

src/database/operation.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
44

55
use crate::extractors::user::User;
66
use crate::routes::category::CategoryForm;
7+
use crate::routes::content_folder::ContentFolderForm;
78

89
/// Type of operation applied to the database.
910
#[derive(Clone, Debug, Display, Serialize, Deserialize)]
@@ -22,6 +23,7 @@ pub struct OperationId {
2223
#[derive(Clone, Debug, Display, Serialize, Deserialize)]
2324
pub enum Table {
2425
Category,
26+
ContentFolder,
2527
}
2628

2729
/// Operation applied to the database.
@@ -31,6 +33,7 @@ pub enum Table {
3133
#[serde(untagged)]
3234
pub enum Operation {
3335
Category(CategoryForm),
36+
ContentFolder(ContentFolderForm),
3437
}
3538

3639
impl std::fmt::Display for Operation {

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,18 @@ pub fn router(state: state::AppState) -> Router {
2020
Router::new()
2121
// Register dynamic routes
2222
.route("/", get(routes::index::index))
23+
.route("/file-system", get(routes::index::file_system))
2324
.route("/progress/{view_request}", get(routes::progress::progress))
2425
.route("/categories", get(routes::category::index))
2526
.route("/categories", post(routes::category::create))
2627
.route("/categories/new", get(routes::category::new))
2728
.route("/categories/{id}/delete", get(routes::category::delete))
29+
.route("/folders/{category_id}", get(routes::category::show))
30+
.route(
31+
"/folders/{category_name}/{*folder_path}",
32+
get(routes::content_folder::show),
33+
)
34+
.route("/folders", post(routes::content_folder::create))
2835
.route("/logs", get(routes::logs::index))
2936
// Register static assets routes
3037
.nest("/assets", static_router())

src/migration/m20251110_01_create_table_category.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl MigrationTrait for Migration {
2727
}
2828

2929
#[derive(DeriveIden)]
30-
enum Category {
30+
pub enum Category {
3131
Table,
3232
Id,
3333
Name,
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use sea_orm_migration::{prelude::*, schema::*};
2+
3+
use super::m20251110_01_create_table_category::Category;
4+
5+
#[derive(DeriveMigrationName)]
6+
pub struct Migration;
7+
8+
#[async_trait::async_trait]
9+
impl MigrationTrait for Migration {
10+
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
11+
manager
12+
.create_table(
13+
Table::create()
14+
.table(ContentFolder::Table)
15+
.if_not_exists()
16+
.col(pk_auto(ContentFolder::Id))
17+
.col(string(ContentFolder::Name))
18+
.col(string(ContentFolder::Path))
19+
.col(
20+
ColumnDef::new(ContentFolder::CategoryId)
21+
.integer()
22+
.not_null(),
23+
)
24+
.foreign_key(
25+
ForeignKey::create()
26+
.name("fk-content-file-category_id")
27+
.from(ContentFolder::Table, ContentFolder::CategoryId)
28+
.to(Category::Table, Category::Id),
29+
)
30+
.col(ColumnDef::new(ContentFolder::ParentId).integer())
31+
.foreign_key(
32+
ForeignKey::create()
33+
.name("fk-content-folder-parent_id")
34+
.from(ContentFolder::ParentId, ContentFolder::ParentId)
35+
.to(ContentFolder::Table, ContentFolder::Id),
36+
)
37+
.to_owned(),
38+
)
39+
.await
40+
}
41+
42+
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
43+
manager
44+
.drop_table(Table::drop().table(ContentFolder::Table).to_owned())
45+
.await
46+
}
47+
}
48+
49+
#[derive(DeriveIden)]
50+
pub enum ContentFolder {
51+
Table,
52+
Id,
53+
Name,
54+
Path,
55+
CategoryId,
56+
ParentId,
57+
}

0 commit comments

Comments
 (0)