diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/server.rs | 7 | ||||
-rw-r--r-- | src/handlers/product.rs | 39 | ||||
-rw-r--r-- | src/models.rs | 5 |
3 files changed, 47 insertions, 4 deletions
diff --git a/src/bin/server.rs b/src/bin/server.rs index c28d041..180c5bc 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs | |||
@@ -42,7 +42,12 @@ async fn main() -> std::io::Result<()> { | |||
42 | ) | 42 | ) |
43 | .service( | 43 | .service( |
44 | web::scope("/product") | 44 | web::scope("/product") |
45 | .route("/new", web::post().to(product::new_product)), | 45 | .route("/{id}", web::get().to(product::product_details)) |
46 | .route("/new", web::post().to(product::new_product)) | ||
47 | .route( | ||
48 | "/update_product/{id}", | ||
49 | web::post().to(product::update_product), | ||
50 | ), | ||
46 | ) | 51 | ) |
47 | .route("/hey", web::get().to(manual_hello)) | 52 | .route("/hey", web::get().to(manual_hello)) |
48 | }) | 53 | }) |
diff --git a/src/handlers/product.rs b/src/handlers/product.rs index e655e9d..60a4684 100644 --- a/src/handlers/product.rs +++ b/src/handlers/product.rs | |||
@@ -2,9 +2,7 @@ use crate::models::{NewProduct, Product}; | |||
2 | use crate::schema::product::dsl::*; | 2 | use crate::schema::product::dsl::*; |
3 | use crate::TPool; | 3 | use crate::TPool; |
4 | 4 | ||
5 | use actix_identity::Identity; | ||
6 | use actix_web::{web, HttpResponse, Responder}; | 5 | use actix_web::{web, HttpResponse, Responder}; |
7 | use bcrypt::{hash, verify, DEFAULT_COST}; | ||
8 | use diesel::prelude::*; | 6 | use diesel::prelude::*; |
9 | use log::{error, info}; | 7 | use log::{error, info}; |
10 | use serde::Deserialize; | 8 | use serde::Deserialize; |
@@ -13,6 +11,7 @@ pub async fn new_product( | |||
13 | pool: web::Data<TPool>, | 11 | pool: web::Data<TPool>, |
14 | item: web::Json<NewProduct>, | 12 | item: web::Json<NewProduct>, |
15 | ) -> impl Responder { | 13 | ) -> impl Responder { |
14 | info!("New product hit: {:?}", item.name); | ||
16 | let conn = pool.get().unwrap(); | 15 | let conn = pool.get().unwrap(); |
17 | diesel::insert_into(product) | 16 | diesel::insert_into(product) |
18 | .values(item.into_inner()) | 17 | .values(item.into_inner()) |
@@ -43,3 +42,39 @@ pub async fn product_details( | |||
43 | } | 42 | } |
44 | } | 43 | } |
45 | } | 44 | } |
45 | |||
46 | #[derive(Deserialize)] | ||
47 | pub struct UpdateProduct { | ||
48 | name: String, | ||
49 | kind: Option<String>, | ||
50 | price: f32, | ||
51 | description: Option<String>, | ||
52 | } | ||
53 | |||
54 | pub async fn update_product( | ||
55 | pool: web::Data<TPool>, | ||
56 | product_id: web::Path<i32>, | ||
57 | product_details: web::Json<UpdateProduct>, | ||
58 | ) -> impl Responder { | ||
59 | let conn = pool.get().unwrap(); | ||
60 | let product_id = product_id.into_inner(); | ||
61 | let product_details = product_details.into_inner(); | ||
62 | info!("Updating product: {:?}", product_id); | ||
63 | match diesel::update(product.filter(id.eq(product_id))) | ||
64 | .set(( | ||
65 | name.eq(product_details.name), | ||
66 | kind.eq(product_details.kind), | ||
67 | price.eq(product_details.price), | ||
68 | description.eq(product_details.description), | ||
69 | )) | ||
70 | .execute(&conn) | ||
71 | { | ||
72 | Ok(_) => { | ||
73 | return HttpResponse::Ok().body("Changed product successfully") | ||
74 | } | ||
75 | _ => { | ||
76 | return HttpResponse::InternalServerError() | ||
77 | .body("Unable to update record") | ||
78 | } | ||
79 | } | ||
80 | } | ||
diff --git a/src/models.rs b/src/models.rs index fb6122c..6af1af6 100644 --- a/src/models.rs +++ b/src/models.rs | |||
@@ -1,6 +1,5 @@ | |||
1 | use super::schema::{members, product}; | 1 | use super::schema::{members, product}; |
2 | 2 | ||
3 | use bigdecimal::BigDecimal; | ||
4 | use diesel::{Insertable, Queryable}; | 3 | use diesel::{Insertable, Queryable}; |
5 | use serde::{Deserialize, Serialize}; | 4 | use serde::{Deserialize, Serialize}; |
6 | 5 | ||
@@ -35,7 +34,11 @@ pub struct Product { | |||
35 | #[table_name = "product"] | 34 | #[table_name = "product"] |
36 | pub struct NewProduct { | 35 | pub struct NewProduct { |
37 | pub name: String, | 36 | pub name: String, |
37 | |||
38 | #[serde(skip_serializing_if = "Option::is_none")] | ||
38 | pub kind: Option<String>, | 39 | pub kind: Option<String>, |
39 | pub price: f32, | 40 | pub price: f32, |
41 | |||
42 | #[serde(skip_serializing_if = "Option::is_none")] | ||
40 | pub description: Option<String>, | 43 | pub description: Option<String>, |
41 | } | 44 | } |