From 63e1ae6f0de03a4a984df623910a5f0cdee4e4e9 Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Sun, 25 Dec 2022 13:18:28 +0200 Subject: [PATCH] Consistency added --- backend/src/models.rs | 6 +++--- backend/src/vehicle_data.rs | 36 ++++++++++++++++++------------------ common/src/lib.rs | 4 ++-- src/api.rs | 8 +++++--- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/backend/src/models.rs b/backend/src/models.rs index 336137d..5d242c1 100644 --- a/backend/src/models.rs +++ b/backend/src/models.rs @@ -87,7 +87,7 @@ pub struct NewDriver<'a> { } //Vehicles -#[derive(Queryable, AsChangeset)] +#[derive(Queryable)] #[diesel(belongs_to(Driver))] pub struct Vehicle { pub model: Option, @@ -96,10 +96,10 @@ pub struct Vehicle { pub plate_num: String, pub vehicle_type: String, pub category: String, - pub owner: Option, + pub owner: Option, } -#[derive(Queryable, AsChangeset)] +#[derive(Queryable)] pub struct Radar { pub id: i32, pub category: Option, diff --git a/backend/src/vehicle_data.rs b/backend/src/vehicle_data.rs index c3ece2a..7aead3b 100644 --- a/backend/src/vehicle_data.rs +++ b/backend/src/vehicle_data.rs @@ -4,7 +4,7 @@ use diesel::PgConnection; use crate::models::Vehicle; -pub fn get_vehicle(connection: &mut PgConnection, vehicle_id: i32) -> CommonVehicle { +pub fn get_vehicle(connection: &mut PgConnection, vehicle_id: String) -> CommonVehicle { use crate::schema::vehicles::dsl::*; let vehicle = &mut vehicles .filter(plate_num.eq(vehicle_id)) @@ -22,20 +22,20 @@ pub fn get_vehicle(connection: &mut PgConnection, vehicle_id: i32) -> CommonVehi }; } -pub fn insert_vehicle(connection: &mut PgConnection, vehicle: CommonVehicle) -> bool { - use crate::schema::vehicles::dsl::*; - //convert CommonVehicle to Vehicle - let vehicle = Vehicle { - model: vehicle.model, - color: vehicle.color, - chasse_num: vehicle.chasse_num, - plate_num: vehicle.plate_num, - vehicle_type: vehicle.vehicle_type, - category: vehicle.category, - owner: vehicle.owner, - }; - diesel::insert_into(vehicles) - .values(&vehicle) - .execute(connection) - .is_ok() -} +// pub fn insert_vehicle(connection: &mut PgConnection, vehicle: CommonVehicle) -> bool { +// use crate::schema::vehicles::dsl::*; +// //convert CommonVehicle to Vehicle +// let vehicle = Vehicle { +// model: vehicle.model, +// color: vehicle.color, +// chasse_num: vehicle.chasse_num, +// plate_num: vehicle.plate_num, +// vehicle_type: vehicle.vehicle_type, +// category: vehicle.category, +// owner: vehicle.owner, +// }; +// diesel::insert_into(vehicles) +// .values(&vehicle) +// .execute(connection) +// .is_ok() +// } diff --git a/common/src/lib.rs b/common/src/lib.rs index 54c496d..e5c3726 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -40,7 +40,7 @@ pub struct CommonVehicle { pub category: String, pub color: Option, pub chasse_num: Option, - pub plate_num: i32, + pub plate_num: String, pub vehicle_type: String, - pub owner: Option, + pub owner: Option, } diff --git a/src/api.rs b/src/api.rs index 6dbf088..e53ce01 100644 --- a/src/api.rs +++ b/src/api.rs @@ -6,6 +6,7 @@ use actix_web::{ }; 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}; @@ -86,7 +87,8 @@ async fn api_test() -> impl Responder { #[get("api/vehicle/{id}")] async fn api_vehicle(id: web::Path) -> impl Responder { - let fetched_vehicle_data = get_vehicle(&mut establish_connection(), *id); + 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", "*")) @@ -97,7 +99,7 @@ async fn api_vehicle(id: web::Path) -> impl Responder { "Content-Type, Content-Length, User-Agent, X-Requested-With, Range, DNT ", )) .body(format!( - "Vehicle ID: {}, Vehicle Model: {}, Vehicle Color: {}", - fetched_vehicle_data.plate_num, fetched_vehicle_data.model, fetched_vehicle_data.color + "Vehicle ID: {}, Vehicle Category: {}", + fetched_vehicle_data.plate_num, fetched_vehicle_data.category )) }