aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-11-16 14:15:42 +0000
committerAkshay <[email protected]>2020-11-16 14:15:42 +0000
commit67e5016829787c2398dcb1ceea6b6dca192ac27c (patch)
tree3bf82a2002c22e66439fda23bd66844d0a953571 /src
parent4589d5a278c8d1c51e4a465c04945f8a84b6e131 (diff)
add product table
Diffstat (limited to 'src')
-rw-r--r--src/handlers/product.rs45
1 files changed, 45 insertions, 0 deletions
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 @@
1use crate::models::{NewProduct, Product};
2use crate::schema::product::dsl::*;
3use crate::TPool;
4
5use actix_identity::Identity;
6use actix_web::{web, HttpResponse, Responder};
7use bcrypt::{hash, verify, DEFAULT_COST};
8use diesel::prelude::*;
9use log::{error, info};
10use serde::Deserialize;
11
12pub async fn new_product(
13 pool: web::Data<TPool>,
14 item: web::Json<NewProduct>,
15) -> impl Responder {
16 let conn = pool.get().unwrap();
17 diesel::insert_into(product)
18 .values(item.into_inner())
19 .execute(&conn)
20 .expect("Coundn't connect to DB");
21 HttpResponse::Ok().body("Inserted successfully!")
22}
23
24pub async fn product_details(
25 pool: web::Data<TPool>,
26 product_id: web::Path<i32>,
27) -> impl Responder {
28 let conn = pool.get().unwrap();
29 let product_id = product_id.into_inner();
30 info!("Fetching product details for {}", product_id);
31 let selected_product = product
32 .filter(id.eq(&product_id))
33 .limit(1)
34 .first::<Product>(&conn);
35 match selected_product {
36 Ok(m) => {
37 info!("Found product: {}", product_id);
38 HttpResponse::Ok().json(m)
39 }
40 Err(_) => {
41 error!("Product not found: {}", product_id);
42 HttpResponse::NotFound().finish()
43 }
44 }
45}