From c168bca044e7ae2ad74205eaa3f622910b944f9b Mon Sep 17 00:00:00 2001 From: Franklin Date: Thu, 4 May 2023 14:57:16 -0400 Subject: [PATCH] View services --- src/dao/view.rs | 6 +++--- src/routes/view.rs | 11 +++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/dao/view.rs b/src/dao/view.rs index c88eed0..519d6b1 100644 --- a/src/dao/view.rs +++ b/src/dao/view.rs @@ -1,7 +1,7 @@ use realtor_lp_types::domain::view::View; -use sqlx::PgPool; +use sqlx::{PgPool, Transaction, Postgres}; -pub async fn insert_view(conn: &PgPool, view: View) -> Result { +pub async fn insert_view<'a>(transaction: &mut Transaction<'a, Postgres>, view: View) -> Result { sqlx::query_file_as!( View, "sql/view/insert.sql", @@ -11,6 +11,6 @@ pub async fn insert_view(conn: &PgPool, view: View) -> Result view.trackable_id, view.time_created ) - .fetch_one(conn) + .fetch_one(transaction) .await } diff --git a/src/routes/view.rs b/src/routes/view.rs index 8b13789..bc6b738 100644 --- a/src/routes/view.rs +++ b/src/routes/view.rs @@ -1 +1,12 @@ +use actix_web_utils::extensions::typed_response::TypedHttpResponse; +use realtor_lp_types::dto::payloads::view::ViewForCreationPayload; +use sqlx::PgPool; +use crate::{dao, handle_tx, handle_db_write_op, success}; + + +pub async fn new_view(conn: &PgPool, view: ViewForCreationPayload) -> TypedHttpResponse<()> { + let mut transaction = handle_tx!(conn.begin()); + handle_db_write_op!(dao::view::insert_view(&mut transaction, view.into()), transaction); + success!(()) +} \ No newline at end of file