From f4c3b70f19fbdc58c911f5ad335e3a012389bec8 Mon Sep 17 00:00:00 2001 From: Franklin Date: Sun, 19 Mar 2023 11:40:22 -0400 Subject: [PATCH] Added macros, fixed agent not having pfp in all queries --- migrations/2_agent.sql | 1 + sql/agent/fetch_all.sql | 1 + sql/agent/fetch_with_ids.sql | 13 ++++---- sql/agent/get_with_id.sql | 1 + sql/agent/insert.sql | 5 ++-- sql/agent/update.sql | 6 ++-- src/dao/agent.rs | 2 ++ src/services/admin.rs | 0 src/services/mod.rs | 3 +- src/services/read.rs | 0 src/utils/macros.rs | 57 ++++++++++++++++++++++++++++++++++++ src/utils/mod.rs | 2 +- 12 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 src/services/admin.rs create mode 100644 src/services/read.rs create mode 100644 src/utils/macros.rs diff --git a/migrations/2_agent.sql b/migrations/2_agent.sql index e8d2192..6a86a5a 100644 --- a/migrations/2_agent.sql +++ b/migrations/2_agent.sql @@ -3,6 +3,7 @@ CREATE TABLE IF NOT EXISTS "agent" ( full_name VARCHAR NOT NULL, credential VARCHAR NOT NULL, credential_type VARCHAR NOT NULL, + profile_picture_url VARCHAR NOT NULL, time_created TIMESTAMPTZ NOT NULL, last_updated TIMESTAMPTZ NOT NULL ); \ No newline at end of file diff --git a/sql/agent/fetch_all.sql b/sql/agent/fetch_all.sql index f351309..9a6666e 100644 --- a/sql/agent/fetch_all.sql +++ b/sql/agent/fetch_all.sql @@ -3,6 +3,7 @@ SELECT full_name, credential, credential_type as "credential_type: _", + profile_picture_url, time_created, last_updated FROM agent diff --git a/sql/agent/fetch_with_ids.sql b/sql/agent/fetch_with_ids.sql index 9115a6b..f35f65b 100644 --- a/sql/agent/fetch_with_ids.sql +++ b/sql/agent/fetch_with_ids.sql @@ -1,9 +1,10 @@ SELECT -id, -full_name, -credential, -credential_type as "credential_type: _", -time_created, -last_updated + id, + full_name, + credential, + credential_type as "credential_type: _", + profile_picture_url, + 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 index 26c0980..6e029be 100644 --- a/sql/agent/get_with_id.sql +++ b/sql/agent/get_with_id.sql @@ -3,6 +3,7 @@ id, full_name, credential, credential_type as "credential_type: _", +profile_picture_url, 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 index 4d4408b..9677d6e 100644 --- a/sql/agent/insert.sql +++ b/sql/agent/insert.sql @@ -1,11 +1,12 @@ INSERT INTO agent ( - id, full_name, credential, credential_type, time_created, last_updated + id, full_name, credential, credential_type, profile_picture_url, time_created, last_updated ) VALUES ( - $1, $2, $3, $4, $5, $5 + $1, $2, $3, $4, $5, $6, $6 ) RETURNING id, full_name, credential, credential_type as "credential_type: _", +profile_picture_url, time_created, last_updated; \ No newline at end of file diff --git a/sql/agent/update.sql b/sql/agent/update.sql index d83da50..b566b3b 100644 --- a/sql/agent/update.sql +++ b/sql/agent/update.sql @@ -2,12 +2,14 @@ UPDATE agent SET full_name = $1, credential = $2, credential_type = $3, - last_updated = $4 -WHERE id = $5 + profile_picture_url = $4, + last_updated = $5 +WHERE id = $6 RETURNING id, full_name, credential, credential_type as "credential_type: _", + profile_picture_url, time_created, last_updated; \ No newline at end of file diff --git a/src/dao/agent.rs b/src/dao/agent.rs index 56ab5fe..f0040aa 100644 --- a/src/dao/agent.rs +++ b/src/dao/agent.rs @@ -13,6 +13,7 @@ pub async fn insert_agent( agent.full_name, agent.credential, agent.credential_type as _, + agent.profile_picture_url, agent.time_created ) .fetch_one(tx) @@ -47,6 +48,7 @@ pub async fn update_agent( agent.full_name, agent.credential, agent.credential_type as _, + agent.profile_picture_url, agent.last_updated, agent.id ) diff --git a/src/services/admin.rs b/src/services/admin.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/services/mod.rs b/src/services/mod.rs index 8b13789..26a8412 100644 --- a/src/services/mod.rs +++ b/src/services/mod.rs @@ -1 +1,2 @@ - +pub mod admin; +pub mod read; \ No newline at end of file diff --git a/src/services/read.rs b/src/services/read.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/utils/macros.rs b/src/utils/macros.rs new file mode 100644 index 0000000..ea467b3 --- /dev/null +++ b/src/utils/macros.rs @@ -0,0 +1,57 @@ +/// This macro unwraps the value and if its an error it rolls back the transaction and returns a TypedHttpResponse with the corresponding erorr. +#[macro_export] +macro_rules! handle_db_write_op { + ($e:expr, $tx:expr) => { + match $e.await { + Ok(value) => value, + Err(error) => { + handle_tx!($tx.rollback()); + return actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_error(400, err::MessageResource::new_from_string(error.to_string())); + } + } + }; +} + +#[macro_export] +macro_rules! handle_db_read_op { + ($e:expr) => { + match $e.await { + Ok(value) => value, + Err(error) => return actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_error(400, err::MessageResource::new_from_string(error.to_string())), + } + }; +} + +/// This macro calls await on whatever you give it and if it gets an error it returns a TypedHttpResponse with an InternalServerError status code (500) and an error message. +#[macro_export] +macro_rules! handle_tx { + ($e:expr) => { + match $e.await { + Ok(value) => value, + Err(_) => { + return actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_error(500, err::MessageResource::new_from_str("Failed to acquire, commit or rollback tx...")); + } + } + }; +} + +/// This macro just returns a TypedHttpResponse with a success status code (200) and whatever you give it inside. +#[macro_export] +macro_rules! success { + ($e:expr) => { + return actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_response(200, $e) + }; +} + +/// This macro just returns a TypedHttpResponse with a not found status code (404) and an error concatenated. +/// The literal should be a subject in plural form: +/// Agent -> agents +#[macro_export] +macro_rules! unwrap_or_not_found { + ($e:expr, $what:literal) => { + match $e { + Some(value) => value, + None => return actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_error(404, err::MessageResource::new_from_string(format!("No {} found with specified Id.", $what))), + } + }; +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 8b13789..d28af63 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1 +1 @@ - +pub mod macros; \ No newline at end of file