Skip to content

Commit c3d654e

Browse files
committed
feat: Use normalized paths in category form
1 parent dd8fe20 commit c3d654e

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

src/database/category.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use sea_orm::*;
55
use snafu::prelude::*;
66

77
use crate::database::operation::*;
8+
use crate::extractors::normalized_path::*;
89
use crate::extractors::user::User;
910
use crate::routes::category::CategoryForm;
1011
use crate::state::AppState;
@@ -20,9 +21,9 @@ pub struct Model {
2021
#[sea_orm(primary_key)]
2122
pub id: i32,
2223
#[sea_orm(unique)]
23-
pub name: String,
24+
pub name: NormalizedPathComponent,
2425
#[sea_orm(unique)]
25-
pub path: String,
26+
pub path: NormalizedPathAbsolute,
2627
}
2728

2829
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
@@ -88,7 +89,7 @@ impl CategoryOperator {
8889
operation: OperationType::Delete,
8990
operation_id: OperationId {
9091
object_id: category_clone.id,
91-
name: category_clone.name.to_owned(),
92+
name: category_clone.name.to_string(),
9293
},
9394
operation_form: None,
9495
};
@@ -99,7 +100,7 @@ impl CategoryOperator {
99100
.await
100101
.context(LoggerSnafu)?;
101102

102-
Ok(category_clone.name)
103+
Ok(category_clone.name.to_string())
103104
}
104105
None => Err(CategoryError::NotFound { id }),
105106
}
@@ -130,12 +131,12 @@ impl CategoryOperator {
130131

131132
if list.iter().any(|x| x.name == f.name) {
132133
return Err(CategoryError::NameTaken {
133-
name: f.name.clone(),
134+
name: f.name.to_string(),
134135
});
135136
}
136137
if list.iter().any(|x| x.path == f.path) {
137138
return Err(CategoryError::PathTaken {
138-
path: f.path.clone(),
139+
path: f.path.to_string(),
139140
});
140141
}
141142

src/routes/category.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ use snafu::prelude::*;
99

1010
use crate::database::category::CategoryError;
1111
use crate::database::{category, category::CategoryOperator};
12+
use crate::extractors::normalized_path::*;
1213
use crate::extractors::user::User;
1314
use crate::state::flash_message::{OperationStatus, get_cookie};
1415
use crate::state::{AppState, AppStateContext, error::*};
1516

1617
#[derive(Clone, Debug, Deserialize, Serialize)]
1718
pub struct CategoryForm {
18-
pub name: String,
19-
pub path: String,
19+
pub name: NormalizedPathComponent,
20+
pub path: NormalizedPathAbsolute,
2021
}
2122

2223
#[derive(Template, WebTemplate)]

src/routes/index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl IndexTemplate {
2626
.await
2727
.context(CategorySnafu)?
2828
.into_iter()
29-
.map(|x| x.name)
29+
.map(|x| x.name.to_string())
3030
.collect();
3131

3232
Ok(IndexTemplate {

src/state/logger.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ mod tests {
152152
object_id: 1,
153153
},
154154
operation_form: Some(Operation::Category(CategoryForm {
155-
name: "object".to_string(),
156-
path: "path".to_string(),
155+
name: NormalizedPathComponent::from_str("object").unwrap(),
156+
path: NormalizedPathAbsolute::from_str("/path").unwrap(),
157157
})),
158158
};
159159

@@ -192,8 +192,8 @@ mod tests {
192192
object_id: 1,
193193
},
194194
operation_form: Some(Operation::Category(CategoryForm {
195-
name: "object".to_string(),
196-
path: "path".to_string(),
195+
name: NormalizedPathComponent::from_str("object").unwrap(),
196+
path: NormalizedPathAbsolute::from_str("/path").unwrap(),
197197
})),
198198
};
199199

0 commit comments

Comments
 (0)