diff options
Diffstat (limited to 'src/handlers/product.rs')
-rw-r--r-- | src/handlers/product.rs | 23 |
1 files changed, 17 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 | } | ||