Skip to content

Commit cf52cd6

Browse files
Merge pull request #6 from tonicospinelli/06-front-controller
refactoring: create front controller
2 parents 4aa1c28 + ecfaa66 commit cf52cd6

File tree

11 files changed

+109
-58
lines changed

11 files changed

+109
-58
lines changed

config/app.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
define('DATABASE_DSN', 'sqlite:' . __DIR__ . '/../data/business.db');
44
define('DATABASE_USERNAME', null);
55
define('DATABASE_PASSWORD', null);
6+
7+
define('TEMPLATE_DIR', __DIR__ . '/../templates');
8+
define('LIBRARY_DIR', __DIR__ . '/../lib');

lib/controllers/product.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
function productListAction()
4+
{
5+
$products = findAllProducts();
6+
include TEMPLATE_DIR . '/products/list.php';
7+
}
8+
9+
function productAddAction($post)
10+
{
11+
$product = getProductFromPost($post['product']);
12+
13+
if (addProduct($product)) {
14+
$successmsg = 'Product was saved successfully!';
15+
} else {
16+
$errormsg = 'Product could not be added! :(';
17+
}
18+
productListAction();
19+
}
20+
21+
function productRemoveAction($id)
22+
{
23+
if (!removeProduct($id)) {
24+
$errormsg = 'Product could not be removed! :(';
25+
}
26+
27+
header('Location: /index.php/products');
28+
}

lib/controllers/wishlist.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
function wishlistListAction($email)
4+
{
5+
$wishlist = findAllWishProducts($email);
6+
include TEMPLATE_DIR . '/wishlists/list.php';
7+
}
8+
9+
function wishlistAddAction($email, $post)
10+
{
11+
$wishItem = getWishList($post['wish_item']);
12+
if (addWishItem($wishItem)) {
13+
$successmsg = 'Product was added at wish list successfully!';
14+
} else {
15+
$errormsg = 'Product could not be added at wishlist! :(';
16+
}
17+
wishlistListAction($email);
18+
}
19+
20+
function wishlistRemoveAction($email, $id)
21+
{
22+
if (!removeWishItem($id)) {
23+
$errormsg = 'Product could not be removed from wishlist! :(';
24+
}
25+
header('Location: /wishlist?' . http_build_query(['email' => $_GET['email']]));
26+
27+
}

lib/dbconn.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
require_once __DIR__ . '/../config/app.php';
4-
53
/**
64
* @return PDO
75
*/

lib/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
function removeUrl($uri, $id, array $extraQuery = array())
99
{
1010
$query = http_build_query(array_merge(['remove' => $id], $extraQuery));
11-
return sprintf('<a href="/%s?%s">remove</a>', $uri, $query);
11+
return sprintf('<a href="%s/remove?%s">remove</a>', $uri, $query);
1212
}

lib/models/product.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function getProductFromPost(array $data)
2626
return array(
2727
'id' => (isset($data['id']) ? $data['id'] : null),
2828
'name' => $data['name'],
29-
'price' => $data['price'],
29+
'unit_price' => $data['unit_price'],
3030
'stock' => $data['stock']
3131
);
3232
}
@@ -62,3 +62,18 @@ function addProduct(array $product)
6262
}
6363
return false;
6464
}
65+
66+
function removeProduct($id)
67+
{
68+
$db = dbConnect();
69+
$db->beginTransaction();
70+
try {
71+
$stm = $db->prepare('DELETE FROM products WHERE id = ?');
72+
$stm->execute([$id]);
73+
$db->commit();
74+
return true;
75+
} catch (Exception $e) {
76+
$db->rollBack();
77+
}
78+
return false;
79+
}

public/index.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
// load and initialize any global libraries
4+
require_once __DIR__ . '/../config/app.php';
5+
require_once LIBRARY_DIR . '/functions.php';
6+
require_once LIBRARY_DIR . '/dbconn.php';
7+
require_once LIBRARY_DIR . '/models/product.php';
8+
require_once LIBRARY_DIR . '/controllers/product.php';
9+
require_once LIBRARY_DIR . '/models/wishlist.php';
10+
require_once LIBRARY_DIR . '/controllers/wishlist.php';
11+
12+
// route the request internally
13+
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
14+
15+
if ('/index.php/products' === $uri) {
16+
productListAction();
17+
} elseif ('/index.php/products/add' === $uri && isset($_POST['product']) && isValidProduct($_POST['product'])) {
18+
productAddAction($_POST);
19+
} elseif ('/index.php/products/remove' === $uri && isset($_GET['remove'])) {
20+
productRemoveAction($_GET['remove']);
21+
} elseif ('/index.php/wishlist' === $uri && isset($_GET['email'])) {
22+
wishlistListAction($_GET['email']);
23+
} elseif ('/index.php/wishlist/add' === $uri && isset($_GET['email']) && isset($_POST['submit']) && isValidWishList($_POST['wish_item'])) {
24+
wishlistAddAction($_GET['email'], $_POST);
25+
} elseif ('/index.php/wishlist/remove' === $uri && isset($_GET['email']) && isset($_GET['remove'])) {
26+
wishlistRemoveAction($_GET['email'], $_GET['remove']);
27+
} else {
28+
header('HTTP/1.1 404 Not Found');
29+
echo '<html><body><h1>Page Not Found</h1></body></html>';
30+
}

public/product.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

public/wishlist.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

templates/products/list.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<?php ob_start() ?>
44
<h3>Products</h3>
5-
<?php if (null !== $errormsg): ?>
5+
<?php if (isset($errormsg)): ?>
66
<div class="alert error"><?php echo $errormsg; ?> </div>
77
<?php elseif (isset($product)): ?>
88
<div class="alert success"><?php echo $successmsg; ?></div>
@@ -24,7 +24,7 @@
2424
<td><?php echo $product['name']; ?> </td>
2525
<td><?php echo $product['unit_price']; ?> </td>
2626
<td><?php echo $product['stock']; ?> </td>
27-
<td><?php echo removeUrl('product.php', $product['id']); ?> </td>
27+
<td><?php echo removeUrl('products', $product['id']); ?> </td>
2828
</tr>
2929
<?php endforeach; ?>
3030
</tbody>

templates/wishlists/list.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<?php ob_start() ?>
44
<h3><?php echo $title;?></h3>
5-
<?php if (null !== $errormsg): ?>
5+
<?php if (isset($errormsg)): ?>
66
<div class="alert error"><?php echo $errormsg; ?> </div>
77
<?php elseif (isset($wishItem)): ?>
88
<div class="alert success"><?php echo $successmsg; ?></div>
@@ -26,7 +26,7 @@
2626
<?php else: ?>
2727
<td>Available</td>
2828
<?php endif; ?>
29-
<td><?php echo removeUrl('wishlist.php', $wish['id'], ['email' => $_GET['email']]); ?> </td>
29+
<td><?php echo removeUrl('wishlist', $wish['id'], ['email' => $_GET['email']]); ?> </td>
3030
</tr>
3131
<?php endforeach; ?>
3232
</tbody>

0 commit comments

Comments
 (0)