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::{ use crate::{
AppState, AppState,
models::{Ticket, TicketCreateScheme, TicketResponse}, models::{Ticket, TicketCreateScheme, TicketResponse, TicketUpdateScheme},
}; };
pub async fn create_ticket( pub async fn create_ticket(
@@ -93,7 +93,7 @@ pub async fn get_ticket_by_id(
Path(id): Path<i32>, Path(id): Path<i32>,
State(data): State<Arc<AppState>>, State(data): State<Arc<AppState>>,
) -> Result<impl IntoResponse, (StatusCode, Json<serde_json::Value>)> { ) -> 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) .bind(id)
.fetch_one(&data.db) .fetch_one(&data.db)
.await; .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 { fn filter_record(ticket: &Ticket) -> TicketResponse {
TicketResponse { TicketResponse {
id: ticket.id.to_owned(), id: ticket.id.to_owned(),

View File

@@ -42,3 +42,8 @@ pub struct TicketCreateScheme {
pub description: String, pub description: String,
pub room: i16, pub room: i16,
} }
#[derive(Deserialize, Serialize, Debug)]
pub struct TicketUpdateScheme {
pub status: String,
}

View File

@@ -7,7 +7,7 @@ use axum::{
use crate::{ use crate::{
AppState, AppState,
handlers::ticket::{create_ticket, delete_ticket, get_ticket_by_id, get_tickets}, handlers::ticket::{create_ticket, delete_ticket, edit_ticket, get_ticket_by_id, get_tickets},
}; };
pub fn create_router(state: Arc<AppState>) -> Router { pub fn create_router(state: Arc<AppState>) -> Router {
@@ -16,7 +16,9 @@ pub fn create_router(state: Arc<AppState>) -> Router {
.route("/api/tickets/create", post(create_ticket)) .route("/api/tickets/create", post(create_ticket))
.route( .route(
"/api/tickets/{id}", "/api/tickets/{id}",
get(get_ticket_by_id).delete(delete_ticket), get(get_ticket_by_id)
.delete(delete_ticket)
.patch(edit_ticket),
) )
.with_state(state) .with_state(state)
} }

View File

@@ -6,5 +6,5 @@ CREATE TABLE IF NOT EXISTS tickets (
room SMALLINT, room SMALLINT,
status VARCHAR(15) NOT NULL DEFAULT 'ToDo', status VARCHAR(15) NOT NULL DEFAULT 'ToDo',
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
user_id SMALLINT user_id SMALLINT DEFAULT 1
); );