diff options
-rw-r--r-- | Cargo.lock | 57 | ||||
-rw-r--r-- | Cargo.toml | 5 | ||||
-rw-r--r-- | migrations/2020-11-16-133516_create_products_table/down.sql | 3 | ||||
-rw-r--r-- | migrations/2020-11-16-133516_create_products_table/up.sql | 8 | ||||
-rw-r--r-- | src/handlers/product.rs | 45 |
5 files changed, 116 insertions, 2 deletions
@@ -461,6 +461,28 @@ dependencies = [ | |||
461 | ] | 461 | ] |
462 | 462 | ||
463 | [[package]] | 463 | [[package]] |
464 | name = "bigdecimal" | ||
465 | version = "0.0.14" | ||
466 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
467 | checksum = "679e21a6734fdfc63378aea80c2bf31e6ac8ced21ed33e1ee37f8f7bf33c2056" | ||
468 | dependencies = [ | ||
469 | "num-bigint", | ||
470 | "num-integer", | ||
471 | "num-traits", | ||
472 | ] | ||
473 | |||
474 | [[package]] | ||
475 | name = "bigdecimal" | ||
476 | version = "0.1.2" | ||
477 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
478 | checksum = "1374191e2dd25f9ae02e3aa95041ed5d747fc77b3c102b49fe2dd9a8117a6244" | ||
479 | dependencies = [ | ||
480 | "num-bigint", | ||
481 | "num-integer", | ||
482 | "num-traits", | ||
483 | ] | ||
484 | |||
485 | [[package]] | ||
464 | name = "bitflags" | 486 | name = "bitflags" |
465 | version = "1.2.1" | 487 | version = "1.2.1" |
466 | source = "registry+https://github.com/rust-lang/crates.io-index" | 488 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -643,9 +665,13 @@ version = "1.4.5" | |||
643 | source = "registry+https://github.com/rust-lang/crates.io-index" | 665 | source = "registry+https://github.com/rust-lang/crates.io-index" |
644 | checksum = "3e2de9deab977a153492a1468d1b1c0662c1cf39e5ea87d0c060ecd59ef18d8c" | 666 | checksum = "3e2de9deab977a153492a1468d1b1c0662c1cf39e5ea87d0c060ecd59ef18d8c" |
645 | dependencies = [ | 667 | dependencies = [ |
668 | "bigdecimal 0.1.2", | ||
646 | "byteorder", | 669 | "byteorder", |
647 | "diesel_derives", | 670 | "diesel_derives", |
648 | "mysqlclient-sys", | 671 | "mysqlclient-sys", |
672 | "num-bigint", | ||
673 | "num-integer", | ||
674 | "num-traits", | ||
649 | "r2d2", | 675 | "r2d2", |
650 | "url 1.7.2", | 676 | "url 1.7.2", |
651 | ] | 677 | ] |
@@ -764,6 +790,7 @@ dependencies = [ | |||
764 | "actix-identity", | 790 | "actix-identity", |
765 | "actix-web", | 791 | "actix-web", |
766 | "bcrypt", | 792 | "bcrypt", |
793 | "bigdecimal 0.0.14", | ||
767 | "diesel", | 794 | "diesel", |
768 | "log", | 795 | "log", |
769 | "pretty_env_logger", | 796 | "pretty_env_logger", |
@@ -1228,6 +1255,36 @@ dependencies = [ | |||
1228 | ] | 1255 | ] |
1229 | 1256 | ||
1230 | [[package]] | 1257 | [[package]] |
1258 | name = "num-bigint" | ||
1259 | version = "0.2.6" | ||
1260 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1261 | checksum = "090c7f9998ee0ff65aa5b723e4009f7b217707f1fb5ea551329cc4d6231fb304" | ||
1262 | dependencies = [ | ||
1263 | "autocfg", | ||
1264 | "num-integer", | ||
1265 | "num-traits", | ||
1266 | ] | ||
1267 | |||
1268 | [[package]] | ||
1269 | name = "num-integer" | ||
1270 | version = "0.1.44" | ||
1271 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1272 | checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" | ||
1273 | dependencies = [ | ||
1274 | "autocfg", | ||
1275 | "num-traits", | ||
1276 | ] | ||
1277 | |||
1278 | [[package]] | ||
1279 | name = "num-traits" | ||
1280 | version = "0.2.14" | ||
1281 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1282 | checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" | ||
1283 | dependencies = [ | ||
1284 | "autocfg", | ||
1285 | ] | ||
1286 | |||
1287 | [[package]] | ||
1231 | name = "num_cpus" | 1288 | name = "num_cpus" |
1232 | version = "1.13.0" | 1289 | version = "1.13.0" |
1233 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1290 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -16,9 +16,10 @@ log = "0.4" | |||
16 | pretty_env_logger = "0.4" | 16 | pretty_env_logger = "0.4" |
17 | actix-identity = "0.3.1" | 17 | actix-identity = "0.3.1" |
18 | actix-cors = "0.4.0" | 18 | actix-cors = "0.4.0" |
19 | bigdecimal = "0.0.14" | ||
19 | rand = "0.7" | 20 | rand = "0.7" |
20 | 21 | ||
21 | [dependencies.diesel] | 22 | [dependencies.diesel] |
22 | version = "1.4" | 23 | version = "1.4.2" |
23 | features = ["mysql", "r2d2"] | 24 | features = ["mysql", "r2d2", "numeric"] |
24 | default-features = false | 25 | default-features = false |
diff --git a/migrations/2020-11-16-133516_create_products_table/down.sql b/migrations/2020-11-16-133516_create_products_table/down.sql new file mode 100644 index 0000000..7d99a98 --- /dev/null +++ b/migrations/2020-11-16-133516_create_products_table/down.sql | |||
@@ -0,0 +1,3 @@ | |||
1 | -- This file should undo anything in `up.sql` | ||
2 | |||
3 | DROP TABLE product; | ||
diff --git a/migrations/2020-11-16-133516_create_products_table/up.sql b/migrations/2020-11-16-133516_create_products_table/up.sql new file mode 100644 index 0000000..829c5be --- /dev/null +++ b/migrations/2020-11-16-133516_create_products_table/up.sql | |||
@@ -0,0 +1,8 @@ | |||
1 | -- Your SQL goes here | ||
2 | CREATE TABLE product ( | ||
3 | id INTEGER PRIMARY KEY AUTO_INCREMENT, | ||
4 | name VARCHAR(255) NOT NULL, | ||
5 | kind VARCHAR(255), | ||
6 | price FLOAT NOT NULL, | ||
7 | description VARCHAR(255) | ||
8 | ) | ||
diff --git a/src/handlers/product.rs b/src/handlers/product.rs new file mode 100644 index 0000000..e655e9d --- /dev/null +++ b/src/handlers/product.rs | |||
@@ -0,0 +1,45 @@ | |||
1 | use crate::models::{NewProduct, Product}; | ||
2 | use crate::schema::product::dsl::*; | ||
3 | use crate::TPool; | ||
4 | |||
5 | use actix_identity::Identity; | ||
6 | use actix_web::{web, HttpResponse, Responder}; | ||
7 | use bcrypt::{hash, verify, DEFAULT_COST}; | ||
8 | use diesel::prelude::*; | ||
9 | use log::{error, info}; | ||
10 | use serde::Deserialize; | ||
11 | |||
12 | pub async fn new_product( | ||
13 | pool: web::Data<TPool>, | ||
14 | item: web::Json<NewProduct>, | ||
15 | ) -> impl Responder { | ||
16 | let conn = pool.get().unwrap(); | ||
17 | diesel::insert_into(product) | ||
18 | .values(item.into_inner()) | ||
19 | .execute(&conn) | ||
20 | .expect("Coundn't connect to DB"); | ||
21 | HttpResponse::Ok().body("Inserted successfully!") | ||
22 | } | ||
23 | |||
24 | pub async fn product_details( | ||
25 | pool: web::Data<TPool>, | ||
26 | product_id: web::Path<i32>, | ||
27 | ) -> impl Responder { | ||
28 | let conn = pool.get().unwrap(); | ||
29 | let product_id = product_id.into_inner(); | ||
30 | info!("Fetching product details for {}", product_id); | ||
31 | let selected_product = product | ||
32 | .filter(id.eq(&product_id)) | ||
33 | .limit(1) | ||
34 | .first::<Product>(&conn); | ||
35 | match selected_product { | ||
36 | Ok(m) => { | ||
37 | info!("Found product: {}", product_id); | ||
38 | HttpResponse::Ok().json(m) | ||
39 | } | ||
40 | Err(_) => { | ||
41 | error!("Product not found: {}", product_id); | ||
42 | HttpResponse::NotFound().finish() | ||
43 | } | ||
44 | } | ||
45 | } | ||