From cfa25d2bf779aa409616848a831bb0804936f963 Mon Sep 17 00:00:00 2001 From: Franklin Date: Sat, 18 Mar 2023 08:00:06 -0400 Subject: [PATCH] Added all sql queries for agent & location + Dao methods --- Cargo.lock | 73 ++++++++++++++++++-------------- Cargo.toml | 2 +- sql/agent/delete.sql | 1 + sql/agent/fetch_with_ids.sql | 9 ++++ sql/agent/get_with_id.sql | 8 ++++ sql/agent/insert.sql | 11 +++++ sql/agent/update.sql | 12 ++++++ sql/location/delete.sql | 1 + sql/location/fetch_with_city.sql | 1 + sql/location/get_with_id.sql | 1 + sql/location/insert.sql | 5 +++ sql/project/insert.sql | 0 sql/unit/insert.sql | 0 src/dao/agent.rs | 24 +++++++++++ src/dao/location.rs | 20 +++++++++ src/dao/mod.rs | 1 - 16 files changed, 136 insertions(+), 33 deletions(-) create mode 100644 sql/agent/delete.sql create mode 100644 sql/agent/fetch_with_ids.sql create mode 100644 sql/agent/get_with_id.sql create mode 100644 sql/agent/insert.sql create mode 100644 sql/agent/update.sql create mode 100644 sql/location/delete.sql create mode 100644 sql/location/fetch_with_city.sql create mode 100644 sql/location/get_with_id.sql create mode 100644 sql/location/insert.sql create mode 100644 sql/project/insert.sql create mode 100644 sql/unit/insert.sql diff --git a/Cargo.lock b/Cargo.lock index 7231cde..58fe456 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -80,7 +80,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "465a6172cf69b960917811022d8f29bc0b7fa1398bc4f78b3c466673db1213b6" dependencies = [ "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -195,7 +195,7 @@ dependencies = [ "actix-router", "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -521,9 +521,9 @@ dependencies = [ [[package]] name = "cxx" -version = "1.0.92" +version = "1.0.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a140f260e6f3f79013b8bfc65e7ce630c9ab4388c6a89c71e07226f49487b72" +checksum = "a9c00419335c41018365ddf7e4d5f1c12ee3659ddcf3e01974650ba1de73d038" dependencies = [ "cc", "cxxbridge-flags", @@ -533,9 +533,9 @@ dependencies = [ [[package]] name = "cxx-build" -version = "1.0.92" +version = "1.0.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da6383f459341ea689374bf0a42979739dc421874f112ff26f829b8040b8e613" +checksum = "fb8307ad413a98fff033c8545ecf133e3257747b3bae935e7602aab8aa92d4ca" dependencies = [ "cc", "codespan-reporting", @@ -543,24 +543,24 @@ dependencies = [ "proc-macro2", "quote", "scratch", - "syn", + "syn 2.0.0", ] [[package]] name = "cxxbridge-flags" -version = "1.0.92" +version = "1.0.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90201c1a650e95ccff1c8c0bb5a343213bdd317c6e600a93075bca2eff54ec97" +checksum = "edc52e2eb08915cb12596d29d55f0b5384f00d697a646dbd269b6ecb0fbd9d31" [[package]] name = "cxxbridge-macro" -version = "1.0.92" +version = "1.0.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b75aed41bb2e6367cae39e6326ef817a851db13c13e4f3263714ca3cfb8de56" +checksum = "631569015d0d8d54e6c241733f944042623ab6df7bc3be7466874b05fcdb1c5f" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.0", ] [[package]] @@ -573,7 +573,7 @@ dependencies = [ "proc-macro2", "quote", "rustc_version", - "syn", + "syn 1.0.109", ] [[package]] @@ -629,7 +629,7 @@ dependencies = [ "proc-macro-hack", "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -1562,22 +1562,22 @@ checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed" [[package]] name = "serde" -version = "1.0.156" +version = "1.0.157" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "314b5b092c0ade17c00142951e50ced110ec27cea304b1037c6969246c2469a4" +checksum = "707de5fcf5df2b5788fca98dd7eab490bc2fd9b7ef1404defc462833b83f25ca" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.156" +version = "1.0.157" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7e29c4601e36bcec74a223228dce795f4cd3616341a4af93520ca1a837c087d" +checksum = "78997f4555c22a7971214540c4a661291970619afd56de19f77e0de86296e1e5" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.0", ] [[package]] @@ -1765,7 +1765,7 @@ dependencies = [ "sha2", "sqlx-core", "sqlx-rt", - "syn", + "syn 1.0.109", "url", ] @@ -1807,6 +1807,17 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syn" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cff13bb1732bccfe3b246f3fdb09edfd51c01d6f5299b7ccd9457c2e4e37774" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "termcolor" version = "1.2.0" @@ -1818,22 +1829,22 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.39" +version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5ab016db510546d856297882807df8da66a16fb8c4101cb8b30054b0d5b2d9c" +checksum = "978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.39" +version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5420d42e90af0c38c3290abcca25b9b3bdf379fc9f55c528f53a269d9c9a267e" +checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.0", ] [[package]] @@ -1917,7 +1928,7 @@ checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -1997,9 +2008,9 @@ checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" [[package]] name = "unicode-bidi" -version = "0.3.11" +version = "0.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "524b68aca1d05e03fdf03fcdce2c6c94b6daf6d16861ddaa7e4f2b6638a9052c" +checksum = "7d502c968c6a838ead8e69b2ee18ec708802f99db92a0d156705ec9ef801993b" [[package]] name = "unicode-ident" @@ -2071,7 +2082,7 @@ checksum = "c1b300a878652a387d2a0de915bdae8f1a548f0c6d45e072fe2688794b656cc9" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -2123,7 +2134,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn", + "syn 1.0.109", "wasm-bindgen-shared", ] @@ -2157,7 +2168,7 @@ checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.109", "wasm-bindgen-backend", "wasm-bindgen-shared", ] diff --git a/Cargo.toml b/Cargo.toml index 0b6ecda..5fdcba0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,4 +23,4 @@ uuid = { version = "1.3.0", features = ["v4", "fast-rng", "macro-diagnostics"] } actix-web-utils = { git = "https://git.franklinblanco.dev/franklinblanco/actix-web-utils.git" } err = { git = "https://git.franklinblanco.dev/franklinblanco/err.git" } -jl-types = { path = "../jl-types" } \ No newline at end of file +jl-types = { path = "../jl-types", features = ["sqlx"]} \ No newline at end of file diff --git a/sql/agent/delete.sql b/sql/agent/delete.sql new file mode 100644 index 0000000..b6e2d20 --- /dev/null +++ b/sql/agent/delete.sql @@ -0,0 +1 @@ +DELETE FROM agent WHERE id = $1; \ No newline at end of file diff --git a/sql/agent/fetch_with_ids.sql b/sql/agent/fetch_with_ids.sql new file mode 100644 index 0000000..9115a6b --- /dev/null +++ b/sql/agent/fetch_with_ids.sql @@ -0,0 +1,9 @@ +SELECT +id, +full_name, +credential, +credential_type as "credential_type: _", +time_created, +last_updated +FROM agent where id = ANY($1) +ORDER BY time_created DESC; \ No newline at end of file diff --git a/sql/agent/get_with_id.sql b/sql/agent/get_with_id.sql new file mode 100644 index 0000000..26c0980 --- /dev/null +++ b/sql/agent/get_with_id.sql @@ -0,0 +1,8 @@ +SELECT +id, +full_name, +credential, +credential_type as "credential_type: _", +time_created, +last_updated +FROM agent WHERE id = $1 \ No newline at end of file diff --git a/sql/agent/insert.sql b/sql/agent/insert.sql new file mode 100644 index 0000000..4d4408b --- /dev/null +++ b/sql/agent/insert.sql @@ -0,0 +1,11 @@ +INSERT INTO agent ( + id, full_name, credential, credential_type, time_created, last_updated +) VALUES ( + $1, $2, $3, $4, $5, $5 +) RETURNING +id, +full_name, +credential, +credential_type as "credential_type: _", +time_created, +last_updated; \ No newline at end of file diff --git a/sql/agent/update.sql b/sql/agent/update.sql new file mode 100644 index 0000000..8beafc8 --- /dev/null +++ b/sql/agent/update.sql @@ -0,0 +1,12 @@ +UPDATE agent SET +full_name = $1, +credential = $2, +credential_type = $3, +last_updated = $4 +RETURNING +id, +full_name, +credential, +credential_type as "credential_type: _", +time_created, +last_updated; \ No newline at end of file diff --git a/sql/location/delete.sql b/sql/location/delete.sql new file mode 100644 index 0000000..833b444 --- /dev/null +++ b/sql/location/delete.sql @@ -0,0 +1 @@ +DELETE FROM location where id = $1; \ No newline at end of file diff --git a/sql/location/fetch_with_city.sql b/sql/location/fetch_with_city.sql new file mode 100644 index 0000000..33d34a5 --- /dev/null +++ b/sql/location/fetch_with_city.sql @@ -0,0 +1 @@ +SELECT * FROM location WHERE city = $1 ORDER BY district DESC; \ No newline at end of file diff --git a/sql/location/get_with_id.sql b/sql/location/get_with_id.sql new file mode 100644 index 0000000..b6759c0 --- /dev/null +++ b/sql/location/get_with_id.sql @@ -0,0 +1 @@ +SELECT * FROM location WHERE id = $1; \ No newline at end of file diff --git a/sql/location/insert.sql b/sql/location/insert.sql new file mode 100644 index 0000000..67fb861 --- /dev/null +++ b/sql/location/insert.sql @@ -0,0 +1,5 @@ +INSERT INTO location ( + id, city, district +) VALUES ( + $1, $2, $3 +) RETURNING *; \ No newline at end of file diff --git a/sql/project/insert.sql b/sql/project/insert.sql new file mode 100644 index 0000000..e69de29 diff --git a/sql/unit/insert.sql b/sql/unit/insert.sql new file mode 100644 index 0000000..e69de29 diff --git a/src/dao/agent.rs b/src/dao/agent.rs index e69de29..b311985 100644 --- a/src/dao/agent.rs +++ b/src/dao/agent.rs @@ -0,0 +1,24 @@ +use jl_types::domain::agent::Agent; +use sqlx::{Transaction, Postgres, PgPool, postgres::PgQueryResult}; +use uuid::Uuid; + + +pub async fn insert_agent(tx: &mut Transaction<'_, Postgres>, agent: &Agent) -> Result { + sqlx::query_file_as!(Agent, "sql/agent/insert.sql", agent.id, agent.full_name, agent.credential, agent.credential_type as _, agent.time_created).fetch_one(tx).await +} + +pub async fn get_agent_with_id(conn: &PgPool, agent_id: &Uuid) -> Result, sqlx::Error> { + sqlx::query_file_as!(Agent, "sql/agent/get_with_id.sql", agent_id).fetch_optional(conn).await +} + +pub async fn get_agents_with_ids(conn: &PgPool, agent_ids: &Vec) -> Result, sqlx::Error> { + sqlx::query_file_as!(Agent, "sql/agent/fetch_with_ids.sql", agent_ids).fetch_all(conn).await +} + +pub async fn update_agent(tx: &mut Transaction<'_, Postgres>, agent: Agent) -> Result { + sqlx::query_file_as!(Agent, "sql/agent/update.sql", agent.full_name, agent.credential, agent.credential_type as _, agent.last_updated).fetch_one(tx).await +} + +pub async fn delete_agent(tx: &mut Transaction<'_, Postgres>, agent_id: &Uuid) -> Result { + sqlx::query_file!("sql/agent/delete.sql", agent_id).execute(tx).await +} \ No newline at end of file diff --git a/src/dao/location.rs b/src/dao/location.rs index e69de29..90bd188 100644 --- a/src/dao/location.rs +++ b/src/dao/location.rs @@ -0,0 +1,20 @@ +use jl_types::domain::location::Location; +use sqlx::{Transaction, Postgres, postgres::PgQueryResult, PgPool}; +use uuid::Uuid; + + +pub async fn insert_location(tx: &mut Transaction<'_, Postgres>, location: Location) -> Result { + sqlx::query_file_as!(Location, "sql/location/insert.sql", location.id, location.city, location.district).fetch_one(tx).await +} + +pub async fn get_location_with_id(conn: &PgPool, location_id: &Uuid) -> Result, sqlx::Error> { + sqlx::query_file_as!(Location, "sql/location/get_with_id.sql", location_id).fetch_optional(conn).await +} + +pub async fn get_locations_in_city(conn: &PgPool, city: &String) -> Result, sqlx::Error> { + sqlx::query_file_as!(Location, "sql/location/fetch_with_city.sql", city).fetch_all(conn).await +} + +pub async fn delete_location(tx: &mut Transaction<'_, Postgres>, location_id: Uuid) -> Result { + sqlx::query_file!("sql/location/delete.sql", location_id).execute(tx).await +} \ No newline at end of file diff --git a/src/dao/mod.rs b/src/dao/mod.rs index 68b672d..9ea8269 100644 --- a/src/dao/mod.rs +++ b/src/dao/mod.rs @@ -1,5 +1,4 @@ pub mod main_dao; - pub mod project; pub mod agent; pub mod location;