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/bin/server.rs | 7 ++++++- src/handlers/product.rs | 39 +++++++++++++++++++++++++++++++++++++-- src/models.rs | 5 ++++- 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<()> { ) .service( web::scope("/product") - .route("/new", web::post().to(product::new_product)), + .route("/{id}", web::get().to(product::product_details)) + .route("/new", web::post().to(product::new_product)) + .route( + "/update_product/{id}", + web::post().to(product::update_product), + ), ) .route("/hey", web::get().to(manual_hello)) }) 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") + } + } +} 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 @@ use super::schema::{members, product}; -use bigdecimal::BigDecimal; use diesel::{Insertable, Queryable}; use serde::{Deserialize, Serialize}; @@ -35,7 +34,11 @@ pub struct Product { #[table_name = "product"] pub struct NewProduct { pub name: String, + + #[serde(skip_serializing_if = "Option::is_none")] pub kind: Option, pub price: f32, + + #[serde(skip_serializing_if = "Option::is_none")] pub description: Option, } -- cgit v1.2.3