diff options
author | Akshay <[email protected]> | 2020-11-16 15:32:44 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2020-11-16 15:32:44 +0000 |
commit | 199f6bfd503901121b0cdf532a46de89b592ada0 (patch) | |
tree | 39f7f5ee38359fec26df641e40863153ba6aa346 /src/handlers | |
parent | af628595f3cc5c373502fc73acf579047aee5482 (diff) |
add product deets and update endpoints
Diffstat (limited to 'src/handlers')
-rw-r--r-- | src/handlers/product.rs | 39 |
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}; | |||
2 | use crate::schema::product::dsl::*; | 2 | use crate::schema::product::dsl::*; |
3 | use crate::TPool; | 3 | use crate::TPool; |
4 | 4 | ||
5 | use actix_identity::Identity; | ||
6 | use actix_web::{web, HttpResponse, Responder}; | 5 | use actix_web::{web, HttpResponse, Responder}; |
7 | use bcrypt::{hash, verify, DEFAULT_COST}; | ||
8 | use diesel::prelude::*; | 6 | use diesel::prelude::*; |
9 | use log::{error, info}; | 7 | use log::{error, info}; |
10 | use serde::Deserialize; | 8 | use 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)] | ||
47 | pub struct UpdateProduct { | ||
48 | name: String, | ||
49 | kind: Option<String>, | ||
50 | price: f32, | ||
51 | description: Option<String>, | ||
52 | } | ||
53 | |||
54 | pub 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 | } | ||