Skip to content

Commit

Permalink
Merge pull request #231 from Divyeshhhh/useenum
Browse files Browse the repository at this point in the history
refactor OrderProduct model to use enums for cup size and milk type
  • Loading branch information
creme332 authored Jul 9, 2024
2 parents 0215865 + 4d094c4 commit 23084de
Show file tree
Hide file tree
Showing 14 changed files with 156 additions and 92 deletions.
2 changes: 1 addition & 1 deletion src/controllers/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private function getHandler(string $controllerName): ?string
); // Replace placeholders with regex capture groups
$pattern = '/^' . $pattern . '$/';

if (preg_match($pattern, '/' . Utility::getURL(), $matches)) {
if (preg_match($pattern, '/' . Utility::getURL())) {
return $handler;
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/controllers/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Steamy\Model\OrderProduct;
use Steamy\Model\Product;
use Steamy\Model\Store;
use Steamy\Model\OrderMilkType;
use Steamy\Model\OrderCupSize;

class Cart
{
Expand Down Expand Up @@ -101,8 +103,8 @@ private function handleCheckout(): void
foreach ($form_data['items'] as $item) {
$line_item = new OrderProduct(
product_id: filter_var($item['productID'], FILTER_VALIDATE_INT),
cup_size: strtolower($item['cupSize']),
milk_type: strtolower($item['milkType']),
cup_size: OrderCupSize::from(strtolower($item['cupSize'])),
milk_type: OrderMilkType::from(strtolower($item['milkType'])),
quantity: filter_var($item['quantity'], FILTER_VALIDATE_INT)
);
$new_order->addLineItem($line_item);
Expand Down Expand Up @@ -141,6 +143,7 @@ private function handleCheckout(): void
if (!$success_mail) {
http_response_code(503);
echo json_encode(['error' => "Order was saved but email could not be sent for an unknown reason."]);
return;
}

// if everything is good, tell client to reset the document view
Expand Down
5 changes: 5 additions & 0 deletions src/controllers/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ private function displayProfileDetails(Client $client, string $password = "", st
);
}

/**
* @throws Exception
*/
public function reorderOrder(): void
{
$order_id = (int)($_POST['order_id'] ?? -1);
Expand All @@ -127,6 +130,8 @@ public function reorderOrder(): void
} catch (Exception $e) {
$this->view_data['order_action_error'] = $e->getMessage();
}

$this->signed_client->sendOrderConfirmationEmail($new_order);
}

public function cancelOrder(): void
Expand Down
8 changes: 4 additions & 4 deletions src/models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(
array $line_items = [],
?int $order_id = null,
?DateTime $pickup_date = null,
OrderStatus $status = OrderStatus::PENDING, // Default to 'pending',
OrderStatus $status = OrderStatus::PENDING,
DateTime $created_date = new DateTime(),
) {
$this->store_id = $store_id;
Expand Down Expand Up @@ -273,7 +273,7 @@ public function cancelOrder(): void
$stm->execute(['order_id' => $this->order_id]);

$conn->commit();
} catch (PDOException $e) {
} catch (PDOException) {
$conn->rollBack();
} finally {
$conn = null;
Expand Down Expand Up @@ -302,8 +302,8 @@ public static function getOrderProducts(int $order_id): array
foreach ($data as $result) {
$order_products_arr[] = new OrderProduct(
product_id: $result->product_id,
cup_size: $result->cup_size,
milk_type: $result->milk_type,
cup_size: OrderCupSize::from($result->cup_size), // Convert string to enum
milk_type: OrderMilkType::from($result->milk_type), // Convert string to enum
quantity: $result->quantity,
unit_price: (float)$result->unit_price,
order_id: $result->order_id,
Expand Down
12 changes: 12 additions & 0 deletions src/models/OrderCupSize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Steamy\Model;

enum OrderCupSize: string
{
case SMALL = 'small';
case MEDIUM = 'medium';
case LARGE = 'large';
}
14 changes: 14 additions & 0 deletions src/models/OrderMilkType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Steamy\Model;

enum OrderMilkType: string
{
case ALMOND = 'almond';
case COCONUT = 'coconut';
case OAT = 'oat';
case SOY = 'soy';

}
36 changes: 14 additions & 22 deletions src/models/OrderProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ class OrderProduct

private int $order_id;
private int $product_id;
private string $cup_size;
private string $milk_type;
private OrderCupSize $cup_size;
private OrderMilkType $milk_type;
private int $quantity;
private float $unit_price;

/**
* Create a new OrderProduct object
* @param int $product_id
* @param string $cup_size
* @param string $milk_type
* @param OrderCupSize $cup_size
* @param OrderMilkType $milk_type
* @param int $quantity
* @param float|null $unit_price If not set, the default $unit_price is -1.
* @param int|null $order_id If not set, the default $order_id is -1.
*/
public function __construct(
int $product_id,
string $cup_size,
string $milk_type,
OrderCupSize $cup_size,
OrderMilkType $milk_type,
int $quantity,
?float $unit_price = null,
?int $order_id = null,
Expand Down Expand Up @@ -72,14 +72,6 @@ public function validate(): array
$errors['quantity'] = 'Quantity must be a positive integer';
}

if (!in_array($this->milk_type, ['almond', 'coconut', 'oat', 'soy'])) {
$errors['milk_type'] = 'Milk type invalid';
}

if (!in_array($this->cup_size, ['small', 'medium', 'large'])) {
$errors['cup_size'] = 'Cup size type invalid';
}

if ($this->unit_price <= 0) {
$errors['unit_price'] = 'Unit price cannot be negative';
}
Expand Down Expand Up @@ -107,8 +99,8 @@ public static function getByID(int $order_id, int $product_id): ?OrderProduct

return new OrderProduct(
product_id: $result->product_id,
cup_size: $result->cup_size,
milk_type: $result->milk_type,
cup_size: OrderCupSize::from($result->cup_size),
milk_type: OrderMilkType::from($result->milk_type),
quantity: $result->quantity,
unit_price: (float)$result->unit_price,
order_id: $result->order_id,
Expand All @@ -130,12 +122,12 @@ public function getProductName(): string
return Product::getByID($this->product_id)->getName();
}

public function getCupSize(): string
public function getCupSize(): OrderCupSize
{
return $this->cup_size;
}

public function getMilkType(): string
public function getMilkType(): OrderMilkType
{
return $this->milk_type;
}
Expand All @@ -160,12 +152,12 @@ public function setProductID(int $product_id): void
$this->product_id = $product_id;
}

public function setCupSize(string $cup_size): void
public function setCupSize(OrderCupSize $cup_size): void
{
$this->cup_size = $cup_size;
}

public function setMilkType(string $milk_type): void
public function setMilkType(OrderMilkType $milk_type): void
{
$this->milk_type = $milk_type;
}
Expand All @@ -185,8 +177,8 @@ public function toArray(): array
return [
'order_id' => $this->order_id,
'product_id' => $this->product_id,
'cup_size' => $this->cup_size,
'milk_type' => $this->milk_type,
'cup_size' => $this->cup_size->value,
'milk_type' => $this->milk_type->value,
'quantity' => $this->quantity,
'unit_price' => $this->unit_price,
];
Expand Down
38 changes: 26 additions & 12 deletions src/views/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @var string $defaultLastName Default value for last name input field
* @var string $defaultEmail Default value for email input field
* @var string $defaultMessage Default value for message textarea
* @var bool $contact_us_successful Whether contact was successful
* @var array $errors Array of validation errors
*/

Expand All @@ -19,28 +20,40 @@
<h1>Contact Us</h1>
<form action="" method="post">
<label for="firstname">First name</label>
<input type="text" id="firstname" name="first_name" value="<?= htmlspecialchars($defaultFirstName) ?>" required>
<?php if (isset($errors['first_name'])) : ?>
<input type="text" id="firstname" name="first_name" value="<?= htmlspecialchars($defaultFirstName) ?>"
required>
<?php
if (isset($errors['first_name'])) : ?>
<small class="warning"><?= $errors['first_name'] ?></small>
<?php endif; ?>
<?php
endif; ?>

<label for="lastname">Last name</label>
<input type="text" id="lastname" name="last_name" value="<?= htmlspecialchars($defaultLastName) ?>" required>
<?php if (isset($errors['last_name'])) : ?>
<input type="text" id="lastname" name="last_name" value="<?= htmlspecialchars($defaultLastName) ?>"
required>
<?php
if (isset($errors['last_name'])) : ?>
<small class="warning"><?= $errors['last_name'] ?></small>
<?php endif; ?>
<?php
endif; ?>

<label for="email">Your email</label>
<input type="email" id="email" name="email" value="<?= htmlspecialchars($defaultEmail) ?>" required>
<?php if (isset($errors['email'])) : ?>
<?php
if (isset($errors['email'])) : ?>
<small class="warning"><?= $errors['email'] ?></small>
<?php endif; ?>
<?php
endif; ?>

<label for="message">Your message</label>
<textarea id="message" name="message" rows="5" cols="30" required><?= htmlspecialchars($defaultMessage) ?></textarea>
<?php if (isset($errors['message'])) : ?>
<textarea id="message" name="message" rows="5" cols="30" required><?= htmlspecialchars(
$defaultMessage
) ?></textarea>
<?php
if (isset($errors['message'])) : ?>
<small class="warning"><?= $errors['message'] ?></small>
<?php endif; ?>
<?php
endif; ?>

<input type="submit" name="form_submit" value="Submit">
</form>
Expand All @@ -50,7 +63,8 @@
<dialog <?= $contact_us_successful ? "open" : "" ?>>
<article>
<h3>Thank You for Contacting Us! 🔎</h3>
<p>Your message has been successfully sent. Our team will review your inquiry and get back to you shortly. We appreciate your interest in our services.</p>
<p>Your message has been successfully sent. Our team will review your inquiry and get back to you shortly. We
appreciate your interest in our services.</p>
<footer>
<a href="/home" role="button" data-target="my-modal">Return to Home</a>
</footer>
Expand Down
4 changes: 2 additions & 2 deletions src/views/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
<tr>
<td><?= htmlspecialchars($item->getProductName()) ?></td>
<td><?= filter_var($item->getQuantity(), FILTER_SANITIZE_NUMBER_INT) ?></td>
<td><?= htmlspecialchars($item->getMilkType()) ?></td>
<td><?= htmlspecialchars($item->getCupSize()) ?></td>
<td><?= htmlspecialchars(ucfirst($item->getMilkType()->value)) ?></td>
<td><?= htmlspecialchars(ucfirst($item->getCupSize()->value)) ?></td>
<td>$<?= (number_format($item->getUnitPrice(), 2)) ?></td>
</tr>
<?php
Expand Down
79 changes: 45 additions & 34 deletions src/views/mails/Contact.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
<?php

declare(strict_types=1);

/**
* View for contact us page
*
* @var string $first_name First name of sender
* @var string $last_name Last name of sender
* @var string $email Email of sender
* @var string $message Message of sender
*/
?>
<!DOCTYPE html>
<html lang="en">
Expand All @@ -10,42 +17,46 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>New Contact Message</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
background-color: #f4f4f4;
padding: 20px;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
h2 {
color: #555;
}
p {
margin: 0 0 10px;
}
.footer {
margin-top: 20px;
font-size: 0.9em;
color: #888;
}
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
background-color: #f4f4f4;
padding: 20px;
}

.container {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

h2 {
color: #555;
}

p {
margin: 0 0 10px;
}

.footer {
margin-top: 20px;
font-size: 0.9em;
color: #888;
}
</style>
</head>
<body>
<div class="container">
<h2>New Contact Message</h2>
<p><strong>Name:</strong> <?= htmlspecialchars($first_name) ?> <?= htmlspecialchars($last_name) ?></p>
<p><strong>Email:</strong> <?= htmlspecialchars($email) ?></p>
<p><strong>Message:</strong></p>
<p><?= nl2br(htmlspecialchars($message)) ?></p>
<div class="footer">
<p>This message was sent via the contact form on your website.</p>
</div>
<div class="container">
<h2>New Contact Message</h2>
<p><strong>Name:</strong> <?= htmlspecialchars($first_name) ?> <?= htmlspecialchars($last_name) ?></p>
<p><strong>Email:</strong> <?= htmlspecialchars($email) ?></p>
<p><strong>Message:</strong></p>
<p><?= nl2br(htmlspecialchars($message)) ?></p>
<div class="footer">
<p>This message was sent via the contact form on your website.</p>
</div>
</div>
</body>
</html>
Loading

0 comments on commit 23084de

Please sign in to comment.