Added functions for adding, deleting and showing tickets
As said, functions got implemented. Also minor changes to migration layout and dependencies
This commit is contained in:
103
backend/src/handlers/ticket.rs
Normal file
103
backend/src/handlers/ticket.rs
Normal file
@@ -0,0 +1,103 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::{
|
||||
Json,
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
response::IntoResponse,
|
||||
};
|
||||
use serde_json::json;
|
||||
use sqlx::query;
|
||||
|
||||
use crate::{
|
||||
AppState,
|
||||
models::{Ticket, TicketCreateScheme, TicketResponse},
|
||||
};
|
||||
|
||||
pub async fn create_ticket(
|
||||
State(data): State<Arc<AppState>>,
|
||||
Json(body): Json<TicketCreateScheme>,
|
||||
) -> Result<impl IntoResponse, (StatusCode, Json<serde_json::Value>)> {
|
||||
let query = query(
|
||||
r#"INSERT INTO tickets (category, description, betreff, room) VALUES ($1, $2, $3, $4)"#,
|
||||
)
|
||||
.bind(body.category.to_string())
|
||||
.bind(body.description.to_string())
|
||||
.bind(body.betreff.to_string())
|
||||
.bind(body.room.to_string())
|
||||
.execute(&data.db)
|
||||
.await;
|
||||
|
||||
if let Err(err) = query {
|
||||
return Err((
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(json!({"status": "error", "message": format!("{:?}", err),})),
|
||||
));
|
||||
}
|
||||
|
||||
let response_status = serde_json::json!({"status": "success"});
|
||||
Ok(Json(response_status))
|
||||
}
|
||||
|
||||
pub async fn delete_ticket(
|
||||
Path(id): Path<i32>,
|
||||
State(data): State<Arc<AppState>>,
|
||||
) -> Result<impl IntoResponse, (StatusCode, Json<serde_json::Value>)> {
|
||||
let query = sqlx::query(r#"DELETE FROM tickets WHERE id = $1"#)
|
||||
.bind(id)
|
||||
.execute(&data.db)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(json!({"status": "error", "message": format!("{:?}", e)})),
|
||||
)
|
||||
})?;
|
||||
|
||||
if query.rows_affected() == 0 {
|
||||
let error_response = serde_json::json!({
|
||||
"status": "error",
|
||||
"message": format!("Ticket with ID {} not found", id)
|
||||
});
|
||||
return Err((StatusCode::NOT_FOUND, Json(error_response)));
|
||||
}
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
pub async fn get_tickets(
|
||||
State(data): State<Arc<AppState>>,
|
||||
) -> Result<impl IntoResponse, (StatusCode, Json<serde_json::Value>)> {
|
||||
let tickets =
|
||||
sqlx::query_as(r#"SELECT * FROM tickets WHERE status <> 'Archived' ORDER BY date DESC"#)
|
||||
.fetch_all(&data.db)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
let error_response = serde_json::json!({
|
||||
"status": "error",
|
||||
"message": format!("Database error: {}", e),
|
||||
});
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(error_response))
|
||||
})?;
|
||||
|
||||
let ticket_response = tickets
|
||||
.iter()
|
||||
.map(|ticket| filter_record(&ticket))
|
||||
.collect::<Vec<TicketResponse>>();
|
||||
|
||||
let json_response = serde_json::json!(ticket_response);
|
||||
Ok(Json(json_response))
|
||||
}
|
||||
|
||||
fn filter_record(ticket: &Ticket) -> TicketResponse {
|
||||
TicketResponse {
|
||||
id: ticket.id.to_owned(),
|
||||
category: ticket.category.to_owned(),
|
||||
betreff: ticket.betreff.to_owned(),
|
||||
description: ticket.description.to_owned(),
|
||||
room: ticket.room.to_owned(),
|
||||
status: ticket.status.to_owned(),
|
||||
date: ticket.date.to_owned(),
|
||||
user_id: ticket.user_id.to_owned(),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user