aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock57
-rw-r--r--Cargo.toml5
-rw-r--r--migrations/2020-11-16-133516_create_products_table/down.sql3
-rw-r--r--migrations/2020-11-16-133516_create_products_table/up.sql8
-rw-r--r--src/handlers/product.rs45
5 files changed, 116 insertions, 2 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 3dcd7dd..2c30823 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -461,6 +461,28 @@ dependencies = [
461] 461]
462 462
463[[package]] 463[[package]]
464name = "bigdecimal"
465version = "0.0.14"
466source = "registry+https://github.com/rust-lang/crates.io-index"
467checksum = "679e21a6734fdfc63378aea80c2bf31e6ac8ced21ed33e1ee37f8f7bf33c2056"
468dependencies = [
469 "num-bigint",
470 "num-integer",
471 "num-traits",
472]
473
474[[package]]
475name = "bigdecimal"
476version = "0.1.2"
477source = "registry+https://github.com/rust-lang/crates.io-index"
478checksum = "1374191e2dd25f9ae02e3aa95041ed5d747fc77b3c102b49fe2dd9a8117a6244"
479dependencies = [
480 "num-bigint",
481 "num-integer",
482 "num-traits",
483]
484
485[[package]]
464name = "bitflags" 486name = "bitflags"
465version = "1.2.1" 487version = "1.2.1"
466source = "registry+https://github.com/rust-lang/crates.io-index" 488source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -643,9 +665,13 @@ version = "1.4.5"
643source = "registry+https://github.com/rust-lang/crates.io-index" 665source = "registry+https://github.com/rust-lang/crates.io-index"
644checksum = "3e2de9deab977a153492a1468d1b1c0662c1cf39e5ea87d0c060ecd59ef18d8c" 666checksum = "3e2de9deab977a153492a1468d1b1c0662c1cf39e5ea87d0c060ecd59ef18d8c"
645dependencies = [ 667dependencies = [
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]]
1258name = "num-bigint"
1259version = "0.2.6"
1260source = "registry+https://github.com/rust-lang/crates.io-index"
1261checksum = "090c7f9998ee0ff65aa5b723e4009f7b217707f1fb5ea551329cc4d6231fb304"
1262dependencies = [
1263 "autocfg",
1264 "num-integer",
1265 "num-traits",
1266]
1267
1268[[package]]
1269name = "num-integer"
1270version = "0.1.44"
1271source = "registry+https://github.com/rust-lang/crates.io-index"
1272checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
1273dependencies = [
1274 "autocfg",
1275 "num-traits",
1276]
1277
1278[[package]]
1279name = "num-traits"
1280version = "0.2.14"
1281source = "registry+https://github.com/rust-lang/crates.io-index"
1282checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
1283dependencies = [
1284 "autocfg",
1285]
1286
1287[[package]]
1231name = "num_cpus" 1288name = "num_cpus"
1232version = "1.13.0" 1289version = "1.13.0"
1233source = "registry+https://github.com/rust-lang/crates.io-index" 1290source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index b7f1012..81cb4de 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -16,9 +16,10 @@ log = "0.4"
16pretty_env_logger = "0.4" 16pretty_env_logger = "0.4"
17actix-identity = "0.3.1" 17actix-identity = "0.3.1"
18actix-cors = "0.4.0" 18actix-cors = "0.4.0"
19bigdecimal = "0.0.14"
19rand = "0.7" 20rand = "0.7"
20 21
21[dependencies.diesel] 22[dependencies.diesel]
22version = "1.4" 23version = "1.4.2"
23features = ["mysql", "r2d2"] 24features = ["mysql", "r2d2", "numeric"]
24default-features = false 25default-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
3DROP 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
2CREATE 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 @@
1use crate::models::{NewProduct, Product};
2use crate::schema::product::dsl::*;
3use crate::TPool;
4
5use actix_identity::Identity;
6use actix_web::{web, HttpResponse, Responder};
7use bcrypt::{hash, verify, DEFAULT_COST};
8use diesel::prelude::*;
9use log::{error, info};
10use serde::Deserialize;
11
12pub 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
24pub 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}