Sql migrations created and main file

This commit is contained in:
Franklin 2023-03-15 09:06:42 -04:00
commit 39d97f3660
18 changed files with 2497 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
.env

2344
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

26
Cargo.toml Normal file
View File

@ -0,0 +1,26 @@
[package]
name = "jl-backend"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dotenvy = "0.15.6"
dotenvy_macro = "0.15.1"
tokio = {version = "1.20.1", features = ["full"]}
reqwest = { version = "0.11.11", features = ["rustls-tls", "json", "blocking"], default-features = false }
chrono = "0.4.23"
chrono-tz = "0.8"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.88"
sqlx = { version = "0.6.0", features = [ "runtime-tokio-rustls", "postgres", "chrono", "uuid", "offline" ] }
dotenv = { version = "0.15.0" }
actix-web = {version = "4.1.0"}
actix-cors = "0.6.2"
uuid = { version = "1.3.0", features = ["v4", "fast-rng", "macro-diagnostics"] }
actix-web-utils = { git = "https://git.franklinblanco.dev/franklinblanco/actix-web-utils.git" }
err = { git = "https://git.franklinblanco.dev/franklinblanco/err.git" }
jl-types = { path = "../jl-types" }

14
migrations/1_project.sql Normal file
View File

@ -0,0 +1,14 @@
CREATE TABLE IF NOT EXISTS "project" (
id UUID PRIMARY KEY,
project_type VARCHAR NOT NULL,
project_condition VARCHAR NOT NULL,
agent_id UUID NOT NULL,
location_id UUID NOT NULL,
title VARCHAR,
description TEXT NOT NULL,
admin_tag VARCHAR,
floors SMALLINT NOT NULL,
media TEXT NOT NULL,
time_created TIMESTAMPTZ NOT NULL,
last_updated TIMESTAMPTZ NOT NULL
);

8
migrations/2_agent.sql Normal file
View File

@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS "agent" (
id UUID PRIMARY KEY,
full_name VARCHAR NOT NULL,
credential VARCHAR NOT NULL,
credential_type VARCHAR NOT NULL,
time_created TIMESTAMPTZ NOT NULL,
last_updated TIMESTAMPTZ NOT NULL
);

View File

@ -0,0 +1,5 @@
CREATE TABLE IF NOT EXISTS "location" (
id UUID PRIMARY KEY,
city VARCHAR NOT NULL,
district VARCHAR NOT NULL
);

16
migrations/4_unit.sql Normal file
View File

@ -0,0 +1,16 @@
CREATE TABLE IF NOT EXISTS "unit" (
id UUID PRIMARY KEY,
project_id UUID NOT NULL,
price_usd FLOAT8 NOT NULL,
unit_type VARCHAR NOT NULL,
rooms SMALLINT NOT NULL,
bathrooms SMALLINT NOT NULL,
area FLOAT4 NOT NULL,
description TEXT NOT NULL,
media TEXT NOT NULL,
admin_tag VARCHAR,
time_created TIMESTAMPTZ NOT NULL,
last_updated TIMESTAMPTZ NOT NULL
);

0
src/dao/agent.rs Normal file
View File

0
src/dao/location.rs Normal file
View File

10
src/dao/main_dao.rs Normal file
View File

@ -0,0 +1,10 @@
use dotenvy_macro::dotenv;
use sqlx::PgPool;
pub async fn connect_to_database() -> Result<PgPool, sqlx::Error> {
let db_url: &str = dotenv!("DATABASE_URL");
let formatted_db_url = &format!("{db_url}");
sqlx::PgPool::connect(&formatted_db_url).await
}

6
src/dao/mod.rs Normal file
View File

@ -0,0 +1,6 @@
pub mod main_dao;
pub mod project;
pub mod agent;
pub mod location;
pub mod unit;

0
src/dao/project.rs Normal file
View File

0
src/dao/unit.rs Normal file
View File

21
src/main.rs Normal file
View File

@ -0,0 +1,21 @@
use std::sync::Arc;
use chrono::Utc;
use dao::main_dao;
pub mod dao;
pub mod routes;
pub mod services;
pub mod utils;
#[tokio::main]
async fn main() {
let start_time = Utc::now().timestamp_millis();
let db_conn = Arc::new(main_dao::connect_to_database().await.unwrap());
// Pass shared state to server and start it
routes::main_router::start_all_routes(start_time, db_conn)
.await
.unwrap();
}

44
src/routes/main_router.rs Normal file
View File

@ -0,0 +1,44 @@
use std::sync::Arc;
use actix_cors::Cors;
use actix_web::{web, App, HttpServer};
use chrono::Utc;
use reqwest::Client;
use sqlx::PgPool;
pub const HOST_ADDR: &str = "0.0.0.0";
pub const HOST_PORT: u16 = 8095;
pub async fn start_all_routes(start_time: i64, db_conn: Arc<PgPool>) -> Result<(), std::io::Error> {
let client_state = web::Data::new(Arc::new(Client::new()));
// Start server code that turns into a future to be executed below
let server_future = HttpServer::new(move || {
let cors_policy = Cors::permissive();
App::new()
.wrap(cors_policy)
.app_data(client_state.clone())
.app_data(web::Data::new(db_conn.clone()))
.service(web::scope("/admin")
/*.service(super::admin::create_new_agent_profile)
.service(super::admin::create_new_location)
.service(super::admin::create_new_property)
.service(super::admin::update_agent_info)
.service(super::admin::update_listing))
.service(web::scope("/read")
.service(super::read::get_all_agents)
.service(super::read::get_listing_container)
.service(super::read::get_property_listings_paged)*/)
})
.bind((HOST_ADDR, HOST_PORT))?
.run();
after_startup_fn(start_time);
server_future.await
}
pub fn after_startup_fn(start_time: i64) {
println!(
"Server took {}ms to setup",
Utc::now().timestamp_millis() - start_time
);
}

1
src/routes/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod main_router;

0
src/services/mod.rs Normal file
View File

0
src/utils/mod.rs Normal file
View File