Added selecting specific ticket by id
This commit is contained in:
@@ -1 +1 @@
|
||||
mod ticket;
|
||||
pub mod ticket;
|
||||
|
||||
@@ -89,6 +89,36 @@ pub async fn get_tickets(
|
||||
Ok(Json(json_response))
|
||||
}
|
||||
|
||||
pub async fn get_ticket_by_id(
|
||||
Path(id): Path<i32>,
|
||||
State(data): State<Arc<AppState>>,
|
||||
) -> Result<impl IntoResponse, (StatusCode, Json<serde_json::Value>)> {
|
||||
let query = sqlx::query_as(r#"SELECT * FROM tickets WHERE id = $1"#)
|
||||
.bind(id)
|
||||
.fetch_one(&data.db)
|
||||
.await;
|
||||
|
||||
match query {
|
||||
Ok(ticket) => {
|
||||
let ticket_response = serde_json::json!(filter_record(&ticket));
|
||||
return Ok(Json(ticket_response));
|
||||
}
|
||||
Err(sqlx::Error::RowNotFound) => {
|
||||
let error_response = serde_json::json!({
|
||||
"status": "fail",
|
||||
"message": format!("Ticket with ID {} not found", id)
|
||||
});
|
||||
return Err((StatusCode::NOT_FOUND, Json(error_response)));
|
||||
}
|
||||
Err(e) => {
|
||||
return Err((
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(json!({"status": "error", "message": format!("{:?}", e)})),
|
||||
));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn filter_record(ticket: &Ticket) -> TicketResponse {
|
||||
TicketResponse {
|
||||
id: ticket.id.to_owned(),
|
||||
|
||||
Reference in New Issue
Block a user