aboutsummaryrefslogtreecommitdiff
path: root/backend/src/handlers/rating.rs
diff options
context:
space:
mode:
Diffstat (limited to 'backend/src/handlers/rating.rs')
-rw-r--r--backend/src/handlers/rating.rs91
1 files changed, 91 insertions, 0 deletions
diff --git a/backend/src/handlers/rating.rs b/backend/src/handlers/rating.rs
new file mode 100644
index 0000000..dfbeb3e
--- /dev/null
+++ b/backend/src/handlers/rating.rs
@@ -0,0 +1,91 @@
1use crate::models::{AddRating, Customer, Rating};
2use crate::schema::rating::dsl as rating;
3use crate::schema::{customer::dsl::*, product::dsl::*};
4use crate::TPool;
5
6use actix_identity::Identity;
7use actix_web::{web, HttpResponse, Responder};
8use diesel::prelude::*;
9use log::{error, info};
10use serde::Deserialize;
11
12#[derive(Deserialize, Debug)]
13pub struct AddRatingJson {
14 pub comment_text: Option<String>,
15 pub stars: Option<i32>,
16 pub product_id: i32,
17}
18
19pub 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 info!("{:?}", cookie.identity());
26 let conn = pool.get().unwrap();
27 if let Some(uname) = cookie.identity() {
28 let selected_user = customer
29 .filter(username.eq(&uname))
30 .limit(1)
31 .first::<Customer>(&conn)
32 .expect("Couldn't connect to DB");
33 let rating_details = rating_details.into_inner();
34 let new_rating = AddRating {
35 comment_text: rating_details.comment_text,
36 stars: rating_details.stars,
37 product_id: rating_details.product_id,
38 customer_id: selected_user.id,
39 };
40 diesel::insert_into(rating::rating)
41 .values(new_rating)
42 .execute(&conn)
43 .expect("Coundn't connect to DB");
44 HttpResponse::Ok().body("Inserted rating successfully!")
45 } else {
46 error!("Unauthorized add rating action!");
47 return HttpResponse::Unauthorized()
48 .body("Need to be logged in to add rating!");
49 }
50}
51
52#[derive(Deserialize, Debug)]
53pub struct RemoveRating {
54 rating_id: i32,
55}
56
57pub async fn remove_rating(
58 cookie: Identity,
59 rating_details: web::Json<RemoveRating>,
60 pool: web::Data<TPool>,
61) -> impl Responder {
62 info!("Remove rating hit: {:?}", rating_details.rating_id);
63 let conn = pool.get().unwrap();
64 if let Some(uname) = cookie.identity() {
65 let selected_user = customer
66 .filter(username.eq(&uname))
67 .limit(1)
68 .first::<Customer>(&conn)
69 .expect("Couldn't connect to DB");
70
71 diesel::delete(
72 rating::rating
73 .filter(rating::customer_id.eq(selected_user.id))
74 .filter(rating::id.eq(rating_details.rating_id)),
75 )
76 .execute(&conn)
77 .expect("Coundn't connect to DB");
78 HttpResponse::Ok().body("Removed successfully!")
79 } else {
80 error!("Unauthorized add to cart action!");
81 return HttpResponse::Unauthorized()
82 .body("Need to be logged in to add to cart!");
83 }
84}
85
86// pub async fn get_product_reviews(
87// product: web::Json<GetProductReviews>,
88// pool: web::Data<TPool>,
89// ) -> impl Responder {
90// unimplemented!()
91// }