From 01a291f73855ed1744f43797e644f5205050c4bc Mon Sep 17 00:00:00 2001 From: Felipe Diniello Date: Mon, 19 Jun 2023 18:42:56 +0200 Subject: [PATCH 1/7] Include Antenna model diesel macros --- kairo-common/Cargo.toml | 2 ++ kairo-common/diesel.toml | 9 +++++ kairo-common/migrations/.keep | 0 .../down.sql | 6 ++++ .../up.sql | 36 +++++++++++++++++++ .../down.sql | 3 ++ .../2023-06-18-164844_create_antennas/up.sql | 10 ++++++ kairo-common/src/lib.rs | 9 +++-- kairo-common/src/models/antenna.rs | 15 +++++++- kairo-common/src/postgres.rs | 29 +++++++++++++++ kairo-common/src/schema.rs | 13 +++++++ 11 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 kairo-common/diesel.toml create mode 100644 kairo-common/migrations/.keep create mode 100644 kairo-common/migrations/00000000000000_diesel_initial_setup/down.sql create mode 100644 kairo-common/migrations/00000000000000_diesel_initial_setup/up.sql create mode 100644 kairo-common/migrations/2023-06-18-164844_create_antennas/down.sql create mode 100644 kairo-common/migrations/2023-06-18-164844_create_antennas/up.sql create mode 100644 kairo-common/src/postgres.rs create mode 100644 kairo-common/src/schema.rs diff --git a/kairo-common/Cargo.toml b/kairo-common/Cargo.toml index 020e03a..213b17c 100644 --- a/kairo-common/Cargo.toml +++ b/kairo-common/Cargo.toml @@ -20,3 +20,5 @@ influxdb2-structmap = "0.2" influxdb2-derive = "0.1.1" futures = "0.3.28" num-traits = "0.2" +diesel = { version = "2.1.0", features = ["postgres"] } +diesel-async = { version = "0.3.1", features = ["postgres"] } diff --git a/kairo-common/diesel.toml b/kairo-common/diesel.toml new file mode 100644 index 0000000..c028f4a --- /dev/null +++ b/kairo-common/diesel.toml @@ -0,0 +1,9 @@ +# For documentation on how to configure this file, +# see https://diesel.rs/guides/configuring-diesel-cli + +[print_schema] +file = "src/schema.rs" +custom_type_derives = ["diesel::query_builder::QueryId"] + +[migrations_directory] +dir = "migrations" diff --git a/kairo-common/migrations/.keep b/kairo-common/migrations/.keep new file mode 100644 index 0000000..e69de29 diff --git a/kairo-common/migrations/00000000000000_diesel_initial_setup/down.sql b/kairo-common/migrations/00000000000000_diesel_initial_setup/down.sql new file mode 100644 index 0000000..a9f5260 --- /dev/null +++ b/kairo-common/migrations/00000000000000_diesel_initial_setup/down.sql @@ -0,0 +1,6 @@ +-- This file was automatically created by Diesel to setup helper functions +-- and other internal bookkeeping. This file is safe to edit, any future +-- changes will be added to existing projects as new migrations. + +DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); +DROP FUNCTION IF EXISTS diesel_set_updated_at(); diff --git a/kairo-common/migrations/00000000000000_diesel_initial_setup/up.sql b/kairo-common/migrations/00000000000000_diesel_initial_setup/up.sql new file mode 100644 index 0000000..d68895b --- /dev/null +++ b/kairo-common/migrations/00000000000000_diesel_initial_setup/up.sql @@ -0,0 +1,36 @@ +-- This file was automatically created by Diesel to setup helper functions +-- and other internal bookkeeping. This file is safe to edit, any future +-- changes will be added to existing projects as new migrations. + + + + +-- Sets up a trigger for the given table to automatically set a column called +-- `updated_at` whenever the row is modified (unless `updated_at` was included +-- in the modified columns) +-- +-- # Example +-- +-- ```sql +-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW()); +-- +-- SELECT diesel_manage_updated_at('users'); +-- ``` +CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$ +BEGIN + EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s + FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl); +END; +$$ LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$ +BEGIN + IF ( + NEW IS DISTINCT FROM OLD AND + NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at + ) THEN + NEW.updated_at := current_timestamp; + END IF; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; diff --git a/kairo-common/migrations/2023-06-18-164844_create_antennas/down.sql b/kairo-common/migrations/2023-06-18-164844_create_antennas/down.sql new file mode 100644 index 0000000..8e1248e --- /dev/null +++ b/kairo-common/migrations/2023-06-18-164844_create_antennas/down.sql @@ -0,0 +1,3 @@ +-- This file should undo anything in `up.sql` + +DROP TABLE antennas; \ No newline at end of file diff --git a/kairo-common/migrations/2023-06-18-164844_create_antennas/up.sql b/kairo-common/migrations/2023-06-18-164844_create_antennas/up.sql new file mode 100644 index 0000000..f5902a1 --- /dev/null +++ b/kairo-common/migrations/2023-06-18-164844_create_antennas/up.sql @@ -0,0 +1,10 @@ +-- Your SQL goes here + +CREATE TABLE antennas ( + id VARCHAR(17) PRIMARY KEY, + tssi DOUBLE PRECISION, + pos_x DOUBLE PRECISION, + pos_y DOUBLE PRECISION, + pos_z DOUBLE PRECISION, + comment TEXT +); \ No newline at end of file diff --git a/kairo-common/src/lib.rs b/kairo-common/src/lib.rs index 13f254b..ed439b7 100644 --- a/kairo-common/src/lib.rs +++ b/kairo-common/src/lib.rs @@ -4,6 +4,7 @@ #![allow(confusable_idents)] pub mod influx; +pub mod postgres; // random functions for mqtt pub mod mqtt; @@ -18,7 +19,8 @@ mod types { pub type Point = types::point::Point; pub type MAC = types::mac::MAC; - +// DB models: for SQL with Diesel and InfluxDB and influxdb-derive +mod schema; mod models { pub mod antenna; pub mod beacon_measure; @@ -30,8 +32,11 @@ mod models { pub data: Vec, } } -pub type Antenna = models::antenna::Antenna; pub type DeviceReport = models::DeviceReport; + +pub type Antenna = models::antenna::Antenna; +pub type NewAntenna<'a> = models::antenna::NewAntenna<'a>; + pub type KnownPosition = models::known_position::KnownPosition; pub type DynamicDeviceStatus = models::dynamic_device_status::DynamicDeviceStatus; pub type BeaconMeasure = models::beacon_measure::BeaconMeasure; diff --git a/kairo-common/src/models/antenna.rs b/kairo-common/src/models/antenna.rs index 9d8faf8..9786b91 100644 --- a/kairo-common/src/models/antenna.rs +++ b/kairo-common/src/models/antenna.rs @@ -1,8 +1,10 @@ use std::f64::consts::PI; +use diesel::prelude::*; use crate::{unit_conversion::UnitsConversion, Point}; -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone, Default,Queryable, Selectable)] +#[diesel(table_name = crate::schema::antennas)] pub struct Antenna { pub id: String, pub tssi: f64, @@ -12,6 +14,17 @@ pub struct Antenna { pos_z: f64, } +#[derive(Insertable)] +#[diesel(table_name = crate::schema::antennas)] +pub struct NewAntenna<'a> { + id: &'a str, + comment: Option<&'a str>, + tssi: f64, + pos_x: f64, + pos_y: f64, + pos_z: f64, +} + impl Antenna { const C: f64 = 2.99e8; const F: f64 = 2.4e9; diff --git a/kairo-common/src/postgres.rs b/kairo-common/src/postgres.rs new file mode 100644 index 0000000..06ac973 --- /dev/null +++ b/kairo-common/src/postgres.rs @@ -0,0 +1,29 @@ +use diesel::r2d2::{ConnectionManager, Pool, PooledConnection}; +use diesel::prelude::*; + +pub type DbConn = diesel::pg::PgConnection; + +// pub type DbPooledConn = PooledConnection>; +pub type DbPool = Pool>; + +pub struct DbPooledConn(pub PooledConnection>); + + +pub fn establish_connection() -> DbConn { + let database_url = dotenv::var("DATABASE_URL").expect("DATABASE_URL must be set"); + PgConnection::establish(&database_url) + .unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) +} + +pub fn init_pool() -> DbPool { + let database_url = dotenv::var("DATABASE_URL").expect("DATABASE_URL must be set"); + let manager = ConnectionManager::::new(database_url); + DbPool::new(manager).expect("Error connecting to DB") +} + +impl std::ops::Deref for DbPooledConn { + type Target = PgConnection; + fn deref(&self) -> &Self::Target { + &self.0 + } +} \ No newline at end of file diff --git a/kairo-common/src/schema.rs b/kairo-common/src/schema.rs new file mode 100644 index 0000000..17c9565 --- /dev/null +++ b/kairo-common/src/schema.rs @@ -0,0 +1,13 @@ +// @generated automatically by Diesel CLI. + +diesel::table! { + antennas (id) { + #[max_length = 17] + id -> Varchar, + tssi -> Nullable, + pos_x -> Nullable, + pos_y -> Nullable, + pos_z -> Nullable, + comment -> Nullable, + } +} -- 2.49.1 From fff33c1e21009c4e7fc98b5e348ede4e0048204c Mon Sep 17 00:00:00 2001 From: Felipe Diniello Date: Fri, 21 Jul 2023 20:11:55 +0200 Subject: [PATCH 2/7] Move diesel as a workspace dependency --- Cargo.toml | 3 ++- kairo-common/Cargo.toml | 4 ++-- kairo-common/src/lib.rs | 2 +- kairo-core/Cargo.toml | 4 ++++ 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d32d81a..ef8d3fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,4 +18,5 @@ dotenv = "0.15.0" chrono = "0.4.24" paho-mqtt = "0.12.1" serde = "1.0.162" -serde_json = { version = "1.0.95" } \ No newline at end of file +serde_json = { version = "1.0.95" } +diesel = { version = "2.1.0", features = ["postgres", "extras"] } diff --git a/kairo-common/Cargo.toml b/kairo-common/Cargo.toml index 213b17c..9556c19 100644 --- a/kairo-common/Cargo.toml +++ b/kairo-common/Cargo.toml @@ -14,11 +14,11 @@ dotenv = { workspace = true } chrono = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } +diesel = { workspace = true } + influxdb2 = "0.4.2" influxdb2-structmap = "0.2" influxdb2-derive = "0.1.1" futures = "0.3.28" num-traits = "0.2" -diesel = { version = "2.1.0", features = ["postgres"] } -diesel-async = { version = "0.3.1", features = ["postgres"] } diff --git a/kairo-common/src/lib.rs b/kairo-common/src/lib.rs index ed439b7..0610a16 100644 --- a/kairo-common/src/lib.rs +++ b/kairo-common/src/lib.rs @@ -20,7 +20,7 @@ pub type Point = types::point::Point; pub type MAC = types::mac::MAC; // DB models: for SQL with Diesel and InfluxDB and influxdb-derive -mod schema; +pub mod schema; mod models { pub mod antenna; pub mod beacon_measure; diff --git a/kairo-core/Cargo.toml b/kairo-core/Cargo.toml index c7449b1..c86cad7 100644 --- a/kairo-core/Cargo.toml +++ b/kairo-core/Cargo.toml @@ -6,3 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +diesel = { workspace = true} + +rocket = { version = "0.5.0-rc.3", features = ["json"] } +kairo-common = { path = "../kairo-common" } \ No newline at end of file -- 2.49.1 From 9fd99acf2eee1e4907a3927424f267fe5de7bd41 Mon Sep 17 00:00:00 2001 From: Felipe Diniello Date: Fri, 21 Jul 2023 20:15:06 +0200 Subject: [PATCH 3/7] Matching model, table and schema --- .../2023-06-18-164844_create_antennas/up.sql | 8 +++--- kairo-common/src/models/antenna.rs | 25 ++++++++++--------- kairo-common/src/schema.rs | 8 +++--- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/kairo-common/migrations/2023-06-18-164844_create_antennas/up.sql b/kairo-common/migrations/2023-06-18-164844_create_antennas/up.sql index f5902a1..57332ea 100644 --- a/kairo-common/migrations/2023-06-18-164844_create_antennas/up.sql +++ b/kairo-common/migrations/2023-06-18-164844_create_antennas/up.sql @@ -2,9 +2,9 @@ CREATE TABLE antennas ( id VARCHAR(17) PRIMARY KEY, - tssi DOUBLE PRECISION, - pos_x DOUBLE PRECISION, - pos_y DOUBLE PRECISION, - pos_z DOUBLE PRECISION, + tssi DOUBLE PRECISION NOT NULL, + pos_x DOUBLE PRECISION NOT NULL, + pos_y DOUBLE PRECISION NOT NULL, + pos_z DOUBLE PRECISION NOT NULL, comment TEXT ); \ No newline at end of file diff --git a/kairo-common/src/models/antenna.rs b/kairo-common/src/models/antenna.rs index 9786b91..e226be6 100644 --- a/kairo-common/src/models/antenna.rs +++ b/kairo-common/src/models/antenna.rs @@ -1,28 +1,29 @@ -use std::f64::consts::PI; use diesel::prelude::*; +use std::f64::consts::PI; use crate::{unit_conversion::UnitsConversion, Point}; -#[derive(Debug, Clone, Default,Queryable, Selectable)] +#[derive(Debug, Clone, Default, Queryable, Selectable, serde::Serialize, serde::Deserialize)] +#[diesel(check_for_backend(diesel::pg::Pg))] #[diesel(table_name = crate::schema::antennas)] pub struct Antenna { pub id: String, pub tssi: f64, + pub pos_x: f64, + pub pos_y: f64, + pub pos_z: f64, pub comment: Option, - pos_x: f64, - pos_y: f64, - pos_z: f64, } -#[derive(Insertable)] +#[derive(Debug, Clone, Copy, Insertable, AsChangeset, serde::Deserialize)] #[diesel(table_name = crate::schema::antennas)] pub struct NewAntenna<'a> { - id: &'a str, - comment: Option<&'a str>, - tssi: f64, - pos_x: f64, - pos_y: f64, - pos_z: f64, + pub id: &'a str, + pub tssi: f64, + pub pos_x: f64, + pub pos_y: f64, + pub pos_z: f64, + pub comment: Option<&'a str>, } impl Antenna { diff --git a/kairo-common/src/schema.rs b/kairo-common/src/schema.rs index 17c9565..d0a6a02 100644 --- a/kairo-common/src/schema.rs +++ b/kairo-common/src/schema.rs @@ -4,10 +4,10 @@ diesel::table! { antennas (id) { #[max_length = 17] id -> Varchar, - tssi -> Nullable, - pos_x -> Nullable, - pos_y -> Nullable, - pos_z -> Nullable, + tssi -> Float8, + pos_x -> Float8, + pos_y -> Float8, + pos_z -> Float8, comment -> Nullable, } } -- 2.49.1 From 0ddbb21f52a17533d4fc4b8a2280d7a4e58d0ab1 Mon Sep 17 00:00:00 2001 From: Felipe Diniello Date: Fri, 21 Jul 2023 20:15:51 +0200 Subject: [PATCH 4/7] antennas endpoints working --- kairo-core/src/antennas.rs | 63 ++++++++++++++++++++++++++++++++++++++ kairo-core/src/main.rs | 31 +++++++++++++++++-- 2 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 kairo-core/src/antennas.rs diff --git a/kairo-core/src/antennas.rs b/kairo-core/src/antennas.rs new file mode 100644 index 0000000..2ee037d --- /dev/null +++ b/kairo-core/src/antennas.rs @@ -0,0 +1,63 @@ +use diesel::prelude::*; +use rocket::{http::Status, serde::json::Json, State}; + +use kairo_common::{postgres, schema::antennas, Antenna, NewAntenna}; + +#[rocket::get("/id/")] +pub fn get_by_id(db_pool: &State, id: String) -> Option> { + let mut db = db_pool.get().unwrap(); + + let res = antennas::table + .select(antennas::all_columns) + .find(id) + .get_result::(&mut db); + + match res { + Ok(v) => Some(rocket::serde::json::Json(v)), + _ => None, + } +} + +#[rocket::get("/")] +pub fn get_list(db_pool: &State) -> Json> { + let mut db = db_pool.get().unwrap(); + + let res = antennas::table + .select(antennas::all_columns) + .load::(&mut db); + + match res { + Ok(v) => rocket::serde::json::Json(v), + _ => rocket::serde::json::Json(vec![]), + } +} + +#[rocket::post("/new", format = "json", data = "")] +pub fn new(db_pool: &State, antenna: Json>) -> Status { + let mut db = db_pool.get().unwrap(); + + let res = diesel::insert_into(antennas::table) + .values(*antenna) + .execute(&mut db); + + match res { + Ok(_) => Status::Ok, + _ => Status::NotAcceptable, + } +} + +#[rocket::patch("/update", format = "json", data = "")] +pub fn update(db_pool: &State, antenna: Json>) -> Status { + let mut db = db_pool.get().unwrap(); + + let res = diesel::update(antennas::table) + .filter(antennas::id.eq(antenna.id)) + .set(*antenna) + .execute(&mut db); + + match res { + Ok(0) => Status::NotModified, + Ok(1) => Status::Ok, + _ => Status::BadRequest, + } +} diff --git a/kairo-core/src/main.rs b/kairo-core/src/main.rs index e7a11a9..45a377e 100644 --- a/kairo-core/src/main.rs +++ b/kairo-core/src/main.rs @@ -1,3 +1,30 @@ -fn main() { - println!("Hello, world!"); +#[macro_use] +extern crate rocket; + +mod antennas; + +use std::path::{Path, PathBuf}; + +use rocket::fs::NamedFile; +use rocket::response::status::NotFound; + +use kairo_common::postgres; + +#[get("/")] +async fn serve_file(file: PathBuf) -> Result> { + let path = Path::new("static/").join(file); + NamedFile::open(&path) + .await + .map_err(|e| NotFound(e.to_string())) +} + +#[launch] +fn rocket() -> _ { + rocket::build() + .manage(postgres::init_pool()) + .mount("/static", routes![serve_file]) + .mount("/antennas/", routes![antennas::get_by_id]) + .mount("/antennas/", routes![antennas::get_list]) + .mount("/antennas/", routes![antennas::update]) + .mount("/antennas/", routes![antennas::new]) } -- 2.49.1 From cb7b62a09289514db94c788f45ef2a07e20bf9c0 Mon Sep 17 00:00:00 2001 From: Felipe Diniello Date: Fri, 21 Jul 2023 20:15:57 +0200 Subject: [PATCH 5/7] formating --- kairo-common/src/lib.rs | 3 +-- kairo-common/src/postgres.rs | 8 ++------ xyz-engine/src/main.rs | 4 +--- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/kairo-common/src/lib.rs b/kairo-common/src/lib.rs index 0610a16..24bdebf 100644 --- a/kairo-common/src/lib.rs +++ b/kairo-common/src/lib.rs @@ -6,11 +6,10 @@ pub mod influx; pub mod postgres; -// random functions for mqtt +// random functions for mqtt pub mod mqtt; pub mod unit_conversion; - // Commonly used types across the services mod types { pub mod mac; // deprecated for the time being. diff --git a/kairo-common/src/postgres.rs b/kairo-common/src/postgres.rs index 06ac973..68d5acc 100644 --- a/kairo-common/src/postgres.rs +++ b/kairo-common/src/postgres.rs @@ -1,14 +1,10 @@ -use diesel::r2d2::{ConnectionManager, Pool, PooledConnection}; use diesel::prelude::*; +use diesel::r2d2::{ConnectionManager, Pool, PooledConnection}; pub type DbConn = diesel::pg::PgConnection; - -// pub type DbPooledConn = PooledConnection>; pub type DbPool = Pool>; - pub struct DbPooledConn(pub PooledConnection>); - pub fn establish_connection() -> DbConn { let database_url = dotenv::var("DATABASE_URL").expect("DATABASE_URL must be set"); PgConnection::establish(&database_url) @@ -26,4 +22,4 @@ impl std::ops::Deref for DbPooledConn { fn deref(&self) -> &Self::Target { &self.0 } -} \ No newline at end of file +} diff --git a/xyz-engine/src/main.rs b/xyz-engine/src/main.rs index 4e08638..240fbcf 100644 --- a/xyz-engine/src/main.rs +++ b/xyz-engine/src/main.rs @@ -3,9 +3,7 @@ use futures::stream::StreamExt; mod handler; mod position_solver; -use kairo_common::mqtt::for_async::{ - get_mqtt_cli_and_stream, mqtt_cli_reconnect, mqtt_subscribe, -}; +use kairo_common::mqtt::for_async::{get_mqtt_cli_and_stream, mqtt_cli_reconnect, mqtt_subscribe}; #[tokio::main] async fn main() { -- 2.49.1 From 9426c5513e992ac6ba1588394537ba526c0baead Mon Sep 17 00:00:00 2001 From: Felipe Diniello Date: Sat, 22 Jul 2023 00:43:00 +0200 Subject: [PATCH 6/7] AntennaNew was not really needed. --- kairo-common/src/lib.rs | 1 - kairo-common/src/models/antenna.rs | 22 ++++++++++------------ kairo-core/src/antennas.rs | 12 ++++++------ 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/kairo-common/src/lib.rs b/kairo-common/src/lib.rs index 24bdebf..e126a27 100644 --- a/kairo-common/src/lib.rs +++ b/kairo-common/src/lib.rs @@ -34,7 +34,6 @@ mod models { pub type DeviceReport = models::DeviceReport; pub type Antenna = models::antenna::Antenna; -pub type NewAntenna<'a> = models::antenna::NewAntenna<'a>; pub type KnownPosition = models::known_position::KnownPosition; pub type DynamicDeviceStatus = models::dynamic_device_status::DynamicDeviceStatus; diff --git a/kairo-common/src/models/antenna.rs b/kairo-common/src/models/antenna.rs index e226be6..d7f494c 100644 --- a/kairo-common/src/models/antenna.rs +++ b/kairo-common/src/models/antenna.rs @@ -3,7 +3,16 @@ use std::f64::consts::PI; use crate::{unit_conversion::UnitsConversion, Point}; -#[derive(Debug, Clone, Default, Queryable, Selectable, serde::Serialize, serde::Deserialize)] +#[derive( + Debug, + Clone, + Queryable, + Selectable, + Insertable, + AsChangeset, + serde::Serialize, + serde::Deserialize, +)] #[diesel(check_for_backend(diesel::pg::Pg))] #[diesel(table_name = crate::schema::antennas)] pub struct Antenna { @@ -15,17 +24,6 @@ pub struct Antenna { pub comment: Option, } -#[derive(Debug, Clone, Copy, Insertable, AsChangeset, serde::Deserialize)] -#[diesel(table_name = crate::schema::antennas)] -pub struct NewAntenna<'a> { - pub id: &'a str, - pub tssi: f64, - pub pos_x: f64, - pub pos_y: f64, - pub pos_z: f64, - pub comment: Option<&'a str>, -} - impl Antenna { const C: f64 = 2.99e8; const F: f64 = 2.4e9; diff --git a/kairo-core/src/antennas.rs b/kairo-core/src/antennas.rs index 2ee037d..463d08c 100644 --- a/kairo-core/src/antennas.rs +++ b/kairo-core/src/antennas.rs @@ -1,7 +1,7 @@ use diesel::prelude::*; use rocket::{http::Status, serde::json::Json, State}; -use kairo_common::{postgres, schema::antennas, Antenna, NewAntenna}; +use kairo_common::{postgres, schema::antennas, Antenna}; #[rocket::get("/id/")] pub fn get_by_id(db_pool: &State, id: String) -> Option> { @@ -33,11 +33,11 @@ pub fn get_list(db_pool: &State) -> Json> { } #[rocket::post("/new", format = "json", data = "")] -pub fn new(db_pool: &State, antenna: Json>) -> Status { +pub fn new(db_pool: &State, antenna: Json) -> Status { let mut db = db_pool.get().unwrap(); let res = diesel::insert_into(antennas::table) - .values(*antenna) + .values(antenna.0) .execute(&mut db); match res { @@ -47,12 +47,12 @@ pub fn new(db_pool: &State, antenna: Json>) -> } #[rocket::patch("/update", format = "json", data = "")] -pub fn update(db_pool: &State, antenna: Json>) -> Status { +pub fn update(db_pool: &State, antenna: Json) -> Status { let mut db = db_pool.get().unwrap(); let res = diesel::update(antennas::table) - .filter(antennas::id.eq(antenna.id)) - .set(*antenna) + .filter(antennas::id.eq(antenna.id.clone())) + .set(antenna.0) .execute(&mut db); match res { -- 2.49.1 From 894fcd3b40ddb1bb812a86985c914643106c6e4e Mon Sep 17 00:00:00 2001 From: Felipe Diniello Date: Sat, 22 Jul 2023 19:39:26 +0200 Subject: [PATCH 7/7] Delete is also needed --- kairo-core/src/antennas.rs | 14 ++++++++++++++ kairo-core/src/main.rs | 1 + 2 files changed, 15 insertions(+) diff --git a/kairo-core/src/antennas.rs b/kairo-core/src/antennas.rs index 463d08c..2144bcb 100644 --- a/kairo-core/src/antennas.rs +++ b/kairo-core/src/antennas.rs @@ -61,3 +61,17 @@ pub fn update(db_pool: &State, antenna: Json) -> Stat _ => Status::BadRequest, } } + +#[rocket::delete("/delete/")] +pub fn delete(db_pool: &State, id: String) -> Status { + let mut db = db_pool.get().unwrap(); + + let res = diesel::delete(antennas::table) + .filter( antennas::id.eq(id) ) + .execute(&mut db); + + match res { + Ok(1) => Status::Ok, + _ => Status::BadRequest, + } +} diff --git a/kairo-core/src/main.rs b/kairo-core/src/main.rs index 45a377e..b8f0fd9 100644 --- a/kairo-core/src/main.rs +++ b/kairo-core/src/main.rs @@ -25,6 +25,7 @@ fn rocket() -> _ { .mount("/static", routes![serve_file]) .mount("/antennas/", routes![antennas::get_by_id]) .mount("/antennas/", routes![antennas::get_list]) + .mount("/antennas/", routes![antennas::delete]) .mount("/antennas/", routes![antennas::update]) .mount("/antennas/", routes![antennas::new]) } -- 2.49.1