Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
KrisPett committed Apr 25, 2024
1 parent 105167b commit 37664b7
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 31 deletions.
1 change: 1 addition & 0 deletions api/src/configs/init_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ async fn create_house(dbc: &DatabaseConnection, mut broker: ActiveModel, src: St
let description = "Welcome to this bright and well-planned four-bedroom apartment with a balcony in a private location and a view of greenery! The residence features well-organized rooms and substantial windows in three different directions, providing a delightful infusion of natural light throughout the entire apartment. You'll find a spacious living room with comfortable seating areas and access to the pleasant balcony, offering sunny exposure and a lovely view of the green surroundings. Additionally, the apartment boasts a spacious kitchen with room for a dining area for the whole family, and here too, you can enjoy a pleasant view of the green area outside.\n\nThis well-planned apartment includes three good-sized bedrooms. Conveniently, for larger families, it offers both a fully tiled bathroom with a washing machine and a guest WC. Ample storage options are available through closets and a walk-in closet.\n\nYou are warmly welcome to visit!";

let house = entity_helper::new_house(
String::new(),
String::from(location),
String::from(description),
String::from(country),
Expand Down
4 changes: 2 additions & 2 deletions api/src/helpers/entity_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ pub fn new_broker(name: String) -> entity::brokers::ActiveModel {
}
}

pub fn new_house(location: String, description: String, country: String, src: String, city: String, price: i64, house_types: HouseTypes) -> entity::houses::ActiveModel {
pub fn new_house(title: String, location: String, description: String, country: String, src: String, city: String, price: i64, house_types: HouseTypes) -> entity::houses::ActiveModel {
entity::houses::ActiveModel {
id: Set(Uuid::new_v4().to_string()),
created_at: Set(DateTimeWithTimeZone::from(chrono::Utc::now())),
updated_at: Set(DateTimeWithTimeZone::from(chrono::Utc::now())),
title: Set(None),
title: Set(Option::from(title)),
description: Set(Option::from(description)),
location: Set(Option::from(location)),
country: Set(Option::from(country)),
Expand Down
25 changes: 18 additions & 7 deletions api/src/routes/add_property_page/add_property_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::future::Future;
use std::io::Error;
use std::sync::{Arc, Mutex, MutexGuard};

use actix_web::{get, HttpResponse, web};
use actix_web::{get, HttpResponse, post, web};
use sea_orm::{ActiveEnum, DatabaseConnection};
use serde::{Deserialize, Serialize};

Expand All @@ -11,27 +11,36 @@ use entity::houses::Model as House;

use crate::AppState;
use crate::helpers::dto_builder_helpers::{country_helper, house_helper};
use crate::services::house_service;
use crate::services::house_service::HouseServiceI;

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct AddPropertyPageDTO {
id: String
id: String,
}

#[derive(Debug, Clone)]
struct DTOBuilder {
id: String,
}

#[get("/add-property")]
pub async fn get_add_property_page(data: web::Data<AppState>) -> Result<HttpResponse, Error> {
#[derive(Debug, Deserialize)]
struct CreatePropertyReqBody {
title: String,
description: String,
supply: String,
}

#[post("/add-property")]
pub async fn get_add_property_page(data: web::Data<AppState>, body: web::Json<CreatePropertyReqBody>) -> Result<HttpResponse, Error> {
log::info!("get_add_property_page");
let dbc = Arc::new(data.dbc.clone());
let dto = build_dto(&dbc, |_builder| async { Ok(()) }).await?;
let dto = build_dto(&dbc, &body, |_builder| async { Ok(()) }).await?;
Ok(HttpResponse::Ok().json(dto))
}
// TODO
async fn build_dto<F, Fut>(dbc: &Arc<DatabaseConnection>, additional_processing: F) -> Result<AddPropertyPageDTO, Error>

async fn build_dto<F, Fut>(dbc: &Arc<DatabaseConnection>, body: &web::Json<CreatePropertyReqBody>, additional_processing: F) -> Result<AddPropertyPageDTO, Error>
where
F: FnOnce(&Arc<Mutex<DTOBuilder>>) -> Fut,
Fut: Future<Output=Result<(), Error>>,
Expand All @@ -42,6 +51,8 @@ async fn build_dto<F, Fut>(dbc: &Arc<DatabaseConnection>, additional_processing:

additional_processing(&dto_builder).await?;

house_service::HouseService.create_house(dbc, &body.title, &body.description).await?;

let dto_builder_lock = dto_builder.lock().unwrap();
Ok(to_home_page_dto(dto_builder_lock.clone()))
}
Expand Down
3 changes: 1 addition & 2 deletions api/src/routes/add_property_page/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use actix_web::web;
use crate::routes::account_page::account_page;

pub mod add_property_page;

pub fn init_routes(cfg: &mut web::ServiceConfig) {
cfg.service(account_page::get_account_page);
cfg.service(add_property_page::get_add_property_page);
}
2 changes: 1 addition & 1 deletion api/src/routes/house_page/house_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct DTOBuilder {
}

#[get("/house/{house_id}")]
pub async fn get_house_page(data: web::Data<AppState>, house_id: web::Path<String>) -> Result<HttpResponse, Error> {
pub async fn get_house_page(data: web::Data<AppState>, house_id: Path<String>) -> Result<HttpResponse, Error> {
log::info!("get_house_page");
log::info!("house_id: {}" ,house_id);
let dbc = Arc::new(data.dbc.clone());
Expand Down
28 changes: 9 additions & 19 deletions api/src/services/house_service.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
use std::io::Error;

use futures::TryFutureExt;
use sea_orm::{ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter, Set};
use sea_orm::prelude::DateTimeWithTimeZone;
use uuid::Uuid;
use sea_orm::{ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter};

use entity::brokers::Model as BrokerModel;
use entity::countries;
use entity::countries::Model as CountryModel;
use entity::house_images::Model as HouseImagesModel;
use entity::houses::Model as HouseModel;
use entity::prelude::Houses;
use entity::sea_orm_active_enums::CountryName;
use entity::sea_orm_active_enums::HouseTypes;

use crate::helpers::entity_helper;
use crate::middlewares::errors::CustomErrors;

pub trait HouseServiceI {
async fn insert_country(&self, dbc: &DatabaseConnection) -> Result<CountryModel, Error>;
async fn create_house(&self, dbc: &DatabaseConnection, title: &String, description: &String) -> Result<HouseModel, Error>;
async fn find_all_houses(&self, db_conn: &DatabaseConnection) -> Result<Vec<HouseModel>, Error>;
async fn find_house_by_id(&self, db_conn: &DatabaseConnection, id: &String) -> Result<Option<HouseModel>, Error>;
async fn find_broker_by_house_id(&self, db_conn: &DatabaseConnection, house_id: &String) -> Result<Option<BrokerModel>, Error>;
Expand All @@ -26,18 +23,11 @@ pub trait HouseServiceI {
pub struct HouseService;

impl HouseServiceI for HouseService {
async fn insert_country(&self, dbc: &DatabaseConnection) -> Result<CountryModel, Error> {
let country = countries::ActiveModel {
id: Set(String::from(Uuid::new_v4())),
created_at: Set(DateTimeWithTimeZone::from(chrono::Utc::now())),
updated_at: Set(DateTimeWithTimeZone::from(chrono::Utc::now())),
country_name: Set(CountryName::Spain),
};

match country.insert(dbc).await {
Ok(model) => Ok(model),
Err(err) => Err(Error::from(CustomErrors::DatabaseError(err)))
}
async fn create_house(&self, dbc: &DatabaseConnection, title: &String, description: &String) -> Result<HouseModel, Error> {
let house = entity_helper::new_house(String::from(title), String::new(), String::from(description), String::new(), String::new(), String::new(), 0, HouseTypes::Condominium);
house.insert(dbc)
.await
.map_err(|err| Error::from(CustomErrors::DatabaseError(err)))
}

async fn find_all_houses(&self, db_conn: &DatabaseConnection) -> Result<Vec<HouseModel>, Error> {
Expand Down

0 comments on commit 37664b7

Please sign in to comment.