From 67e5016829787c2398dcb1ceea6b6dca192ac27c Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 16 Nov 2020 19:45:42 +0530 Subject: add product table --- src/handlers/product.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/handlers/product.rs (limited to 'src') diff --git a/src/handlers/product.rs b/src/handlers/product.rs new file mode 100644 index 0000000..e655e9d --- /dev/null +++ b/src/handlers/product.rs @@ -0,0 +1,45 @@ +use crate::models::{NewProduct, Product}; +use crate::schema::product::dsl::*; +use crate::TPool; + +use actix_identity::Identity; +use actix_web::{web, HttpResponse, Responder}; +use bcrypt::{hash, verify, DEFAULT_COST}; +use diesel::prelude::*; +use log::{error, info}; +use serde::Deserialize; + +pub async fn new_product( + pool: web::Data, + item: web::Json, +) -> impl Responder { + let conn = pool.get().unwrap(); + diesel::insert_into(product) + .values(item.into_inner()) + .execute(&conn) + .expect("Coundn't connect to DB"); + HttpResponse::Ok().body("Inserted successfully!") +} + +pub async fn product_details( + pool: web::Data, + product_id: web::Path, +) -> impl Responder { + let conn = pool.get().unwrap(); + let product_id = product_id.into_inner(); + info!("Fetching product details for {}", product_id); + let selected_product = product + .filter(id.eq(&product_id)) + .limit(1) + .first::(&conn); + match selected_product { + Ok(m) => { + info!("Found product: {}", product_id); + HttpResponse::Ok().json(m) + } + Err(_) => { + error!("Product not found: {}", product_id); + HttpResponse::NotFound().finish() + } + } +} -- cgit v1.2.3