@@ -2,17 +2,17 @@ use askama::Template;
22use askama_web:: WebTemplate ;
33use axum:: Form ;
44use axum:: extract:: Path ;
5- use axum:: response:: { IntoResponse , Redirect } ;
65use axum_extra:: extract:: CookieJar ;
76use serde:: { Deserialize , Serialize } ;
87use snafu:: prelude:: * ;
98
109use crate :: database:: category;
11- use crate :: database:: category:: CategoryError ;
1210use crate :: database:: content_folder:: PathBreadcrumb ;
1311use crate :: extractors:: normalized_path:: * ;
1412use crate :: filesystem:: FileSystemEntry ;
15- use crate :: state:: flash_message:: { OperationStatus , get_cookie} ;
13+ use crate :: state:: flash_message:: {
14+ FallibleTemplate , FlashRedirect , FlashTemplate , OperationStatus , StatusCookie ,
15+ } ;
1616use crate :: state:: { AppStateContext , error:: * } ;
1717
1818#[ derive( Clone , Debug , Deserialize , Serialize ) ]
@@ -26,73 +26,50 @@ pub struct CategoryForm {
2626pub struct NewCategoryTemplate {
2727 /// Global application state
2828 pub state : AppStateContext ,
29- /// Error
30- pub error : Option < CategoryError > ,
3129 /// Default form with value
3230 pub category_form : Option < CategoryForm > ,
3331}
3432
35- pub async fn new (
36- app_state_context : AppStateContext ,
37- ) -> Result < impl axum:: response:: IntoResponse , AppStateError > {
38- Ok ( NewCategoryTemplate {
33+ pub async fn new ( app_state_context : AppStateContext ) -> NewCategoryTemplate {
34+ NewCategoryTemplate {
3935 state : app_state_context,
4036 category_form : None ,
41- error : None ,
42- } )
37+ }
4338}
4439
4540pub async fn delete (
4641 context : AppStateContext ,
4742 Path ( id) : Path < i32 > ,
4843 jar : CookieJar ,
49- ) -> Result < impl axum:: response:: IntoResponse , AppStateError > {
50- let operation_status = match context. db . category ( ) . delete ( id) . await {
51- Ok ( name) => OperationStatus {
52- success : true ,
53- message : format ! ( "The category {} has been successfully deleted" , name) ,
54- } ,
55- Err ( error) => OperationStatus {
56- success : false ,
57- message : format ! ( "{}" , error) ,
58- } ,
44+ ) -> FlashRedirect {
45+ let status = match context. db . category ( ) . delete ( id) . await {
46+ Ok ( name) => StatusCookie :: success (
47+ jar,
48+ format ! ( "The category {} has been successfully deleted" , name) ,
49+ ) ,
50+ Err ( error) => StatusCookie :: error ( jar, error. to_string ( ) ) ,
5951 } ;
6052
61- let jar = operation_status. set_cookie ( jar) ;
62-
63- Ok ( ( jar, Redirect :: to ( "/categories" ) ) )
53+ status. redirect ( "/categories" )
6454}
6555
6656pub async fn create (
6757 context : AppStateContext ,
6858 jar : CookieJar ,
6959 Form ( form) : Form < CategoryForm > ,
70- ) -> Result < impl axum:: response:: IntoResponse , AppStateError > {
71- match context. db . category ( ) . create ( & form) . await {
72- Ok ( created) => {
73- let operation_status = OperationStatus {
74- success : true ,
75- message : format ! (
76- "The category {} has been successfully created (ID {})" ,
77- created. name, created. id
78- ) ,
79- } ;
80-
81- let jar = operation_status. set_cookie ( jar) ;
82-
83- Ok ( ( jar, Redirect :: to ( "/" ) . into_response ( ) ) )
84- }
85- Err ( error) => {
86- let operation_status = OperationStatus {
87- success : false ,
88- message : format ! ( "{}" , error) ,
89- } ;
90-
91- let jar = operation_status. set_cookie ( jar) ;
92-
93- Ok ( ( jar, Redirect :: to ( "/" ) . into_response ( ) ) )
94- }
95- }
60+ ) -> FlashRedirect {
61+ let status = match context. db . category ( ) . create ( & form) . await {
62+ Ok ( created) => StatusCookie :: success (
63+ jar,
64+ format ! (
65+ "The category {} has been successfully created (ID {})" ,
66+ created. name, created. id
67+ ) ,
68+ ) ,
69+ Err ( error) => StatusCookie :: error ( jar, error. to_string ( ) ) ,
70+ } ;
71+
72+ status. redirect ( "/" )
9673}
9774
9875#[ derive( Template , WebTemplate ) ]
@@ -110,11 +87,17 @@ pub struct CategoryShowTemplate {
11087 pub breadcrumbs : Vec < PathBreadcrumb > ,
11188}
11289
90+ impl FallibleTemplate for CategoryShowTemplate {
91+ fn with_optional_flash ( & mut self , flash : Option < OperationStatus > ) {
92+ self . flash = flash;
93+ }
94+ }
95+
11396pub async fn show (
11497 context : AppStateContext ,
11598 Path ( category_name) : Path < String > ,
116- jar : CookieJar ,
117- ) -> Result < impl IntoResponse , AppStateError > {
99+ status : StatusCookie ,
100+ ) -> Result < FlashTemplate < CategoryShowTemplate > , AppStateError > {
118101 let categories = context. db . category ( ) ;
119102
120103 let category = categories
@@ -130,18 +113,13 @@ pub async fn show(
130113
131114 let children = FileSystemEntry :: from_content_folders ( & category, & content_folders) ;
132115
133- let ( jar, operation_status) = get_cookie ( jar) ;
134-
135116 let breadcrumbs = PathBreadcrumb :: for_filesystem_path ( category. name . as_str ( ) ) ;
136117
137- Ok ( (
138- jar,
139- CategoryShowTemplate {
140- category,
141- children,
142- state : context,
143- flash : operation_status,
144- breadcrumbs,
145- } ,
146- ) )
118+ Ok ( status. with_template ( CategoryShowTemplate {
119+ category,
120+ children,
121+ state : context,
122+ flash : None ,
123+ breadcrumbs,
124+ } ) )
147125}
0 commit comments