Added service methods to project, location, agent

This commit is contained in:
Franklin 2023-03-19 12:22:33 -04:00
parent f4c3b70f19
commit 30c9f86edb
9 changed files with 1364 additions and 10 deletions

12
Cargo.lock generated
View File

@ -543,7 +543,7 @@ dependencies = [
"proc-macro2",
"quote",
"scratch",
"syn 2.0.0",
"syn 2.0.2",
]
[[package]]
@ -560,7 +560,7 @@ checksum = "631569015d0d8d54e6c241733f944042623ab6df7bc3be7466874b05fcdb1c5f"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.0",
"syn 2.0.2",
]
[[package]]
@ -1577,7 +1577,7 @@ checksum = "78997f4555c22a7971214540c4a661291970619afd56de19f77e0de86296e1e5"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.0",
"syn 2.0.2",
]
[[package]]
@ -1809,9 +1809,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.0"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4cff13bb1732bccfe3b246f3fdb09edfd51c01d6f5299b7ccd9457c2e4e37774"
checksum = "59d3276aee1fa0c33612917969b5172b5be2db051232a6e4826f1a1a9191b045"
dependencies = [
"proc-macro2",
"quote",
@ -1844,7 +1844,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.0",
"syn 2.0.2",
]
[[package]]

View File

@ -0,0 +1 @@
SELECT * FROM location;

1263
sqlx-data.json Normal file

File diff suppressed because it is too large Load Diff

View File

@ -40,7 +40,7 @@ pub async fn get_agents_with_ids(
pub async fn update_agent(
tx: &mut Transaction<'_, Postgres>,
agent: Agent,
agent: &Agent,
) -> Result<Agent, sqlx::Error> {
sqlx::query_file_as!(
Agent,

View File

@ -4,7 +4,7 @@ use uuid::Uuid;
pub async fn insert_location(
tx: &mut Transaction<'_, Postgres>,
location: Location,
location: &Location,
) -> Result<Location, sqlx::Error> {
sqlx::query_file_as!(
Location,
@ -35,6 +35,14 @@ pub async fn get_locations_in_city(
.await
}
pub async fn fetch_all_locations(
conn: &PgPool,
) -> Result<Vec<Location>, sqlx::Error> {
sqlx::query_file_as!(Location, "sql/location/fetch_all.sql")
.fetch_all(conn)
.await
}
pub async fn delete_location(
tx: &mut Transaction<'_, Postgres>,
location_id: Uuid,

View File

@ -0,0 +1,81 @@
use actix_web_utils::extensions::typed_response::TypedHttpResponse;
use jl_types::{
domain::{agent::Agent, project::Project, location::Location},
dto::payloads::{agent::{NewAgentPayload, UpdateAgentPayload}, project::{NewProjectPayload, UpdateProjectPayload}, location::NewLocationPayload},
};
use sqlx::PgPool;
use crate::{dao, handle_db_read_op, handle_db_write_op, handle_tx, success, unwrap_or_not_found};
//
// Insert Methods
//
pub async fn create_new_agent_profile(
conn: &PgPool,
new_agent: NewAgentPayload,
) -> TypedHttpResponse<Agent> {
let mut tx = handle_tx!(conn.begin());
let persisted_agent =
handle_db_write_op!(dao::agent::insert_agent(&mut tx, &new_agent.into()), tx);
handle_tx!(tx.commit());
success!(persisted_agent)
}
pub async fn create_new_location(
conn: &PgPool,
new_location: NewLocationPayload,
) -> TypedHttpResponse<Location> {
let mut tx = handle_tx!(conn.begin());
let persisted_location = handle_db_write_op!(dao::location::insert_location(&mut tx, &new_location.into()), tx);
handle_tx!(tx.commit());
success!(persisted_location)
}
pub async fn create_new_project(
conn: &PgPool,
new_project: NewProjectPayload,
) -> TypedHttpResponse<Project> {
let mut tx = handle_tx!(conn.begin());
unwrap_or_not_found!(handle_db_read_op!(dao::agent::get_agent_with_id(conn, &new_project.agent_id)), "agents");
unwrap_or_not_found!(handle_db_read_op!(dao::location::get_location_with_id(conn, &new_project.location_id)), "locations");
let persisted_project = handle_db_write_op!(dao::project::insert(&mut tx, &new_project.into()), tx);
handle_tx!(tx.commit());
success!(persisted_project)
}
//
// Update Methods
//
pub async fn update_agent(
conn: &PgPool,
update_agent_payload: UpdateAgentPayload,
) -> TypedHttpResponse<Agent> {
let mut tx = handle_tx!(conn.begin());
let mut persisted_agent = unwrap_or_not_found!(
handle_db_read_op!(dao::agent::get_agent_with_id(
conn,
&update_agent_payload.id
)),
"agents"
);
update_agent_payload.update_agent(&mut persisted_agent);
let updated_agent =
handle_db_write_op!(dao::agent::update_agent(&mut tx, &persisted_agent), tx);
handle_tx!(tx.commit());
success!(updated_agent)
}
pub async fn update_project(conn: &PgPool, update_project_payload: UpdateProjectPayload) -> TypedHttpResponse<Project> {
let mut tx = handle_tx!(conn.begin());
let mut persisted_project = unwrap_or_not_found!(handle_db_read_op!(dao::project::get_with_id(conn, &update_project_payload.id)), "projects");
update_project_payload.update_project(&mut persisted_project);
let updated_project = handle_db_write_op!(dao::project::update(&mut tx, &persisted_project), tx);
handle_tx!(tx.commit());
success!(updated_project)
}

View File

@ -1,2 +1,2 @@
pub mod admin;
pub mod read;
pub mod read;

View File

@ -0,0 +1 @@

View File

@ -1 +1 @@
pub mod macros;
pub mod macros;