Skip to content

Commit

Permalink
add wishlist feature
Browse files Browse the repository at this point in the history
add product and email in customer wish list
  • Loading branch information
tonicospinelli committed Jun 9, 2016
1 parent 59a49ea commit 800d570
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
29 changes: 29 additions & 0 deletions cli/wishlist_notification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

require_once __DIR__ . '/../lib/functions.php';
require_once __DIR__ . '/../lib/dbconn.php';

$query = <<<SQL
SELECT
wishlists.id, wishlists.email, products.name as product_name
FROM
wishlists
INNER JOIN
products ON products.id = product_id
WHERE
products.stock > 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']]);
}
31 changes: 31 additions & 0 deletions lib/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,34 @@ function removeUrl($uri, $id, array $extraQuery = array())
$query = http_build_query(array_merge(['remove' => $id], $extraQuery));
return sprintf('<a href="/%s?%s">remove</a>', $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',
);
}
81 changes: 81 additions & 0 deletions public/wishlist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

require_once __DIR__ . '/../lib/functions.php';
require_once __DIR__ . '/../lib/dbconn.php';

$errormsg = null;
$successmsg = null;

if (isset($_POST['submit']) && isValidWishList($_POST['wish_item'])) {
$wishItem = getWishList($_POST['wish_item']);
$db->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 = <<<SQL
SELECT
wishlists.id, products.name as product_name, products.stock as product_stock, wishlists.status
FROM
wishlists
INNER JOIN
products ON products.id = product_id
WHERE email = ?
SQL;

$stm = $db->prepare($query);
$stm->execute([$_GET['email']]);
$wishlist = $stm->fetchAll(PDO::FETCH_ASSOC);
?>
<html>
<head></head>
<body>
<?php if (null !== $errormsg): ?>
<div class="alert error"><?php echo $errormsg; ?> </div>
<?php elseif (isset($wishItem)): ?>
<div class="alert success"><?php echo $successmsg; ?></div>
<?php endif; ?>
<h3>My Wish List</h3>
<table>
<thead>
<tr>
<th>ID</th>
<th>PRODUCT</th>
<th>STATUS</th>
<th>ACTIONS</th>
</tr>
</thead>
<tbody>
<?php foreach ($wishlist as $wish): ?>
<tr>
<td><?php echo $wish['id']; ?> </td>
<td><?php echo $wish['product_name']; ?> </td>
<td><?php echo ($wish['status'] == 'P' && $wish['product_stock'] == 0 ? 'Not Available' : 'Available'); ?> </td>
<td><?php echo removeUrl('wishlist.php', $wish['id'], ['email' => $_GET['email']]); ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>

0 comments on commit 800d570

Please sign in to comment.