Added macros, fixed agent not having pfp in all queries
This commit is contained in:
parent
a11a35b767
commit
f4c3b70f19
@ -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
|
||||
);
|
@ -3,6 +3,7 @@ SELECT
|
||||
full_name,
|
||||
credential,
|
||||
credential_type as "credential_type: _",
|
||||
profile_picture_url,
|
||||
time_created,
|
||||
last_updated
|
||||
FROM agent
|
||||
|
@ -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;
|
@ -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
|
@ -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;
|
@ -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;
|
@ -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
|
||||
)
|
||||
|
0
src/services/admin.rs
Normal file
0
src/services/admin.rs
Normal file
@ -1 +1,2 @@
|
||||
|
||||
pub mod admin;
|
||||
pub mod read;
|
0
src/services/read.rs
Normal file
0
src/services/read.rs
Normal file
57
src/utils/macros.rs
Normal file
57
src/utils/macros.rs
Normal file
@ -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))),
|
||||
}
|
||||
};
|
||||
}
|
@ -1 +1 @@
|
||||
|
||||
pub mod macros;
|
Loading…
Reference in New Issue
Block a user