Cargo fmt & All dao's basic done

This commit is contained in:
Franklin 2023-05-03 19:55:15 -04:00
parent fe0b804c83
commit f48d480ba9
9 changed files with 126 additions and 1 deletions

10
sql/click/insert.sql Normal file
View File

@ -0,0 +1,10 @@
INSERT INTO click (
id, realtor_id, clickable, trackable_id, time_created
) VALUES (
$1, $2, $3, $4, $5
) RETURNING
id,
realtor_id,
clickable as "clickable: _",
trackable_id,
time_created;

6
sql/view/insert.sql Normal file
View File

@ -0,0 +1,6 @@
INSERT INTO view (
id, thing_id, thing_pk, trackable_id, time_created
) VALUES (
$1, $2, $3, $4, $5
) RETURNING
id, thing_id, thing_pk as "thing_pk: _", trackable_id, time_created

View File

@ -1 +1,16 @@
use realtor_lp_types::domain::click::Click;
use sqlx::PgPool;
pub async fn insert_click(conn: &PgPool, click: Click) -> Result<Click, sqlx::Error> {
sqlx::query_file_as!(
Click,
"sql/click/insert.sql",
click.id,
click.realtor_id,
click.clickable as _,
click.trackable_id,
click.time_created
)
.fetch_one(conn)
.await
}

View File

@ -1,3 +1,4 @@
#[allow(unused)]
pub mod click;
#[allow(unused)]
pub mod project;
@ -7,3 +8,5 @@ pub mod property;
pub mod realtor;
#[allow(unused)]
pub mod trackable;
#[allow(unused)]
pub mod view;

16
src/dao/view.rs Normal file
View File

@ -0,0 +1,16 @@
use realtor_lp_types::domain::view::View;
use sqlx::PgPool;
pub async fn insert_view(conn: &PgPool, view: View) -> Result<View, sqlx::Error> {
sqlx::query_file_as!(
View,
"sql/view/insert.sql",
view.id,
view.thing_id,
view.thing_pk as _,
view.trackable_id,
view.time_created
)
.fetch_one(conn)
.await
}

0
src/services/project.rs Normal file
View File

0
src/services/realtor.rs Normal file
View File

74
src/utils/macros.rs Normal file
View File

@ -0,0 +1,74 @@
/// 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))),
}
};
}
/// This macro does the authentication needed for all the admin routes. give it a user and password as params.
#[macro_export]
macro_rules! auth {
($request:expr) => {
let __auth_token_header = match $request.headers().get("auth_token") {
Some(header_val) => {header_val.to_str().expect("ASCII Chars")},
None => {return actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_error(401, err::MessageResource::new_from_string(format!("No Token in header.")))}
};
match crate::utils::auth::get_token() {
Some(token) => if token == __auth_token_header {} else {
return actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_error(401, err::MessageResource::new_from_string(format!("Incorrect Token.")))
},
None => return actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_error(401, err::MessageResource::new_from_string(format!("No Token in backend. Please authenticate."))),
}
};
}

View File

@ -1 +1,2 @@
#[allow(unused)]
pub mod macros;