aboutsummaryrefslogtreecommitdiff
path: root/backend/src/handlers/rating.rs
blob: dfbeb3e212be243480a1f3460c3f600f0773b5a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use crate::models::{AddRating, Customer, Rating};
use crate::schema::rating::dsl as rating;
use crate::schema::{customer::dsl::*, product::dsl::*};
use crate::TPool;

use actix_identity::Identity;
use actix_web::{web, HttpResponse, Responder};
use diesel::prelude::*;
use log::{error, info};
use serde::Deserialize;

#[derive(Deserialize, Debug)]
pub struct AddRatingJson {
    pub comment_text: Option<String>,
    pub stars: Option<i32>,
    pub product_id: i32,
}

pub async fn add_rating(
    cookie: Identity,
    rating_details: web::Json<AddRatingJson>,
    pool: web::Data<TPool>,
) -> impl Responder {
    info!("Add rating hit: {:?}", rating_details.product_id);
    info!("{:?}", cookie.identity());
    let conn = pool.get().unwrap();
    if let Some(uname) = cookie.identity() {
        let selected_user = customer
            .filter(username.eq(&uname))
            .limit(1)
            .first::<Customer>(&conn)
            .expect("Couldn't connect to DB");
        let rating_details = rating_details.into_inner();
        let new_rating = AddRating {
            comment_text: rating_details.comment_text,
            stars: rating_details.stars,
            product_id: rating_details.product_id,
            customer_id: selected_user.id,
        };
        diesel::insert_into(rating::rating)
            .values(new_rating)
            .execute(&conn)
            .expect("Coundn't connect to DB");
        HttpResponse::Ok().body("Inserted rating successfully!")
    } else {
        error!("Unauthorized add rating action!");
        return HttpResponse::Unauthorized()
            .body("Need to be logged in to add rating!");
    }
}

#[derive(Deserialize, Debug)]
pub struct RemoveRating {
    rating_id: i32,
}

pub async fn remove_rating(
    cookie: Identity,
    rating_details: web::Json<RemoveRating>,
    pool: web::Data<TPool>,
) -> impl Responder {
    info!("Remove rating hit: {:?}", rating_details.rating_id);
    let conn = pool.get().unwrap();
    if let Some(uname) = cookie.identity() {
        let selected_user = customer
            .filter(username.eq(&uname))
            .limit(1)
            .first::<Customer>(&conn)
            .expect("Couldn't connect to DB");

        diesel::delete(
            rating::rating
                .filter(rating::customer_id.eq(selected_user.id))
                .filter(rating::id.eq(rating_details.rating_id)),
        )
        .execute(&conn)
        .expect("Coundn't connect to DB");
        HttpResponse::Ok().body("Removed successfully!")
    } else {
        error!("Unauthorized add to cart action!");
        return HttpResponse::Unauthorized()
            .body("Need to be logged in to add to cart!");
    }
}

// pub async fn get_product_reviews(
//     product: web::Json<GetProductReviews>,
//     pool: web::Data<TPool>,
// ) -> impl Responder {
//     unimplemented!()
// }