@@ -15,6 +15,7 @@ fn main() {
1515 . post ( todos_create)
1616 . append (
1717 Route :: new ( "<id:uuid>" )
18+ . get ( todos_one)
1819 . patch ( todos_update)
1920 . delete ( todos_delete) ,
2021 ) ;
@@ -26,12 +27,8 @@ struct MiddleWare {
2627}
2728
2829#[ async_trait]
29- impl Handler for MiddleWare {
30- async fn middleware_call (
31- & self ,
32- req : & mut Request ,
33- _res : & mut Response ,
34- ) -> Result < ( ) , SilentError > {
30+ impl MiddleWareHandler for MiddleWare {
31+ async fn pre_request ( & self , req : & mut Request , _res : & mut Response ) -> Result < ( ) > {
3532 req. extensions_mut ( ) . insert ( self . db . clone ( ) ) ;
3633 Ok ( ( ) )
3734 }
@@ -43,7 +40,7 @@ pub struct Pagination {
4340 pub limit : Option < usize > ,
4441}
4542
46- async fn todos_index ( mut req : Request ) -> Result < Vec < Todo > , SilentError > {
43+ async fn todos_index ( mut req : Request ) -> Result < Vec < Todo > > {
4744 let pagination = req. params_parse :: < Pagination > ( ) ?;
4845
4946 let db = req. extensions ( ) . get :: < Db > ( ) . unwrap ( ) ;
@@ -64,7 +61,7 @@ struct CreateTodo {
6461 text : String ,
6562}
6663
67- async fn todos_create ( mut req : Request ) -> Result < Todo , SilentError > {
64+ async fn todos_create ( mut req : Request ) -> Result < Todo > {
6865 let create_todo = req. json_parse :: < CreateTodo > ( ) . await ?;
6966 let db = req. extensions ( ) . get :: < Db > ( ) . unwrap ( ) ;
7067
@@ -85,53 +82,55 @@ struct UpdateTodo {
8582 completed : Option < bool > ,
8683}
8784
88- async fn todos_update ( mut req : Request ) -> Result < Todo , SilentError > {
85+ async fn todos_update ( mut req : Request ) -> Result < Todo > {
8986 let input = req. json_parse :: < UpdateTodo > ( ) . await ?;
9087 let db = req. extensions ( ) . get :: < Db > ( ) . unwrap ( ) ;
91- let id = req. get_path_params ( "id" ) . unwrap ( ) ;
92- if let PathParam :: UUid ( id) = id {
93- let todo = db. read ( ) . unwrap ( ) . get ( id) . cloned ( ) ;
88+ let id: Uuid = req. get_path_params ( "id" ) ?;
89+ let todo = db. read ( ) . unwrap ( ) . get ( & id) . cloned ( ) ;
9490
95- if todo. is_none ( ) {
96- return Err ( SilentError :: BusinessError {
97- code : StatusCode :: NOT_FOUND ,
98- msg : "Not Found" . to_string ( ) ,
99- } ) ;
100- }
91+ if todo. is_none ( ) {
92+ return Err ( SilentError :: BusinessError {
93+ code : StatusCode :: NOT_FOUND ,
94+ msg : "Not Found" . to_string ( ) ,
95+ } ) ;
96+ }
10197
102- let mut todo = todo. unwrap ( ) ;
98+ let mut todo = todo. unwrap ( ) ;
10399
104- if let Some ( text) = input. text {
105- todo. text = text;
106- }
100+ if let Some ( text) = input. text {
101+ todo. text = text;
102+ }
107103
108- if let Some ( completed) = input. completed {
109- todo. completed = completed;
110- }
104+ if let Some ( completed) = input. completed {
105+ todo. completed = completed;
106+ }
111107
112- db. write ( ) . unwrap ( ) . insert ( todo. id , todo. clone ( ) ) ;
108+ db. write ( ) . unwrap ( ) . insert ( todo. id , todo. clone ( ) ) ;
113109
114- Ok ( todo)
115- } else {
116- Err ( SilentError :: BusinessError {
110+ Ok ( todo)
111+ }
112+
113+ async fn todos_one ( req : Request ) -> Result < Todo > {
114+ let db = req. extensions ( ) . get :: < Db > ( ) . unwrap ( ) ;
115+ let id: Uuid = req. get_path_params ( "id" ) ?;
116+ let todo = db. read ( ) . unwrap ( ) . get ( & id) . cloned ( ) ;
117+
118+ if todo. is_none ( ) {
119+ return Err ( SilentError :: BusinessError {
117120 code : StatusCode :: NOT_FOUND ,
118121 msg : "Not Found" . to_string ( ) ,
119- } )
122+ } ) ;
120123 }
124+
125+ let todo = todo. unwrap ( ) ;
126+ Ok ( todo)
121127}
122128
123- async fn todos_delete ( req : Request ) -> Result < ( ) , SilentError > {
129+ async fn todos_delete ( req : Request ) -> Result < ( ) > {
124130 let db = req. extensions ( ) . get :: < Db > ( ) . unwrap ( ) ;
125- let id = req. get_path_params ( "id" ) . unwrap ( ) ;
126- if let PathParam :: UUid ( id) = id {
127- if db. write ( ) . unwrap ( ) . remove ( id) . is_some ( ) {
128- Ok ( ( ) )
129- } else {
130- Err ( SilentError :: BusinessError {
131- code : StatusCode :: NOT_FOUND ,
132- msg : "Not Found" . to_string ( ) ,
133- } )
134- }
131+ let id = req. get_path_params ( "id" ) ?;
132+ if db. write ( ) . unwrap ( ) . remove ( & id) . is_some ( ) {
133+ Ok ( ( ) )
135134 } else {
136135 Err ( SilentError :: BusinessError {
137136 code : StatusCode :: NOT_FOUND ,
0 commit comments