aboutsummaryrefslogtreecommitdiff
path: root/src/handlers
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-12-16 14:30:42 +0000
committerAkshay <[email protected]>2020-12-16 14:30:42 +0000
commitf25d37318e7873bf4f5ca58bcb850bed70ae77a6 (patch)
tree3a470f463d274f31228c71eb793a912f06b09046 /src/handlers
parent199f6bfd503901121b0cdf532a46de89b592ada0 (diff)
add product/ endpoints
Diffstat (limited to 'src/handlers')
-rw-r--r--src/handlers/mod.rs1
-rw-r--r--src/handlers/product.rs25
-rw-r--r--src/handlers/users.rs28
3 files changed, 31 insertions, 23 deletions
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 @@
1pub mod cart_items;
1pub mod product; 2pub mod product;
2pub mod smoke; 3pub mod smoke;
3pub mod users; 4pub 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 @@
1use crate::models::{NewProduct, Product}; 1use crate::models::{NewProduct, Product, UpdateProduct};
2use crate::schema::product::dsl::*; 2use crate::schema::product::dsl::*;
3use crate::TPool; 3use crate::TPool;
4 4
@@ -43,14 +43,6 @@ pub async fn product_details(
43 } 43 }
44} 44}
45 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( 46pub async fn update_product(
55 pool: web::Data<TPool>, 47 pool: web::Data<TPool>,
56 product_id: web::Path<i32>, 48 product_id: web::Path<i32>,
@@ -78,3 +70,18 @@ pub async fn update_product(
78 } 70 }
79 } 71 }
80} 72}
73
74pub async fn get_all_products(pool: web::Data<TPool>) -> impl Responder {
75 let conn = pool.get().unwrap();
76 info!("Generating and returning catalog ...");
77 match product.load::<Product>(&conn) {
78 Ok(products) => {
79 return HttpResponse::Ok()
80 .body(serde_json::to_string(&products).unwrap())
81 }
82 Err(_) => {
83 return HttpResponse::InternalServerError()
84 .body("Unable to fetch product catalog")
85 }
86 }
87}
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 @@
1use crate::models::{Member, NewMember}; 1use crate::models::{Customer, NewCustomer};
2use crate::schema::members::dsl::*; 2use crate::schema::customer::dsl::*;
3use crate::TPool; 3use crate::TPool;
4 4
5use actix_identity::Identity; 5use actix_identity::Identity;
@@ -11,14 +11,14 @@ use serde::Deserialize;
11 11
12pub async fn new_user( 12pub async fn new_user(
13 pool: web::Data<TPool>, 13 pool: web::Data<TPool>,
14 item: web::Json<NewMember>, 14 item: web::Json<NewCustomer>,
15) -> impl Responder { 15) -> impl Responder {
16 let conn = pool.get().unwrap(); 16 let conn = pool.get().unwrap();
17 let hashed_item = NewMember { 17 let hashed_item = NewCustomer {
18 password: hash(&item.password, DEFAULT_COST).unwrap(), 18 password: hash(&item.password, DEFAULT_COST).unwrap(),
19 ..(item.into_inner()) 19 ..(item.into_inner())
20 }; 20 };
21 diesel::insert_into(members) 21 diesel::insert_into(customer)
22 .values(hashed_item) 22 .values(hashed_item)
23 .execute(&conn) 23 .execute(&conn)
24 .expect("Coundn't connect to DB"); 24 .expect("Coundn't connect to DB");
@@ -31,10 +31,10 @@ pub async fn name_exists(
31) -> impl Responder { 31) -> impl Responder {
32 let conn = pool.get().unwrap(); 32 let conn = pool.get().unwrap();
33 info!("target: {:?}", item); 33 info!("target: {:?}", item);
34 if (members 34 if (customer
35 .filter(username.eq(&item)) 35 .filter(username.eq(&item))
36 .limit(1) 36 .limit(1)
37 .load::<Member>(&conn) 37 .load::<Customer>(&conn)
38 .expect("Coundn't connect to DB")) 38 .expect("Coundn't connect to DB"))
39 .len() 39 .len()
40 > 0 40 > 0
@@ -59,10 +59,10 @@ pub async fn login(
59 info!("Login hit"); 59 info!("Login hit");
60 let conn = pool.get().unwrap(); 60 let conn = pool.get().unwrap();
61 let entered_pass = &login_details.password; 61 let entered_pass = &login_details.password;
62 let selected_user = members 62 let selected_user = customer
63 .filter(username.eq(&login_details.username)) 63 .filter(username.eq(&login_details.username))
64 .limit(1) 64 .limit(1)
65 .first::<Member>(&conn) 65 .first::<Customer>(&conn)
66 .expect("Couldn't connect to DB"); 66 .expect("Couldn't connect to DB");
67 let hashed_pass = selected_user.password; 67 let hashed_pass = selected_user.password;
68 if verify(entered_pass, &hashed_pass).unwrap() { 68 if verify(entered_pass, &hashed_pass).unwrap() {
@@ -89,10 +89,10 @@ pub async fn user_details(
89 let conn = pool.get().unwrap(); 89 let conn = pool.get().unwrap();
90 let uname = uname.into_inner(); 90 let uname = uname.into_inner();
91 info!("Fetching info for: \"{}\"", uname); 91 info!("Fetching info for: \"{}\"", uname);
92 let selected_user = members 92 let selected_user = customer
93 .filter(username.eq(&uname)) 93 .filter(username.eq(&uname))
94 .limit(1) 94 .limit(1)
95 .first::<Member>(&conn); 95 .first::<Customer>(&conn);
96 match selected_user { 96 match selected_user {
97 Ok(m) => { 97 Ok(m) => {
98 info!("Found user: {}", uname); 98 info!("Found user: {}", uname);
@@ -121,16 +121,16 @@ pub async fn change_password(
121 if let Some(uname) = cookie.identity() { 121 if let Some(uname) = cookie.identity() {
122 let entered_pass = &password_details.old_password; 122 let entered_pass = &password_details.old_password;
123 let new_password = &password_details.new_password; 123 let new_password = &password_details.new_password;
124 let selected_user = members 124 let selected_user = customer
125 .filter(username.eq(&uname)) 125 .filter(username.eq(&uname))
126 .limit(1) 126 .limit(1)
127 .first::<Member>(&conn) 127 .first::<Customer>(&conn)
128 .expect("Couldn't connect to DB"); 128 .expect("Couldn't connect to DB");
129 let hashed_pass = selected_user.password; 129 let hashed_pass = selected_user.password;
130 if verify(entered_pass, &hashed_pass).unwrap() { 130 if verify(entered_pass, &hashed_pass).unwrap() {
131 let hashed_new_password = 131 let hashed_new_password =
132 hash(&new_password, DEFAULT_COST).unwrap(); 132 hash(&new_password, DEFAULT_COST).unwrap();
133 diesel::update(members.filter(id.eq(selected_user.id))) 133 diesel::update(customer.filter(id.eq(selected_user.id)))
134 .set(password.eq(hashed_new_password)) 134 .set(password.eq(hashed_new_password))
135 .execute(&conn) 135 .execute(&conn)
136 .unwrap(); 136 .unwrap();