Reorg-ed
This commit is contained in:
@@ -0,0 +1 @@
|
||||
pub mod product_controller;
|
||||
@@ -0,0 +1,19 @@
|
||||
use crate::models::product::Product;
|
||||
use actix_web::{get, web, HttpResponse, Responder};
|
||||
|
||||
#[get("/products")]
|
||||
async fn find_all() -> impl Responder {
|
||||
let products = Product::find_all().expect("Error fetching all Products");
|
||||
HttpResponse::Ok().json(products)
|
||||
}
|
||||
|
||||
#[get("/product/{id}")]
|
||||
async fn find(id: web::Path<i32>) -> impl Responder {
|
||||
let product = Product::find(id.into_inner()).expect("Error fetching Product");
|
||||
HttpResponse::Ok().json(product)
|
||||
}
|
||||
|
||||
pub fn routes(cfg: &mut web::ServiceConfig) {
|
||||
cfg.service(find_all);
|
||||
cfg.service(find);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::api_error::ApiError;
|
||||
use crate::utils::api_error::ApiError;
|
||||
use diesel::pg::PgConnection;
|
||||
use diesel::r2d2::ConnectionManager;
|
||||
use lazy_static::lazy_static;
|
||||
@@ -14,18 +14,18 @@ lazy_static! {
|
||||
static ref POOL: Pool = {
|
||||
let db_url = env::var("DATABASE_URL").expect("Database url not set");
|
||||
let manager = ConnectionManager::<PgConnection>::new(db_url);
|
||||
Pool::new(manager).expect("Failed to create db pool")
|
||||
Pool::new(manager).expect("Failed to create config pool")
|
||||
};
|
||||
}
|
||||
|
||||
pub fn init() {
|
||||
info!("Initializing DB");
|
||||
lazy_static::initialize(&POOL);
|
||||
let conn = connection().expect("Failed to get db connection");
|
||||
let conn = get_connection().expect("Failed to get config connection");
|
||||
embedded_migrations::run(&conn).unwrap();
|
||||
}
|
||||
|
||||
pub fn connection() -> Result<DbConnection, ApiError> {
|
||||
pub fn get_connection() -> Result<DbConnection, ApiError> {
|
||||
POOL.get()
|
||||
.map_err(|e| ApiError::new(500, format!("Failed getting db connection: {}", e)))
|
||||
.map_err(|e| ApiError::new(500, format!("Failed getting config connection: {}", e)))
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
pub mod db_pool;
|
||||
|
||||
36
src/main.rs
36
src/main.rs
@@ -7,36 +7,30 @@ extern crate diesel_migrations;
|
||||
|
||||
use actix_web::{App, HttpServer};
|
||||
use dotenv::dotenv;
|
||||
use listenfd::ListenFd;
|
||||
use std::env;
|
||||
use crate::api::product_controller;
|
||||
use config::db_pool;
|
||||
|
||||
mod db;
|
||||
mod product;
|
||||
mod schema;
|
||||
mod api_error;
|
||||
mod utils;
|
||||
mod models;
|
||||
mod api;
|
||||
mod config;
|
||||
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
dotenv().ok();
|
||||
env_logger::init();
|
||||
|
||||
db::init();
|
||||
|
||||
let mut listenfd = ListenFd::from_env();
|
||||
let mut server = HttpServer::new(||
|
||||
App::new()
|
||||
.configure(product::init_routes)
|
||||
);
|
||||
|
||||
server = match listenfd.take_tcp_listener(0)? {
|
||||
Some(listener) => server.listen(listener)?,
|
||||
None => {
|
||||
let host = env::var("HOST").expect("Host not set in .env file");
|
||||
let port = env::var("PORT").expect("Port not set in .env file");
|
||||
server.bind(format!("{}:{}", host, port))?
|
||||
},
|
||||
};
|
||||
db_pool::init();
|
||||
|
||||
let bind_address = format!("{}:{}",
|
||||
env::var("HOST").expect("Host not set in .env file"),
|
||||
env::var("PORT").expect("Port not set in .env file"));
|
||||
info!("Starting Server");
|
||||
server.run().await
|
||||
HttpServer::new(||
|
||||
App::new()
|
||||
.configure(product_controller::routes)
|
||||
).bind(bind_address)?
|
||||
.run().await
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
pub mod product;
|
||||
@@ -0,0 +1,52 @@
|
||||
use crate::config::db_pool;
|
||||
use crate::schema::products;
|
||||
use chrono::{NaiveDateTime, Utc};
|
||||
use diesel::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::utils::api_error::ApiError;
|
||||
|
||||
#[derive(Serialize, Deserialize, AsChangeset)]
|
||||
#[table_name = "products"]
|
||||
pub struct ProductMessage {
|
||||
pub productid: i32,
|
||||
pub productname: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Queryable, Insertable)]
|
||||
#[table_name = "products"]
|
||||
pub struct Product {
|
||||
pub productid: i32,
|
||||
pub productname: String,
|
||||
pub created_at: NaiveDateTime
|
||||
}
|
||||
|
||||
impl Product {
|
||||
pub fn find_all() -> Result<Vec<Self>, ApiError> {
|
||||
let conn = db_pool::get_connection()?;
|
||||
|
||||
let products = products::table
|
||||
.load::<Product>(&conn)?;
|
||||
|
||||
Ok(products)
|
||||
}
|
||||
|
||||
pub fn find(id: i32) -> Result<Self, ApiError> {
|
||||
let conn = db_pool::get_connection()?;
|
||||
|
||||
let product = products::table
|
||||
.filter(products::productid.eq(id))
|
||||
.first(&conn)?;
|
||||
|
||||
Ok(product)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ProductMessage> for Product {
|
||||
fn from(product: ProductMessage) -> Self {
|
||||
Product {
|
||||
productid: product.productid,
|
||||
productname: product.productname,
|
||||
created_at: Utc::now().naive_utc(),
|
||||
}
|
||||
}
|
||||
}
|
||||
32
src/schema.rs
Normal file
32
src/schema.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
table! {
|
||||
order_details (orderid, productid) {
|
||||
orderid -> Int4,
|
||||
productid -> Int4,
|
||||
quantity -> Int4,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
orders (orderid) {
|
||||
orderid -> Int4,
|
||||
customer_email -> Text,
|
||||
created_at -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
products (productid) {
|
||||
productid -> Int4,
|
||||
productname -> Text,
|
||||
created_at -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
joinable!(order_details -> orders (orderid));
|
||||
joinable!(order_details -> products (productid));
|
||||
|
||||
allow_tables_to_appear_in_same_query!(
|
||||
order_details,
|
||||
orders,
|
||||
products,
|
||||
);
|
||||
13
src/utils/constants.rs
Normal file
13
src/utils/constants.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
// Messages
|
||||
// pub const MESSAGE_OK: &str = "ok";
|
||||
// pub const MESSAGE_CAN_NOT_FETCH_DATA: &str = "Can not fetch data";
|
||||
// pub const MESSAGE_CAN_NOT_INSERT_DATA: &str = "Can not insert data";
|
||||
// pub const MESSAGE_CAN_NOT_UPDATE_DATA: &str = "Can not update data";
|
||||
// pub const MESSAGE_CAN_NOT_DELETE_DATA: &str = "Can not delete data";
|
||||
// pub const MESSAGE_INTERNAL_SERVER_ERROR: &str = "Internal Server Error";
|
||||
//
|
||||
// // Misc
|
||||
// pub const EMPTY: &str = "";
|
||||
//
|
||||
// // ignore routes
|
||||
// pub const IGNORE_ROUTES: [&str; 3] = ["/api/ping", "/api/auth/signup", "/api/auth/login"];
|
||||
@@ -0,0 +1,2 @@
|
||||
pub mod api_error;
|
||||
pub mod constants;
|
||||
|
||||
Reference in New Issue
Block a user