@@ -2,13 +2,15 @@ use askama::Template;
22use askama_web:: WebTemplate ;
33use axum:: Form ;
44use axum:: extract:: { Path , State } ;
5- use axum:: response:: IntoResponse ;
5+ use axum:: response:: { IntoResponse , Redirect } ;
6+ use axum_extra:: extract:: CookieJar ;
67use serde:: { Deserialize , Serialize } ;
78use snafu:: prelude:: * ;
89
910use crate :: database:: category:: CategoryError ;
1011use crate :: database:: { category, category:: CategoryOperator } ;
1112use crate :: extractors:: user:: User ;
13+ use crate :: state:: flash_message:: { OperationStatus , get_cookie} ;
1214use crate :: state:: { AppState , AppStateContext , error:: * } ;
1315
1416#[ derive( Clone , Debug , Deserialize , Serialize ) ]
@@ -17,13 +19,6 @@ pub struct CategoryForm {
1719 pub path : String ,
1820}
1921
20- pub struct OperationStatus {
21- /// Status of operation
22- pub success : bool ,
23- /// Message for confirmation alert
24- pub message : String ,
25- }
26-
2722#[ derive( Template , WebTemplate ) ]
2823#[ template( path = "categories/index.html" ) ]
2924pub struct CategoriesTemplate {
@@ -34,7 +29,7 @@ pub struct CategoriesTemplate {
3429 /// Logged-in user.
3530 pub user : Option < User > ,
3631 /// Operation status for UI confirmation
37- pub operation_status : Option < OperationStatus > ,
32+ pub flash : Option < OperationStatus > ,
3833}
3934
4035#[ derive( Template , WebTemplate ) ]
@@ -68,37 +63,33 @@ pub async fn delete(
6863 State ( app_state) : State < AppState > ,
6964 user : Option < User > ,
7065 Path ( id) : Path < i32 > ,
66+ jar : CookieJar ,
7167) -> Result < impl axum:: response:: IntoResponse , AppStateError > {
72- let app_state_context = app_state. context ( ) . await ?;
68+ // let app_state_context = app_state.context().await?;
7369 let categories = CategoryOperator :: new ( app_state. clone ( ) , user. clone ( ) ) ;
7470
7571 let deleted = categories. delete ( id, user. clone ( ) ) . await ;
7672
77- match deleted {
78- Ok ( name) => Ok ( CategoriesTemplate {
79- categories : categories. list ( ) . await . context ( CategorySnafu ) ?,
80- operation_status : Some ( OperationStatus {
81- success : true ,
82- message : format ! ( "The category {} has been successfully deleted" , name) ,
83- } ) ,
84- state : app_state_context,
85- user,
86- } ) ,
87- Err ( error) => Ok ( CategoriesTemplate {
88- categories : categories. list ( ) . await . context ( CategorySnafu ) ?,
89- operation_status : Some ( OperationStatus {
90- success : false ,
91- message : format ! ( "{}" , error) ,
92- } ) ,
93- state : app_state_context,
94- user,
95- } ) ,
96- }
73+ let operation_status = match deleted {
74+ Ok ( name) => OperationStatus {
75+ success : true ,
76+ message : format ! ( "The category {} has been successfully deleted" , name) ,
77+ } ,
78+ Err ( error) => OperationStatus {
79+ success : false ,
80+ message : format ! ( "{}" , error) ,
81+ } ,
82+ } ;
83+
84+ let jar = operation_status. set_cookie ( jar) ;
85+
86+ Ok ( ( jar, Redirect :: to ( "/categories" ) ) )
9787}
9888
9989pub async fn create (
10090 State ( app_state) : State < AppState > ,
10191 user : Option < User > ,
92+ jar : CookieJar ,
10293 Form ( form) : Form < CategoryForm > ,
10394) -> Result < impl axum:: response:: IntoResponse , AppStateError > {
10495 let app_state_context = app_state. context ( ) . await ?;
@@ -107,40 +98,49 @@ pub async fn create(
10798 let created = categories. create ( & form, user. clone ( ) ) . await ;
10899
109100 match created {
110- Ok ( created) => Ok ( CategoriesTemplate {
111- categories : categories. list ( ) . await . context ( CategorySnafu ) ?,
112- state : app_state_context,
113- user,
114- operation_status : Some ( OperationStatus {
101+ Ok ( created) => {
102+ let operation_status = OperationStatus {
115103 success : true ,
116104 message : format ! (
117105 "The category {} has been successfully created (ID {})" ,
118106 created. name, created. id
119107 ) ,
120- } ) ,
121- }
122- . into_response ( ) ) ,
123- Err ( error) => Ok ( NewCategoryTemplate {
124- state : app_state_context,
125- user,
126- category_form : Some ( form) ,
127- error : Some ( error) ,
108+ } ;
109+
110+ let jar = operation_status. set_cookie ( jar) ;
111+
112+ Ok ( ( jar, Redirect :: to ( "/categories" ) . into_response ( ) ) )
128113 }
129- . into_response ( ) ) ,
114+ Err ( error) => Ok ( (
115+ jar,
116+ NewCategoryTemplate {
117+ state : app_state_context,
118+ user,
119+ category_form : Some ( form) ,
120+ error : Some ( error) ,
121+ }
122+ . into_response ( ) ,
123+ ) ) ,
130124 }
131125}
132126
133127pub async fn index (
134128 State ( app_state) : State < AppState > ,
135129 user : Option < User > ,
136- ) -> Result < CategoriesTemplate , AppStateError > {
130+ jar : CookieJar ,
131+ ) -> Result < ( CookieJar , CategoriesTemplate ) , AppStateError > {
137132 let app_state_context = app_state. context ( ) . await ?;
138133 let categories = CategoryOperator :: new ( app_state. clone ( ) , user. clone ( ) ) ;
139134
140- Ok ( CategoriesTemplate {
141- categories : categories. list ( ) . await . context ( CategorySnafu ) ?,
142- state : app_state_context,
143- user,
144- operation_status : None ,
145- } )
135+ let ( jar, operation_status) = get_cookie ( jar) ;
136+
137+ Ok ( (
138+ jar,
139+ CategoriesTemplate {
140+ categories : categories. list ( ) . await . context ( CategorySnafu ) ?,
141+ state : app_state_context,
142+ user,
143+ flash : operation_status,
144+ } ,
145+ ) )
146146}
0 commit comments