Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit81f1bbf

Browse files
committed
updated
1 parent8a8259e commit81f1bbf

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

‎src/handler.rs‎

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,14 @@ async fn create_note_handler(
8585
}
8686

8787
#[get("/notes/{id}")]
88-
asyncfnget_note_handler(path: web::Path<String>,data: web::Data<AppState>) ->implResponder{
88+
asyncfnget_note_handler(
89+
path: web::Path<uuid::Uuid>,
90+
data: web::Data<AppState>,
91+
) ->implResponder{
8992
let 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

9697
match query_result{
9798
Ok(note) =>{
@@ -111,16 +112,14 @@ async fn get_note_handler(path: web::Path<String>, data: web::Data<AppState>) ->
111112

112113
#[patch("/notes/{id}")]
113114
asyncfnedit_note_handler(
114-
path: web::Path<String>,
115+
path: web::Path<uuid::Uuid>,
115116
body: web::Json<UpdateNoteSchema>,
116117
data: web::Data<AppState>,
117118
) ->implResponder{
118119
let 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

125124
if query_result.is_err(){
126125
let 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

154153
returnHttpResponse::Ok().json(note_response);
155154
}
156-
Err(_) =>{
157-
let message =format!("Note with ID: {} not found",note_id);
158-
returnHttpResponse::NotFound()
159-
.json(serde_json::json!({"status":"fail","message": message}));
155+
Err(err) =>{
156+
let message =format!("Error: {:?}",err);
157+
returnHttpResponse::InternalServerError()
158+
.json(serde_json::json!({"status":"error","message": message}));
160159
}
161160
}
162161
}
163162

164163
#[delete("/notes/{id}")]
165-
asyncfndelete_note_handler(path: web::Path<String>,data: web::Data<AppState>) ->implResponder{
164+
asyncfndelete_note_handler(
165+
path: web::Path<uuid::Uuid>,
166+
data: web::Data<AppState>,
167+
) ->implResponder{
166168
let 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+
returnHttpResponse::NotFound().json(json!({"status":"fail","message": message}));
178178
}
179+
180+
HttpResponse::NoContent().finish()
179181
}
180182

181183
pubfnconfig(conf:&mut web::ServiceConfig){

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp