aboutsummaryrefslogtreecommitdiff
path: root/src/handlers
diff options
context:
space:
mode:
Diffstat (limited to 'src/handlers')
-rw-r--r--src/handlers/product.rs39
1 files changed, 37 insertions, 2 deletions
diff --git a/src/handlers/product.rs b/src/handlers/product.rs
index e655e9d..60a4684 100644
--- a/src/handlers/product.rs
+++ b/src/handlers/product.rs
@@ -2,9 +2,7 @@ use crate::models::{NewProduct, Product};
2use crate::schema::product::dsl::*; 2use crate::schema::product::dsl::*;
3use crate::TPool; 3use crate::TPool;
4 4
5use actix_identity::Identity;
6use actix_web::{web, HttpResponse, Responder}; 5use actix_web::{web, HttpResponse, Responder};
7use bcrypt::{hash, verify, DEFAULT_COST};
8use diesel::prelude::*; 6use diesel::prelude::*;
9use log::{error, info}; 7use log::{error, info};
10use serde::Deserialize; 8use serde::Deserialize;
@@ -13,6 +11,7 @@ pub async fn new_product(
13 pool: web::Data<TPool>, 11 pool: web::Data<TPool>,
14 item: web::Json<NewProduct>, 12 item: web::Json<NewProduct>,
15) -> impl Responder { 13) -> impl Responder {
14 info!("New product hit: {:?}", item.name);
16 let conn = pool.get().unwrap(); 15 let conn = pool.get().unwrap();
17 diesel::insert_into(product) 16 diesel::insert_into(product)
18 .values(item.into_inner()) 17 .values(item.into_inner())
@@ -43,3 +42,39 @@ pub async fn product_details(
43 } 42 }
44 } 43 }
45} 44}
45
46#[derive(Deserialize)]
47pub struct UpdateProduct {
48 name: String,
49 kind: Option<String>,
50 price: f32,
51 description: Option<String>,
52}
53
54pub async fn update_product(
55 pool: web::Data<TPool>,
56 product_id: web::Path<i32>,
57 product_details: web::Json<UpdateProduct>,
58) -> impl Responder {
59 let conn = pool.get().unwrap();
60 let product_id = product_id.into_inner();
61 let product_details = product_details.into_inner();
62 info!("Updating product: {:?}", product_id);
63 match diesel::update(product.filter(id.eq(product_id)))
64 .set((
65 name.eq(product_details.name),
66 kind.eq(product_details.kind),
67 price.eq(product_details.price),
68 description.eq(product_details.description),
69 ))
70 .execute(&conn)
71 {
72 Ok(_) => {
73 return HttpResponse::Ok().body("Changed product successfully")
74 }
75 _ => {
76 return HttpResponse::InternalServerError()
77 .body("Unable to update record")
78 }
79 }
80}