diff options
author | Akshay <[email protected]> | 2020-12-16 16:42:50 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2020-12-16 16:42:50 +0000 |
commit | 2a112855e54ea3a365fa032e8726196a7eec9a06 (patch) | |
tree | a4448517fef17c1cfcb845a6b616e8640070d9c7 /src/handlers | |
parent | aa5a6d212f6237ab696cb0f5b00d3d5e762d5fec (diff) |
begin working on rating endpoints
Diffstat (limited to 'src/handlers')
-rw-r--r-- | src/handlers/product.rs | 23 | ||||
-rw-r--r-- | src/handlers/rating.rs | 90 |
2 files changed, 107 insertions, 6 deletions
diff --git a/src/handlers/product.rs b/src/handlers/product.rs index 788efb3..21bdbc9 100644 --- a/src/handlers/product.rs +++ b/src/handlers/product.rs | |||
@@ -1,11 +1,11 @@ | |||
1 | use crate::models::{NewProduct, Product, UpdateProduct}; | 1 | use crate::models::{NewProduct, Product, Rating, UpdateProduct}; |
2 | use crate::schema::product::dsl::*; | 2 | use crate::schema::product::dsl::*; |
3 | use crate::schema::rating::dsl as rating; | ||
3 | use crate::TPool; | 4 | use crate::TPool; |
4 | 5 | ||
5 | use actix_web::{web, HttpResponse, Responder}; | 6 | use actix_web::{web, HttpResponse, Responder}; |
6 | use diesel::prelude::*; | 7 | use diesel::prelude::*; |
7 | use log::{error, info}; | 8 | use log::{error, info}; |
8 | use serde::Deserialize; | ||
9 | 9 | ||
10 | pub async fn new_product( | 10 | pub async fn new_product( |
11 | pool: web::Data<TPool>, | 11 | pool: web::Data<TPool>, |
@@ -75,13 +75,24 @@ pub async fn get_all_products(pool: web::Data<TPool>) -> impl Responder { | |||
75 | let conn = pool.get().unwrap(); | 75 | let conn = pool.get().unwrap(); |
76 | info!("Generating and returning catalog ..."); | 76 | info!("Generating and returning catalog ..."); |
77 | match product.load::<Product>(&conn) { | 77 | match product.load::<Product>(&conn) { |
78 | Ok(products) => { | 78 | Ok(products) => return HttpResponse::Ok().json(&products), |
79 | return HttpResponse::Ok() | ||
80 | .body(serde_json::to_string(&products).unwrap()) | ||
81 | } | ||
82 | Err(_) => { | 79 | Err(_) => { |
83 | return HttpResponse::InternalServerError() | 80 | return HttpResponse::InternalServerError() |
84 | .body("Unable to fetch product catalog") | 81 | .body("Unable to fetch product catalog") |
85 | } | 82 | } |
86 | } | 83 | } |
87 | } | 84 | } |
85 | |||
86 | pub async fn get_product_reviews( | ||
87 | pool: web::Data<TPool>, | ||
88 | product_id: web::Path<i32>, | ||
89 | ) -> impl Responder { | ||
90 | let conn = pool.get().unwrap(); | ||
91 | info!("Fetching product reviews for {}", product_id); | ||
92 | let pid = product_id.into_inner(); | ||
93 | let rating_entries = rating::rating | ||
94 | .filter(rating::product_id.eq(pid)) | ||
95 | .load::<Rating>(&conn) | ||
96 | .expect("Couldn't connect to DB"); | ||
97 | return HttpResponse::Ok().json(&rating_entries); | ||
98 | } | ||
diff --git a/src/handlers/rating.rs b/src/handlers/rating.rs new file mode 100644 index 0000000..309c2c6 --- /dev/null +++ b/src/handlers/rating.rs | |||
@@ -0,0 +1,90 @@ | |||
1 | use crate::models::{AddRating, Customer, Rating}; | ||
2 | use crate::schema::rating::dsl as rating; | ||
3 | use crate::schema::{customer::dsl::*, product::dsl::*}; | ||
4 | use crate::TPool; | ||
5 | |||
6 | use actix_identity::Identity; | ||
7 | use actix_web::{web, HttpResponse, Responder}; | ||
8 | use diesel::prelude::*; | ||
9 | use log::{error, info}; | ||
10 | use serde::Deserialize; | ||
11 | |||
12 | #[derive(Deserialize, Debug)] | ||
13 | pub struct AddRatingJson { | ||
14 | pub comment_text: Option<String>, | ||
15 | pub stars: Option<i32>, | ||
16 | pub product_id: i32, | ||
17 | } | ||
18 | |||
19 | pub async fn add_rating( | ||
20 | cookie: Identity, | ||
21 | rating_details: web::Json<AddRatingJson>, | ||
22 | pool: web::Data<TPool>, | ||
23 | ) -> impl Responder { | ||
24 | info!("Add rating hit: {:?}", rating_details.product_id); | ||
25 | let conn = pool.get().unwrap(); | ||
26 | if let Some(uname) = cookie.identity() { | ||
27 | let selected_user = customer | ||
28 | .filter(username.eq(&uname)) | ||
29 | .limit(1) | ||
30 | .first::<Customer>(&conn) | ||
31 | .expect("Couldn't connect to DB"); | ||
32 | let rating_details = rating_details.into_inner(); | ||
33 | let new_rating = AddRating { | ||
34 | comment_text: rating_details.comment_text, | ||
35 | stars: rating_details.stars, | ||
36 | product_id: rating_details.product_id, | ||
37 | customer_id: selected_user.id, | ||
38 | }; | ||
39 | diesel::insert_into(rating::rating) | ||
40 | .values(new_rating) | ||
41 | .execute(&conn) | ||
42 | .expect("Coundn't connect to DB"); | ||
43 | HttpResponse::Ok().body("Inserted rating successfully!") | ||
44 | } else { | ||
45 | error!("Unauthorized add rating action!"); | ||
46 | return HttpResponse::Unauthorized() | ||
47 | .body("Need to be logged in to add rating!"); | ||
48 | } | ||
49 | } | ||
50 | |||
51 | #[derive(Deserialize, Debug)] | ||
52 | pub struct RemoveRating { | ||
53 | rating_id: i32, | ||
54 | } | ||
55 | |||
56 | pub async fn remove_rating( | ||
57 | cookie: Identity, | ||
58 | rating_details: web::Json<RemoveRating>, | ||
59 | pool: web::Data<TPool>, | ||
60 | ) -> impl Responder { | ||
61 | info!("Remove rating hit: {:?}", rating_details.rating_id); | ||
62 | let conn = pool.get().unwrap(); | ||
63 | if let Some(uname) = cookie.identity() { | ||
64 | let selected_user = customer | ||
65 | .filter(username.eq(&uname)) | ||
66 | .limit(1) | ||
67 | .first::<Customer>(&conn) | ||
68 | .expect("Couldn't connect to DB"); | ||
69 | |||
70 | diesel::delete( | ||
71 | rating::rating | ||
72 | .filter(rating::customer_id.eq(selected_user.id)) | ||
73 | .filter(rating::id.eq(rating_details.rating_id)), | ||
74 | ) | ||
75 | .execute(&conn) | ||
76 | .expect("Coundn't connect to DB"); | ||
77 | HttpResponse::Ok().body("Removed successfully!") | ||
78 | } else { | ||
79 | error!("Unauthorized add to cart action!"); | ||
80 | return HttpResponse::Unauthorized() | ||
81 | .body("Need to be logged in to add to cart!"); | ||
82 | } | ||
83 | } | ||
84 | |||
85 | // pub async fn get_product_reviews( | ||
86 | // product: web::Json<GetProductReviews>, | ||
87 | // pool: web::Data<TPool>, | ||
88 | // ) -> impl Responder { | ||
89 | // unimplemented!() | ||
90 | // } | ||