From a6cfcb89d02aa9be3acb25e79d4833d71cf5fb5c Mon Sep 17 00:00:00 2001 From: Franklin Date: Fri, 14 Apr 2023 17:00:42 -0400 Subject: [PATCH] Added all new routes --- src/routes/admin.rs | 21 +++++++++++++++++++-- src/routes/main_router.rs | 8 ++++++-- src/routes/read.rs | 8 ++++++-- src/services/admin.rs | 17 ++++++++++++++++- src/services/read.rs | 7 ++++++- 5 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/routes/admin.rs b/src/routes/admin.rs index d3086c3..2efd841 100644 --- a/src/routes/admin.rs +++ b/src/routes/admin.rs @@ -2,11 +2,11 @@ use std::sync::Arc; use actix_web::{ delete, post, put, - web::{self, Path}, + web::{self, Path}, get, }; use actix_web_utils::extensions::typed_response::TypedHttpResponse; use jl_types::{ - domain::{agent::Agent, location::Location, project::Project, unit::Unit}, + domain::{agent::Agent, location::Location, project::Project, unit::Unit, count::Count, contact::Contact}, dto::payloads::{ agent::{NewAgentPayload, UpdateAgentPayload}, location::NewLocationPayload, @@ -106,3 +106,20 @@ pub async fn delete_unit( ) -> TypedHttpResponse<()> { services::admin::delete_unit(&db_conn, &unit_id).await } + +#[get("visits/count")] +pub async fn get_visits_count(db_conn: web::Data>) -> TypedHttpResponse { + services::admin::get_all_page_visits(&db_conn).await +} + +#[get("contacts/count")] +pub async fn get_contacts_count(db_conn: web::Data>) -> TypedHttpResponse { + services::admin::get_contact_count(&db_conn).await +} + +#[get("contacts")] +pub async fn get_all_contacts(db_conn: web::Data>) -> TypedHttpResponse> { + services::admin::get_all_contacts(&db_conn).await +} + + diff --git a/src/routes/main_router.rs b/src/routes/main_router.rs index e5ce445..5e4616a 100644 --- a/src/routes/main_router.rs +++ b/src/routes/main_router.rs @@ -30,13 +30,17 @@ pub async fn start_all_routes(start_time: i64, db_conn: Arc) -> Result<( .service(super::admin::delete_agent) .service(super::admin::delete_location) .service(super::admin::delete_project) - .service(super::admin::delete_unit)) + .service(super::admin::delete_unit) + .service(super::admin::get_all_contacts) + .service(super::admin::get_contacts_count) + .service(super::admin::get_visits_count)) .service(web::scope("/read") .service(super::read::get_all_agents) .service(super::read::get_all_locations) .service(super::read::get_locations_in_city) .service(super::read::get_projects_paged) - .service(super::read::get_project_data)) + .service(super::read::get_project_data) + .service(super::read::create_contact_request)) }) .bind((HOST_ADDR, HOST_PORT))? .run(); diff --git a/src/routes/read.rs b/src/routes/read.rs index 804e9d7..6aae495 100644 --- a/src/routes/read.rs +++ b/src/routes/read.rs @@ -2,7 +2,7 @@ use std::{collections::{HashMap, HashSet}, str::FromStr, sync::Arc}; use actix_web::{ get, - web::{self, Path}, HttpRequest, + web::{self, Path}, HttpRequest, post, }; use actix_web_utils::extensions::typed_response::TypedHttpResponse; use err::MessageResource; @@ -11,7 +11,7 @@ use jl_types::{ agent::Agent, project_condition::ProjectCondition, project_type::ProjectType, }, - dto::{filters::Filter, listing::Listing, project_card::ProjectCardDto}, + dto::{filters::Filter, listing::Listing, project_card::ProjectCardDto, payloads::contact::ContactPayload}, }; use sqlx::PgPool; use uuid::Uuid; @@ -60,6 +60,10 @@ pub async fn get_project_data( ) -> TypedHttpResponse { services::read::get_project_data(&db_conn, &project_id).await } +#[post("/contact")] +pub async fn create_contact_request(db_conn: web::Data>, contact: web::Json) -> TypedHttpResponse<()> { + services::read::create_contact(&db_conn, contact.0).await +} fn parse_params_into_filters( params: HashMap, diff --git a/src/services/admin.rs b/src/services/admin.rs index 1c22953..64d5bee 100644 --- a/src/services/admin.rs +++ b/src/services/admin.rs @@ -1,6 +1,6 @@ use actix_web_utils::extensions::typed_response::TypedHttpResponse; use jl_types::{ - domain::{agent::Agent, location::Location, project::Project, unit::Unit}, + domain::{agent::Agent, location::Location, project::Project, unit::Unit, count::Count, contact::Contact}, dto::payloads::{ agent::{NewAgentPayload, UpdateAgentPayload}, location::NewLocationPayload, @@ -177,3 +177,18 @@ pub async fn delete_unit(conn: &PgPool, unit_id: &Uuid) -> TypedHttpResponse<()> handle_tx!(tx.commit()); success!(()) } + +pub async fn get_all_page_visits(conn: &PgPool) -> TypedHttpResponse { + let count = handle_db_read_op!(dao::visit::get_count(conn)); + success!(count) +} + +pub async fn get_all_contacts(conn: &PgPool) -> TypedHttpResponse> { + let contacts = handle_db_read_op!(dao::contact::get_all(conn)); + success!(contacts) +} + +pub async fn get_contact_count(conn: &PgPool) -> TypedHttpResponse { + let count = handle_db_read_op!(dao::contact::get_count(conn)); + success!(count) +} \ No newline at end of file diff --git a/src/services/read.rs b/src/services/read.rs index 6608a3c..d3ced30 100644 --- a/src/services/read.rs +++ b/src/services/read.rs @@ -3,7 +3,7 @@ use std::collections::HashSet; use actix_web_utils::extensions::typed_response::TypedHttpResponse; use jl_types::{ domain::{agent::Agent}, - dto::{filters::Filter, listing::Listing, project_card::ProjectCardDto}, + dto::{filters::Filter, listing::Listing, project_card::ProjectCardDto, payloads::contact::ContactPayload}, }; use sqlx::PgPool; use uuid::Uuid; @@ -60,3 +60,8 @@ pub async fn get_project_data(conn: &PgPool, project_id: &Uuid) -> TypedHttpResp ); success!(Listing::new(project, units, location, agent)) } + +pub async fn create_contact(conn: &PgPool, contact: ContactPayload) -> TypedHttpResponse<()> { + let _ = handle_db_read_op!(dao::contact::insert(conn, &contact.into())); + success!(()) +} \ No newline at end of file