Files
poo-stack.rs/src/api.rs
2022-12-26 01:03:42 +02:00

104 lines
4.0 KiB
Rust

use actix_web::{
get,
http::header::ContentType,
web::{self, Json},
HttpResponse, Responder,
};
use backend::{
admin_data::get_admin, driver_data::get_driver, establish_connection, ticket_data::get_ticket,
vehicle_data::get_vehicle,
};
// use common::{CommonAdmin, CommonDriver, CommonTicket};
#[get("api/ticket/{id}")]
async fn api_ticket(id: web::Path<i32>) -> impl Responder {
let fetched_ticket_data = get_ticket(&mut establish_connection(), *id);
//return struct values as string
HttpResponse::Ok()
.content_type(ContentType::plaintext())
.insert_header(("Access-Control-Allow-Origin", "*"))
.insert_header(("content-type", "text/plain"))
.insert_header(("content-encoded", "gzip"))
.insert_header((
"Access-Control-Allow-Headers",
"Content-Type, Content-Length, User-Agent, X-Requested-With, Range, DNT ",
))
.body(format!(
"Ticket ID: {}, Ticket Price: {}, Ticket Status: {}",
fetched_ticket_data.id, fetched_ticket_data.category, fetched_ticket_data.description
))
}
// Getting admin data or smth idk
#[get("api/admin/{id}")]
async fn api_admin(id: web::Path<i32>) -> impl Responder {
let fetched_admin_data = get_admin(&mut establish_connection(), *id);
//return struct values as string
HttpResponse::Ok()
.content_type(ContentType::plaintext())
.insert_header(("Access-Control-Allow-Origin", "*"))
.insert_header(("content-type", "text/plain"))
.insert_header(("content-encoded", "gzip"))
.insert_header((
"Access-Control-Allow-Headers",
"Content-Type, Content-Length, User-Agent, X-Requested-With, Range, DNT ",
))
.body(format!(
"Admin ID: {}, Admin Name: {}, Admin Address: {}",
fetched_admin_data.id, fetched_admin_data.name, fetched_admin_data.address
))
}
#[get("api/driver/{id}")]
async fn api_driver(id: web::Path<i32>) -> impl Responder {
let fetched_driver_data = get_driver(&mut establish_connection(), *id);
HttpResponse::Ok()
.content_type(ContentType::plaintext())
//headers for plain text
.insert_header(("Access-Control-Allow-Origin", "*"))
.insert_header(("content-type", "text/plain"))
.insert_header(("content-encoded", "gzip"))
.insert_header((
"Access-Control-Allow-Headers",
"Content-Type, Content-Length, User-Agent, X-Requested-With, Range, DNT ",
))
.body(format!(
"Driver ID: {}, Driver Name: {}, Driver Address: {}",
fetched_driver_data.id, fetched_driver_data.name, fetched_driver_data.address
))
}
#[get("api/test")]
async fn api_test() -> impl Responder {
HttpResponse::Ok()
.content_type(ContentType::plaintext())
.insert_header(("Access-Control-Allow-Origin", "*"))
.insert_header(("Access-Control-Allow-Methods", "GET"))
.insert_header((
"Access-Control-Allow-Headers",
"Content-Type, Content-Length, User-Agent, X-Requested-With, Range, DNT ",
))
.insert_header(("content-type", "text/plain"))
.insert_header(("content-encoded", "gzip"))
.body("Alles gut")
}
#[get("api/vehicle/{id}")]
async fn api_vehicle(id: web::Path<String>) -> impl Responder {
let id = id.into_inner();
let fetched_vehicle_data = get_vehicle(&mut establish_connection(), String::from(id));
HttpResponse::Ok()
.content_type(ContentType::plaintext())
.insert_header(("Access-Control-Allow-Origin", "*"))
.insert_header(("content-type", "text/plain"))
.insert_header(("content-encoded", "gzip"))
.insert_header((
"Access-Control-Allow-Headers",
"Content-Type, Content-Length, User-Agent, X-Requested-With, Range, DNT ",
))
.body(format!(
"Vehicle ID: {}, Vehicle Category: {}",
fetched_vehicle_data.plate_num, fetched_vehicle_data.category
))
}