From bf33dcf52dcb9aa7d4b00ff209fc6061361bc4d0 Mon Sep 17 00:00:00 2001 From: Antonio Spinelli Date: Tue, 7 Jun 2016 22:37:05 -0300 Subject: [PATCH 1/3] application bootstrap --- .gitignore | 2 ++ cli/create_tables.php | 8 ++++++++ data/.gitkeep | 0 lib/dbconn.php | 6 ++++++ 4 files changed, 16 insertions(+) create mode 100644 .gitignore create mode 100644 cli/create_tables.php create mode 100644 data/.gitkeep create mode 100644 lib/dbconn.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..720afbd --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/data/* +!/data/.gitkeep diff --git a/cli/create_tables.php b/cli/create_tables.php new file mode 100644 index 0000000..5518486 --- /dev/null +++ b/cli/create_tables.php @@ -0,0 +1,8 @@ +query("CREATE TABLE IF NOT EXISTS wishlists (id INTEGER PRIMARY KEY AUTOINCREMENT, email varchar(255), product_id int, status VARCHAR(1) DEFAULT 'P');"); +$db->query('CREATE TABLE IF NOT EXISTS products (id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar(255), unit_price DECIMAL(10,2), stock int);'); +$stm = $db->query("INSERT INTO products VALUES (null, 'Camiseta', 59.9, 10);"); +$stm = $db->query("INSERT INTO products VALUES (null, 'Bermuda', 69.9, 10);"); \ No newline at end of file diff --git a/data/.gitkeep b/data/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/lib/dbconn.php b/lib/dbconn.php new file mode 100644 index 0000000..7dfdde7 --- /dev/null +++ b/lib/dbconn.php @@ -0,0 +1,6 @@ + Date: Thu, 9 Jun 2016 18:42:00 -0300 Subject: [PATCH 2/3] add product feature it shows a quick view of products and remove one --- README.md | 20 ++++++++++++++++ lib/functions.php | 43 ++++++++++++++++++++++++++++++++++ public/product.php | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 README.md create mode 100644 lib/functions.php create mode 100644 public/product.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..6e25bc4 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +Developing for Business +======================= + +#### User Story + +I as a guest user, I want to add a product sold out in my wish list, so that I receive a notification when it becomes available. + +Eu como um usuário convidado, quero adicionar um produto esgotado em minha lista de desejos para que eu receba uma notificação quando ele estiver disponível. + + +#### Requirements + +* PHP 5.6+ +* PDO + SQLite Driver + +#### Run Application + +```shell +$ php -S localhost:8000 -t public +``` \ No newline at end of file diff --git a/lib/functions.php b/lib/functions.php new file mode 100644 index 0000000..ae80e96 --- /dev/null +++ b/lib/functions.php @@ -0,0 +1,43 @@ + (isset($data['id']) ? $data['id'] : null), + 'name' => $data['name'], + 'price' => $data['price'], + 'stock' => $data['stock'] + ); +} + +/** + * Creates a url to remove product from wish list + * @param int $id + * @return string + */ +function removeUrl($uri, $id, array $extraQuery = array()) +{ + $query = http_build_query(array_merge(['remove' => $id], $extraQuery)); + return sprintf('remove', $uri, $query); +} diff --git a/public/product.php b/public/product.php new file mode 100644 index 0000000..879b5a8 --- /dev/null +++ b/public/product.php @@ -0,0 +1,58 @@ +beginTransaction(); + try { + $stm = $db->prepare('INSERT INTO products (name, unit_price, stock) VALUES (?, ?, ?)'); + $stm->execute([$product['name'], $product['unit_price'], $product['stock']]); + $db->commit(); + $successmsg = 'Product was saved successfully!'; + } catch (Exception $e) { + $db->rollBack(); + $errormsg = 'Product could not be added! :('; + } +} +$stm = $db->prepare('SELECT * FROM products'); +$stm->execute(); +$products = $stm->fetchAll(PDO::FETCH_ASSOC); +?> + + + + +
+ +
+ +

Products

+ + + + + + + + + + + + + + + + + + + + + +
IDPRODUCTUNIT PRICESTOCKACTIONS
+ + \ No newline at end of file From 800d570c0d5c2d35a5e706bc3a7cf623dabe00c5 Mon Sep 17 00:00:00 2001 From: Antonio Spinelli Date: Thu, 9 Jun 2016 18:43:21 -0300 Subject: [PATCH 3/3] add wishlist feature add product and email in customer wish list --- README.md | 6 +++ cli/wishlist_notification.php | 29 +++++++++++++ lib/functions.php | 31 ++++++++++++++ public/wishlist.php | 81 +++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 cli/wishlist_notification.php create mode 100644 public/wishlist.php diff --git a/README.md b/README.md index 6e25bc4..5e3f602 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,12 @@ Eu como um usuário convidado, quero adicionar um produto esgotado em minha list #### Run Application +create sqlite database +```shell +$ php cli/create_tables.php +``` + +start php built-in server ```shell $ php -S localhost:8000 -t public ``` \ No newline at end of file diff --git a/cli/wishlist_notification.php b/cli/wishlist_notification.php new file mode 100644 index 0000000..ffc2cc1 --- /dev/null +++ b/cli/wishlist_notification.php @@ -0,0 +1,29 @@ + 0 + AND wishlists.status = 'P' +SQL; +$stm = $db->prepare($query); +$stm->execute(); +$wishlists = $stm->fetchAll(PDO::FETCH_ASSOC); + +foreach ($wishlists as $wishlist) { + echo sprintf( + 'sending email for: %s with "%s".', + $wishlist['email'], + $wishlist['product_name'] + ) . PHP_EOL; + $stm = $db->prepare("UPDATE wishlists SET status = 'S' WHERE id = ?"); + $stm->execute([$wishlist['id']]); +} \ No newline at end of file diff --git a/lib/functions.php b/lib/functions.php index ae80e96..3d06f11 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -41,3 +41,34 @@ function removeUrl($uri, $id, array $extraQuery = array()) $query = http_build_query(array_merge(['remove' => $id], $extraQuery)); return sprintf('remove', $uri, $query); } + + +/** + * validate wish list data + * @param array $data + * @return bool + */ +function isValidWishList(array $data) +{ + if (!isset($data['product_id']) || !assert(is_numeric($data['product_id']))) { + return false; + } + if (!isset($data['email']) || !assert(filter_var($data['email'], FILTER_VALIDATE_EMAIL) !== false)) { + return false; + } + return true; +} + +/** + * Gets wish list data. + * @param array $data + * @return array + */ +function getWishList(array $data) +{ + return array( + 'email' => $data['email'], + 'product_id' => $data['product_id'], + 'status' => 'P', + ); +} \ No newline at end of file diff --git a/public/wishlist.php b/public/wishlist.php new file mode 100644 index 0000000..3b1648b --- /dev/null +++ b/public/wishlist.php @@ -0,0 +1,81 @@ +beginTransaction(); + try { + $stm = $db->prepare('INSERT INTO wishlists (email, product_id) VALUES (?, ?)'); + $stm->execute([$wishItem['email'], $wishItem['product_id']]); + $db->commit(); + $successmsg = 'Product was added at wish list successfully!'; + } catch (Exception $e) { + $db->rollBack(); + $errormsg = 'Product could not be added at wishlist! :('; + } +} + +if (isset($_GET['remove'])) { + $db->beginTransaction(); + try { + $stm = $db->prepare('DELETE FROM wishlists WHERE id = ?'); + $stm->execute([$_GET['remove']]); + $db->commit(); + $successmsg = 'Product was removed from wish list successfully!'; + } catch (Exception $e) { + $db->rollBack(); + $errormsg = 'Product could not be removed at wishlist! :('; + } + header('Location: /wishlist.php?'.http_build_query(['email' => $_GET['email']])); +} + +$query = <<prepare($query); +$stm->execute([$_GET['email']]); +$wishlist = $stm->fetchAll(PDO::FETCH_ASSOC); +?> + + + + +
+ +
+ +

My Wish List

+ + + + + + + + + + + + + + + + + + + +
IDPRODUCTSTATUSACTIONS
$_GET['email']]); ?>
+ + \ No newline at end of file