@@ -85,13 +85,14 @@ async fn create_note_handler(
8585}
8686
8787#[ get( "/notes/{id}" ) ]
88- async fn get_note_handler ( path : web:: Path < String > , data : web:: Data < AppState > ) ->impl Responder {
88+ async fn get_note_handler (
89+ path : web:: Path < uuid:: Uuid > ,
90+ data : web:: Data < AppState > ,
91+ ) ->impl Responder {
8992let note_id = path. into_inner ( ) ;
90- let note_id_uuid = uuid:: Uuid :: parse_str ( note_id. as_str ( ) ) . unwrap ( ) ;
91- let query_result =
92- sqlx:: query_as!( NoteModel , "SELECT * FROM notes WHERE id = $1" , note_id_uuid)
93- . fetch_one ( & data. db )
94- . await ;
93+ let query_result = sqlx:: query_as!( NoteModel , "SELECT * FROM notes WHERE id = $1" , note_id)
94+ . fetch_one ( & data. db )
95+ . await ;
9596
9697match query_result{
9798Ok ( note) =>{
@@ -111,16 +112,14 @@ async fn get_note_handler(path: web::Path<String>, data: web::Data<AppState>) ->
111112
112113#[ patch( "/notes/{id}" ) ]
113114async fn edit_note_handler (
114- path : web:: Path < String > ,
115+ path : web:: Path < uuid :: Uuid > ,
115116body : web:: Json < UpdateNoteSchema > ,
116117data : web:: Data < AppState > ,
117118) ->impl Responder {
118119let note_id = path. into_inner ( ) ;
119- let note_id_uuid = uuid:: Uuid :: parse_str ( note_id. as_str ( ) ) . unwrap ( ) ;
120- let query_result =
121- sqlx:: query_as!( NoteModel , "SELECT * FROM notes WHERE id = $1" , note_id_uuid)
122- . fetch_one ( & data. db )
123- . await ;
120+ let query_result = sqlx:: query_as!( NoteModel , "SELECT * FROM notes WHERE id = $1" , note_id)
121+ . fetch_one ( & data. db )
122+ . await ;
124123
125124if query_result. is_err ( ) {
126125let message =format ! ( "Note with ID: {} not found" , note_id) ;
@@ -139,7 +138,7 @@ async fn edit_note_handler(
139138 body. category. to_owned( ) . unwrap_or( note. category. unwrap( ) ) ,
140139 body. published. unwrap_or( note. published. unwrap( ) ) ,
141140 now,
142- note_id_uuid
141+ note_id
143142)
144143. fetch_one ( & data. db )
145144. await
@@ -153,29 +152,32 @@ async fn edit_note_handler(
153152
154153return HttpResponse :: Ok ( ) . json ( note_response) ;
155154}
156- Err ( _ ) =>{
157- let message =format ! ( "Note with ID : {} not found " , note_id ) ;
158- return HttpResponse :: NotFound ( )
159- . json ( serde_json:: json!( { "status" : "fail " , "message" : message} ) ) ;
155+ Err ( err ) =>{
156+ let message =format ! ( "Error : {:?} " , err ) ;
157+ return HttpResponse :: InternalServerError ( )
158+ . json ( serde_json:: json!( { "status" : "error " , "message" : message} ) ) ;
160159}
161160}
162161}
163162
164163#[ delete( "/notes/{id}" ) ]
165- async fn delete_note_handler ( path : web:: Path < String > , data : web:: Data < AppState > ) ->impl Responder {
164+ async fn delete_note_handler (
165+ path : web:: Path < uuid:: Uuid > ,
166+ data : web:: Data < AppState > ,
167+ ) ->impl Responder {
166168let note_id = path. into_inner ( ) ;
167- let note_id_uuid = uuid:: Uuid :: parse_str ( note_id. as_str ( ) ) . unwrap ( ) ;
168- let query_result = sqlx:: query!( "DELETE FROM notes WHERE id = $1" , note_id_uuid)
169+ let rows_affected = sqlx:: query!( "DELETE FROM notes WHERE id = $1" , note_id)
169170. execute ( & data. db )
170- . await ;
171+ . await
172+ . unwrap ( )
173+ . rows_affected ( ) ;
171174
172- match query_result{
173- Ok ( _) =>HttpResponse :: NoContent ( ) . finish ( ) ,
174- Err ( _) =>{
175- let message =format ! ( "Note with ID: {} not found" , note_id) ;
176- HttpResponse :: NotFound ( ) . json ( json ! ( { "status" : "fail" , "message" : message} ) )
177- }
175+ if rows_affected ==0 {
176+ let message =format ! ( "Note with ID: {} not found" , note_id) ;
177+ return HttpResponse :: NotFound ( ) . json ( json ! ( { "status" : "fail" , "message" : message} ) ) ;
178178}
179+
180+ HttpResponse :: NoContent ( ) . finish ( )
179181}
180182
181183pub fn config ( conf : & mut web:: ServiceConfig ) {