aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-11-16 15:32:44 +0000
committerAkshay <[email protected]>2020-11-16 15:32:44 +0000
commit199f6bfd503901121b0cdf532a46de89b592ada0 (patch)
tree39f7f5ee38359fec26df641e40863153ba6aa346
parentaf628595f3cc5c373502fc73acf579047aee5482 (diff)
add product deets and update endpoints
-rw-r--r--src/bin/server.rs7
-rw-r--r--src/handlers/product.rs39
-rw-r--r--src/models.rs5
3 files changed, 47 insertions, 4 deletions
diff --git a/src/bin/server.rs b/src/bin/server.rs
index c28d041..180c5bc 100644
--- a/src/bin/server.rs
+++ b/src/bin/server.rs
@@ -42,7 +42,12 @@ async fn main() -> std::io::Result<()> {
42 ) 42 )
43 .service( 43 .service(
44 web::scope("/product") 44 web::scope("/product")
45 .route("/new", web::post().to(product::new_product)), 45 .route("/{id}", web::get().to(product::product_details))
46 .route("/new", web::post().to(product::new_product))
47 .route(
48 "/update_product/{id}",
49 web::post().to(product::update_product),
50 ),
46 ) 51 )
47 .route("/hey", web::get().to(manual_hello)) 52 .route("/hey", web::get().to(manual_hello))
48 }) 53 })
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}
diff --git a/src/models.rs b/src/models.rs
index fb6122c..6af1af6 100644
--- a/src/models.rs
+++ b/src/models.rs
@@ -1,6 +1,5 @@
1use super::schema::{members, product}; 1use super::schema::{members, product};
2 2
3use bigdecimal::BigDecimal;
4use diesel::{Insertable, Queryable}; 3use diesel::{Insertable, Queryable};
5use serde::{Deserialize, Serialize}; 4use serde::{Deserialize, Serialize};
6 5
@@ -35,7 +34,11 @@ pub struct Product {
35#[table_name = "product"] 34#[table_name = "product"]
36pub struct NewProduct { 35pub struct NewProduct {
37 pub name: String, 36 pub name: String,
37
38 #[serde(skip_serializing_if = "Option::is_none")]
38 pub kind: Option<String>, 39 pub kind: Option<String>,
39 pub price: f32, 40 pub price: f32,
41
42 #[serde(skip_serializing_if = "Option::is_none")]
40 pub description: Option<String>, 43 pub description: Option<String>,
41} 44}