Initial commit
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
pub mod models;
|
||||
pub mod schema;
|
||||
|
||||
use anyhow::Result;
|
||||
use diesel::prelude::*;
|
||||
use dotenv::dotenv;
|
||||
use std::env;
|
||||
|
||||
use self::{models::*, schema::*};
|
||||
|
||||
pub fn establish_connection() -> SqliteConnection {
|
||||
dotenv().ok();
|
||||
|
||||
let db_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||
SqliteConnection::establish(&db_url)
|
||||
.unwrap_or_else(|_| panic!("Error connecting to {}", db_url))
|
||||
}
|
||||
|
||||
pub fn get_products() -> Vec<Product> {
|
||||
use self::schema::products::dsl::*;
|
||||
let conn = &mut establish_connection();
|
||||
products
|
||||
.load::<Product>(conn)
|
||||
.expect("Error loading products")
|
||||
}
|
||||
|
||||
pub fn get_products_from_order(order: &Order, conn: &mut SqliteConnection) -> Vec<Product> {
|
||||
let res: Vec<(OrderProduct, Product)> = OrderProduct::belonging_to(order)
|
||||
.inner_join(products::table)
|
||||
.get_results(conn);
|
||||
// Strip off all the elements from the linking table
|
||||
res.into_iter().map(|(_ri, i)| i).collect()
|
||||
}
|
||||
|
||||
pub fn create_product(
|
||||
conn: &mut SqliteConnection,
|
||||
name: &str,
|
||||
price: &f32,
|
||||
owner: &str,
|
||||
) -> Product {
|
||||
let new_product = NewProduct { name, price, owner };
|
||||
|
||||
diesel::insert_into(products::table)
|
||||
.values(&new_product)
|
||||
.get_result(conn)
|
||||
.expect("Error saving new product")
|
||||
}
|
||||
|
||||
pub fn create_order(
|
||||
conn: &mut SqliteConnection,
|
||||
products: Vec<Product>,
|
||||
) -> Result<(Order, Vec<Product>)> {
|
||||
let order = diesel::insert_into(orders::table)
|
||||
.default_values()
|
||||
.get_result(conn)?;
|
||||
|
||||
for product in products {
|
||||
let product_exists = diesel::select(diesel::dsl::exists(products.filter(name.eq(product.name))))
|
||||
.get_result(conn);
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use dotenv;
|
||||
use sqlx::{sqlite::SqlitePoolOptions, SqlitePool};
|
||||
use tide::{Body, Request, Result};
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
struct Product {
|
||||
name: String,
|
||||
}
|
||||
|
||||
#[async_std::main]
|
||||
async fn main() -> Result<()> {
|
||||
dotenv::dotenv().ok();
|
||||
pretty_env_logger::init();
|
||||
|
||||
let db_pool = make_db_pool().await;
|
||||
let mut app = tide::new();
|
||||
app.at("/products").post(create_product);
|
||||
app.listen("127.0.0.1:8080").await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn make_db_pool() -> SqlitePool {
|
||||
let db_url =
|
||||
std::env::var("DATABASE_URL").expect("Environment variable DATABASE_URL must be set");
|
||||
let db_pool = SqlitePoolOptions::new()
|
||||
.max_connections(2)
|
||||
.connect(&format!("sqlite://{}?mode=rwc", &db_url))
|
||||
.await
|
||||
.expect("Could not connect to database");
|
||||
db_pool
|
||||
}
|
||||
|
||||
async fn create_product(mut req: Request<()>) -> Result<Body> {
|
||||
let product: Product = req.body_json().await?;
|
||||
println!("product name: {}", product.name);
|
||||
|
||||
let product = Product {
|
||||
name: "chashu".into(),
|
||||
};
|
||||
|
||||
Body::from_json(&product)
|
||||
}
|
||||
Reference in New Issue
Block a user