Added ticket edit functionality

As said, also minor change to database for testing purposes
This commit is contained in:
2026-04-23 20:13:09 +02:00
parent b905ffc9b1
commit 5e643503aa
4 changed files with 56 additions and 5 deletions

View File

@@ -11,7 +11,7 @@ use sqlx::query;
use crate::{
AppState,
models::{Ticket, TicketCreateScheme, TicketResponse},
models::{Ticket, TicketCreateScheme, TicketResponse, TicketUpdateScheme},
};
pub async fn create_ticket(
@@ -93,7 +93,7 @@ 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"#)
let query = sqlx::query_as::<_, Ticket>(r#"SELECT * FROM tickets WHERE id = $1"#)
.bind(id)
.fetch_one(&data.db)
.await;
@@ -119,6 +119,50 @@ pub async fn get_ticket_by_id(
};
}
pub async fn edit_ticket(
Path(id): Path<i32>,
State(data): State<Arc<AppState>>,
Json(body): Json<TicketUpdateScheme>,
) -> Result<impl IntoResponse, (StatusCode, Json<serde_json::Value>)> {
let update_result = sqlx::query(r#"UPDATE tickets SET status = $1 WHERE id = $2"#)
.bind(body.status.to_owned())
.bind(id)
.execute(&data.db)
.await
.map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"status": "error", "message": format!("{:?}", e)})),
)
})?;
if update_result.rows_affected() == 0 {
let error_response = serde_json::json!({
"status": "error",
"message": format!("Ticket with ID {} not found", id)
});
return Err((StatusCode::INTERNAL_SERVER_ERROR, Json(error_response)));
}
let updated_ticket = sqlx::query_as(r#"SELECT * FROM tickets WHERE id = $1"#)
.bind(id)
.fetch_one(&data.db)
.await
.map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"status": "error", "message": format!("{:?}", e)})),
)
})?;
let ticket_response = serde_json::json!({
"ticket": filter_record(&updated_ticket),
"status": "success"
});
Ok(Json(ticket_response))
}
fn filter_record(ticket: &Ticket) -> TicketResponse {
TicketResponse {
id: ticket.id.to_owned(),