From 199f6bfd503901121b0cdf532a46de89b592ada0 Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 16 Nov 2020 21:02:44 +0530 Subject: add product deets and update endpoints --- src/handlers/product.rs | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) (limited to 'src/handlers') 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}; use crate::schema::product::dsl::*; use crate::TPool; -use actix_identity::Identity; use actix_web::{web, HttpResponse, Responder}; -use bcrypt::{hash, verify, DEFAULT_COST}; use diesel::prelude::*; use log::{error, info}; use serde::Deserialize; @@ -13,6 +11,7 @@ pub async fn new_product( pool: web::Data, item: web::Json, ) -> impl Responder { + info!("New product hit: {:?}", item.name); let conn = pool.get().unwrap(); diesel::insert_into(product) .values(item.into_inner()) @@ -43,3 +42,39 @@ pub async fn product_details( } } } + +#[derive(Deserialize)] +pub struct UpdateProduct { + name: String, + kind: Option, + price: f32, + description: Option, +} + +pub async fn update_product( + pool: web::Data, + product_id: web::Path, + product_details: web::Json, +) -> impl Responder { + let conn = pool.get().unwrap(); + let product_id = product_id.into_inner(); + let product_details = product_details.into_inner(); + info!("Updating product: {:?}", product_id); + match diesel::update(product.filter(id.eq(product_id))) + .set(( + name.eq(product_details.name), + kind.eq(product_details.kind), + price.eq(product_details.price), + description.eq(product_details.description), + )) + .execute(&conn) + { + Ok(_) => { + return HttpResponse::Ok().body("Changed product successfully") + } + _ => { + return HttpResponse::InternalServerError() + .body("Unable to update record") + } + } +} -- cgit v1.2.3