aboutsummaryrefslogtreecommitdiff
path: root/src/handlers/product.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/handlers/product.rs')
-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}