Skip to content

Commit 7e8697a

Browse files
Merge pull request #1 from tonicospinelli/01-flat-php
guest user adds product at its wishlist
2 parents d5bd1d8 + 800d570 commit 7e8697a

File tree

9 files changed

+284
-0
lines changed

9 files changed

+284
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/data/*
2+
!/data/.gitkeep

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Developing for Business
2+
=======================
3+
4+
#### User Story
5+
6+
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.
7+
8+
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.
9+
10+
11+
#### Requirements
12+
13+
* PHP 5.6+
14+
* PDO + SQLite Driver
15+
16+
#### Run Application
17+
18+
create sqlite database
19+
```shell
20+
$ php cli/create_tables.php
21+
```
22+
23+
start php built-in server
24+
```shell
25+
$ php -S localhost:8000 -t public
26+
```

cli/create_tables.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../lib/dbconn.php';
4+
5+
$db->query("CREATE TABLE IF NOT EXISTS wishlists (id INTEGER PRIMARY KEY AUTOINCREMENT, email varchar(255), product_id int, status VARCHAR(1) DEFAULT 'P');");
6+
$db->query('CREATE TABLE IF NOT EXISTS products (id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar(255), unit_price DECIMAL(10,2), stock int);');
7+
$stm = $db->query("INSERT INTO products VALUES (null, 'Camiseta', 59.9, 10);");
8+
$stm = $db->query("INSERT INTO products VALUES (null, 'Bermuda', 69.9, 10);");

cli/wishlist_notification.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../lib/functions.php';
4+
require_once __DIR__ . '/../lib/dbconn.php';
5+
6+
$query = <<<SQL
7+
SELECT
8+
wishlists.id, wishlists.email, products.name as product_name
9+
FROM
10+
wishlists
11+
INNER JOIN
12+
products ON products.id = product_id
13+
WHERE
14+
products.stock > 0
15+
AND wishlists.status = 'P'
16+
SQL;
17+
$stm = $db->prepare($query);
18+
$stm->execute();
19+
$wishlists = $stm->fetchAll(PDO::FETCH_ASSOC);
20+
21+
foreach ($wishlists as $wishlist) {
22+
echo sprintf(
23+
'sending email for: %s with "%s".',
24+
$wishlist['email'],
25+
$wishlist['product_name']
26+
) . PHP_EOL;
27+
$stm = $db->prepare("UPDATE wishlists SET status = 'S' WHERE id = ?");
28+
$stm->execute([$wishlist['id']]);
29+
}

data/.gitkeep

Whitespace-only changes.

lib/dbconn.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
$database = __DIR__ . '/../data/business.db';
4+
$dsn = 'sqlite:' . $database;
5+
$db = new PDO($dsn);
6+

lib/functions.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/**
4+
* validate wish list data
5+
* @param array $data
6+
* @return bool
7+
*/
8+
function isValidProduct(array $data)
9+
{
10+
if (!isset($data['name']) || !assert(is_string($data['name']) !== false)) {
11+
return false;
12+
}
13+
if (!isset($data['stock']) || !assert(is_numeric($data['stock']))) {
14+
return false;
15+
}
16+
return true;
17+
}
18+
19+
/**
20+
* Gets wish list data.
21+
* @param array $data
22+
* @return array
23+
*/
24+
function getProduct(array $data)
25+
{
26+
return array(
27+
'id' => (isset($data['id']) ? $data['id'] : null),
28+
'name' => $data['name'],
29+
'price' => $data['price'],
30+
'stock' => $data['stock']
31+
);
32+
}
33+
34+
/**
35+
* Creates a url to remove product from wish list
36+
* @param int $id
37+
* @return string
38+
*/
39+
function removeUrl($uri, $id, array $extraQuery = array())
40+
{
41+
$query = http_build_query(array_merge(['remove' => $id], $extraQuery));
42+
return sprintf('<a href="/%s?%s">remove</a>', $uri, $query);
43+
}
44+
45+
46+
/**
47+
* validate wish list data
48+
* @param array $data
49+
* @return bool
50+
*/
51+
function isValidWishList(array $data)
52+
{
53+
if (!isset($data['product_id']) || !assert(is_numeric($data['product_id']))) {
54+
return false;
55+
}
56+
if (!isset($data['email']) || !assert(filter_var($data['email'], FILTER_VALIDATE_EMAIL) !== false)) {
57+
return false;
58+
}
59+
return true;
60+
}
61+
62+
/**
63+
* Gets wish list data.
64+
* @param array $data
65+
* @return array
66+
*/
67+
function getWishList(array $data)
68+
{
69+
return array(
70+
'email' => $data['email'],
71+
'product_id' => $data['product_id'],
72+
'status' => 'P',
73+
);
74+
}

public/product.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../lib/functions.php';
4+
require_once __DIR__ . '/../lib/dbconn.php';
5+
6+
$errormsg = null;
7+
$successmsg = null;
8+
9+
if (isset($_POST['submit']) && isValidProduct($_POST['product'])) {
10+
$product = getProduct($_POST['product']);
11+
$db->beginTransaction();
12+
try {
13+
$stm = $db->prepare('INSERT INTO products (name, unit_price, stock) VALUES (?, ?, ?)');
14+
$stm->execute([$product['name'], $product['unit_price'], $product['stock']]);
15+
$db->commit();
16+
$successmsg = 'Product was saved successfully!';
17+
} catch (Exception $e) {
18+
$db->rollBack();
19+
$errormsg = 'Product could not be added! :(';
20+
}
21+
}
22+
$stm = $db->prepare('SELECT * FROM products');
23+
$stm->execute();
24+
$products = $stm->fetchAll(PDO::FETCH_ASSOC);
25+
?>
26+
<html>
27+
<head></head>
28+
<body>
29+
<?php if (null !== $errormsg): ?>
30+
<div class="alert error"><?php echo $errormsg; ?> </div>
31+
<?php elseif (isset($product)): ?>
32+
<div class="alert success"><?php echo $successmsg; ?></div>
33+
<?php endif; ?>
34+
<h3>Products</h3>
35+
<table>
36+
<thead>
37+
<tr>
38+
<th>ID</th>
39+
<th>PRODUCT</th>
40+
<th>UNIT PRICE</th>
41+
<th>STOCK</th>
42+
<th>ACTIONS</th>
43+
</tr>
44+
</thead>
45+
<tbody>
46+
<?php foreach ($products as $product): ?>
47+
<tr>
48+
<td><?php echo $product['id']; ?> </td>
49+
<td><?php echo $product['name']; ?> </td>
50+
<td><?php echo $product['unit_price']; ?> </td>
51+
<td><?php echo $product['stock']; ?> </td>
52+
<td><?php echo removeUrl('product.php', $product['id']); ?> </td>
53+
</tr>
54+
<?php endforeach; ?>
55+
</tbody>
56+
</table>
57+
</body>
58+
</html>

public/wishlist.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../lib/functions.php';
4+
require_once __DIR__ . '/../lib/dbconn.php';
5+
6+
$errormsg = null;
7+
$successmsg = null;
8+
9+
if (isset($_POST['submit']) && isValidWishList($_POST['wish_item'])) {
10+
$wishItem = getWishList($_POST['wish_item']);
11+
$db->beginTransaction();
12+
try {
13+
$stm = $db->prepare('INSERT INTO wishlists (email, product_id) VALUES (?, ?)');
14+
$stm->execute([$wishItem['email'], $wishItem['product_id']]);
15+
$db->commit();
16+
$successmsg = 'Product was added at wish list successfully!';
17+
} catch (Exception $e) {
18+
$db->rollBack();
19+
$errormsg = 'Product could not be added at wishlist! :(';
20+
}
21+
}
22+
23+
if (isset($_GET['remove'])) {
24+
$db->beginTransaction();
25+
try {
26+
$stm = $db->prepare('DELETE FROM wishlists WHERE id = ?');
27+
$stm->execute([$_GET['remove']]);
28+
$db->commit();
29+
$successmsg = 'Product was removed from wish list successfully!';
30+
} catch (Exception $e) {
31+
$db->rollBack();
32+
$errormsg = 'Product could not be removed at wishlist! :(';
33+
}
34+
header('Location: /wishlist.php?'.http_build_query(['email' => $_GET['email']]));
35+
}
36+
37+
$query = <<<SQL
38+
SELECT
39+
wishlists.id, products.name as product_name, products.stock as product_stock, wishlists.status
40+
FROM
41+
wishlists
42+
INNER JOIN
43+
products ON products.id = product_id
44+
WHERE email = ?
45+
SQL;
46+
47+
$stm = $db->prepare($query);
48+
$stm->execute([$_GET['email']]);
49+
$wishlist = $stm->fetchAll(PDO::FETCH_ASSOC);
50+
?>
51+
<html>
52+
<head></head>
53+
<body>
54+
<?php if (null !== $errormsg): ?>
55+
<div class="alert error"><?php echo $errormsg; ?> </div>
56+
<?php elseif (isset($wishItem)): ?>
57+
<div class="alert success"><?php echo $successmsg; ?></div>
58+
<?php endif; ?>
59+
<h3>My Wish List</h3>
60+
<table>
61+
<thead>
62+
<tr>
63+
<th>ID</th>
64+
<th>PRODUCT</th>
65+
<th>STATUS</th>
66+
<th>ACTIONS</th>
67+
</tr>
68+
</thead>
69+
<tbody>
70+
<?php foreach ($wishlist as $wish): ?>
71+
<tr>
72+
<td><?php echo $wish['id']; ?> </td>
73+
<td><?php echo $wish['product_name']; ?> </td>
74+
<td><?php echo ($wish['status'] == 'P' && $wish['product_stock'] == 0 ? 'Not Available' : 'Available'); ?> </td>
75+
<td><?php echo removeUrl('wishlist.php', $wish['id'], ['email' => $_GET['email']]); ?> </td>
76+
</tr>
77+
<?php endforeach; ?>
78+
</tbody>
79+
</table>
80+
</body>
81+
</html>

0 commit comments

Comments
 (0)