From f25d37318e7873bf4f5ca58bcb850bed70ae77a6 Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 16 Dec 2020 20:00:42 +0530 Subject: add product/ endpoints --- src/handlers/mod.rs | 1 + src/handlers/product.rs | 25 ++++++++++++++++--------- src/handlers/users.rs | 28 ++++++++++++++-------------- 3 files changed, 31 insertions(+), 23 deletions(-) (limited to 'src/handlers') diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 41bba8d..28591bc 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -1,3 +1,4 @@ +pub mod cart_items; pub mod product; pub mod smoke; pub mod users; diff --git a/src/handlers/product.rs b/src/handlers/product.rs index 60a4684..788efb3 100644 --- a/src/handlers/product.rs +++ b/src/handlers/product.rs @@ -1,4 +1,4 @@ -use crate::models::{NewProduct, Product}; +use crate::models::{NewProduct, Product, UpdateProduct}; use crate::schema::product::dsl::*; use crate::TPool; @@ -43,14 +43,6 @@ 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, @@ -78,3 +70,18 @@ pub async fn update_product( } } } + +pub async fn get_all_products(pool: web::Data) -> impl Responder { + let conn = pool.get().unwrap(); + info!("Generating and returning catalog ..."); + match product.load::(&conn) { + Ok(products) => { + return HttpResponse::Ok() + .body(serde_json::to_string(&products).unwrap()) + } + Err(_) => { + return HttpResponse::InternalServerError() + .body("Unable to fetch product catalog") + } + } +} diff --git a/src/handlers/users.rs b/src/handlers/users.rs index c7bc870..bff532c 100644 --- a/src/handlers/users.rs +++ b/src/handlers/users.rs @@ -1,5 +1,5 @@ -use crate::models::{Member, NewMember}; -use crate::schema::members::dsl::*; +use crate::models::{Customer, NewCustomer}; +use crate::schema::customer::dsl::*; use crate::TPool; use actix_identity::Identity; @@ -11,14 +11,14 @@ use serde::Deserialize; pub async fn new_user( pool: web::Data, - item: web::Json, + item: web::Json, ) -> impl Responder { let conn = pool.get().unwrap(); - let hashed_item = NewMember { + let hashed_item = NewCustomer { password: hash(&item.password, DEFAULT_COST).unwrap(), ..(item.into_inner()) }; - diesel::insert_into(members) + diesel::insert_into(customer) .values(hashed_item) .execute(&conn) .expect("Coundn't connect to DB"); @@ -31,10 +31,10 @@ pub async fn name_exists( ) -> impl Responder { let conn = pool.get().unwrap(); info!("target: {:?}", item); - if (members + if (customer .filter(username.eq(&item)) .limit(1) - .load::(&conn) + .load::(&conn) .expect("Coundn't connect to DB")) .len() > 0 @@ -59,10 +59,10 @@ pub async fn login( info!("Login hit"); let conn = pool.get().unwrap(); let entered_pass = &login_details.password; - let selected_user = members + let selected_user = customer .filter(username.eq(&login_details.username)) .limit(1) - .first::(&conn) + .first::(&conn) .expect("Couldn't connect to DB"); let hashed_pass = selected_user.password; if verify(entered_pass, &hashed_pass).unwrap() { @@ -89,10 +89,10 @@ pub async fn user_details( let conn = pool.get().unwrap(); let uname = uname.into_inner(); info!("Fetching info for: \"{}\"", uname); - let selected_user = members + let selected_user = customer .filter(username.eq(&uname)) .limit(1) - .first::(&conn); + .first::(&conn); match selected_user { Ok(m) => { info!("Found user: {}", uname); @@ -121,16 +121,16 @@ pub async fn change_password( if let Some(uname) = cookie.identity() { let entered_pass = &password_details.old_password; let new_password = &password_details.new_password; - let selected_user = members + let selected_user = customer .filter(username.eq(&uname)) .limit(1) - .first::(&conn) + .first::(&conn) .expect("Couldn't connect to DB"); let hashed_pass = selected_user.password; if verify(entered_pass, &hashed_pass).unwrap() { let hashed_new_password = hash(&new_password, DEFAULT_COST).unwrap(); - diesel::update(members.filter(id.eq(selected_user.id))) + diesel::update(customer.filter(id.eq(selected_user.id))) .set(password.eq(hashed_new_password)) .execute(&conn) .unwrap(); -- cgit v1.2.3