Skip to content

Commit

Permalink
Merge pull request #6 from tonicospinelli/06-front-controller
Browse files Browse the repository at this point in the history
refactoring: create front controller
  • Loading branch information
tonicospinelli authored Jun 20, 2016
2 parents 4aa1c28 + ecfaa66 commit cf52cd6
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 58 deletions.
3 changes: 3 additions & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
define('DATABASE_DSN', 'sqlite:' . __DIR__ . '/../data/business.db');
define('DATABASE_USERNAME', null);
define('DATABASE_PASSWORD', null);

define('TEMPLATE_DIR', __DIR__ . '/../templates');
define('LIBRARY_DIR', __DIR__ . '/../lib');
28 changes: 28 additions & 0 deletions lib/controllers/product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

function productListAction()
{
$products = findAllProducts();
include TEMPLATE_DIR . '/products/list.php';
}

function productAddAction($post)
{
$product = getProductFromPost($post['product']);

if (addProduct($product)) {
$successmsg = 'Product was saved successfully!';
} else {
$errormsg = 'Product could not be added! :(';
}
productListAction();
}

function productRemoveAction($id)
{
if (!removeProduct($id)) {
$errormsg = 'Product could not be removed! :(';
}

header('Location: /index.php/products');
}
27 changes: 27 additions & 0 deletions lib/controllers/wishlist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

function wishlistListAction($email)
{
$wishlist = findAllWishProducts($email);
include TEMPLATE_DIR . '/wishlists/list.php';
}

function wishlistAddAction($email, $post)
{
$wishItem = getWishList($post['wish_item']);
if (addWishItem($wishItem)) {
$successmsg = 'Product was added at wish list successfully!';
} else {
$errormsg = 'Product could not be added at wishlist! :(';
}
wishlistListAction($email);
}

function wishlistRemoveAction($email, $id)
{
if (!removeWishItem($id)) {
$errormsg = 'Product could not be removed from wishlist! :(';
}
header('Location: /wishlist?' . http_build_query(['email' => $_GET['email']]));

}
2 changes: 0 additions & 2 deletions lib/dbconn.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

require_once __DIR__ . '/../config/app.php';

/**
* @return PDO
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
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);
return sprintf('<a href="%s/remove?%s">remove</a>', $uri, $query);
}
17 changes: 16 additions & 1 deletion lib/models/product.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function getProductFromPost(array $data)
return array(
'id' => (isset($data['id']) ? $data['id'] : null),
'name' => $data['name'],
'price' => $data['price'],
'unit_price' => $data['unit_price'],
'stock' => $data['stock']
);
}
Expand Down Expand Up @@ -62,3 +62,18 @@ function addProduct(array $product)
}
return false;
}

function removeProduct($id)
{
$db = dbConnect();
$db->beginTransaction();
try {
$stm = $db->prepare('DELETE FROM products WHERE id = ?');
$stm->execute([$id]);
$db->commit();
return true;
} catch (Exception $e) {
$db->rollBack();
}
return false;
}
30 changes: 30 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

// load and initialize any global libraries
require_once __DIR__ . '/../config/app.php';
require_once LIBRARY_DIR . '/functions.php';
require_once LIBRARY_DIR . '/dbconn.php';
require_once LIBRARY_DIR . '/models/product.php';
require_once LIBRARY_DIR . '/controllers/product.php';
require_once LIBRARY_DIR . '/models/wishlist.php';
require_once LIBRARY_DIR . '/controllers/wishlist.php';

// route the request internally
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

if ('/index.php/products' === $uri) {
productListAction();
} elseif ('/index.php/products/add' === $uri && isset($_POST['product']) && isValidProduct($_POST['product'])) {
productAddAction($_POST);
} elseif ('/index.php/products/remove' === $uri && isset($_GET['remove'])) {
productRemoveAction($_GET['remove']);
} elseif ('/index.php/wishlist' === $uri && isset($_GET['email'])) {
wishlistListAction($_GET['email']);
} elseif ('/index.php/wishlist/add' === $uri && isset($_GET['email']) && isset($_POST['submit']) && isValidWishList($_POST['wish_item'])) {
wishlistAddAction($_GET['email'], $_POST);
} elseif ('/index.php/wishlist/remove' === $uri && isset($_GET['email']) && isset($_GET['remove'])) {
wishlistRemoveAction($_GET['email'], $_GET['remove']);
} else {
header('HTTP/1.1 404 Not Found');
echo '<html><body><h1>Page Not Found</h1></body></html>';
}
21 changes: 0 additions & 21 deletions public/product.php

This file was deleted.

29 changes: 0 additions & 29 deletions public/wishlist.php

This file was deleted.

4 changes: 2 additions & 2 deletions templates/products/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<?php ob_start() ?>
<h3>Products</h3>
<?php if (null !== $errormsg): ?>
<?php if (isset($errormsg)): ?>
<div class="alert error"><?php echo $errormsg; ?> </div>
<?php elseif (isset($product)): ?>
<div class="alert success"><?php echo $successmsg; ?></div>
Expand All @@ -24,7 +24,7 @@
<td><?php echo $product['name']; ?> </td>
<td><?php echo $product['unit_price']; ?> </td>
<td><?php echo $product['stock']; ?> </td>
<td><?php echo removeUrl('product.php', $product['id']); ?> </td>
<td><?php echo removeUrl('products', $product['id']); ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
Expand Down
4 changes: 2 additions & 2 deletions templates/wishlists/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<?php ob_start() ?>
<h3><?php echo $title;?></h3>
<?php if (null !== $errormsg): ?>
<?php if (isset($errormsg)): ?>
<div class="alert error"><?php echo $errormsg; ?> </div>
<?php elseif (isset($wishItem)): ?>
<div class="alert success"><?php echo $successmsg; ?></div>
Expand All @@ -26,7 +26,7 @@
<?php else: ?>
<td>Available</td>
<?php endif; ?>
<td><?php echo removeUrl('wishlist.php', $wish['id'], ['email' => $_GET['email']]); ?> </td>
<td><?php echo removeUrl('wishlist', $wish['id'], ['email' => $_GET['email']]); ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
Expand Down

0 comments on commit cf52cd6

Please sign in to comment.