diff --git a/sql/click/insert.sql b/sql/click/insert.sql new file mode 100644 index 0000000..36540c3 --- /dev/null +++ b/sql/click/insert.sql @@ -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; \ No newline at end of file diff --git a/sql/view/insert.sql b/sql/view/insert.sql new file mode 100644 index 0000000..40d11c3 --- /dev/null +++ b/sql/view/insert.sql @@ -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 \ No newline at end of file diff --git a/src/dao/click.rs b/src/dao/click.rs index 8b13789..ed65b9b 100644 --- a/src/dao/click.rs +++ b/src/dao/click.rs @@ -1 +1,16 @@ +use realtor_lp_types::domain::click::Click; +use sqlx::PgPool; +pub async fn insert_click(conn: &PgPool, click: Click) -> Result { + 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 +} diff --git a/src/dao/mod.rs b/src/dao/mod.rs index 48e8134..538bf95 100644 --- a/src/dao/mod.rs +++ b/src/dao/mod.rs @@ -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; diff --git a/src/dao/view.rs b/src/dao/view.rs new file mode 100644 index 0000000..c88eed0 --- /dev/null +++ b/src/dao/view.rs @@ -0,0 +1,16 @@ +use realtor_lp_types::domain::view::View; +use sqlx::PgPool; + +pub async fn insert_view(conn: &PgPool, view: View) -> Result { + 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 +} diff --git a/src/services/project.rs b/src/services/project.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/services/realtor.rs b/src/services/realtor.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..3728739 --- /dev/null +++ b/src/utils/macros.rs @@ -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."))), + } + }; +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 8b13789..c21ce77 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1 +1,2 @@ - +#[allow(unused)] +pub mod macros;