public
src
Controller
Entity
Enums
Repository
AddressRepository.php
UserRepository.php
Service
bootstrap.php
templates
.gitignore
LICENSE
README.md
config.json.sample
178 lines
6.2 KiB
PHP
178 lines
6.2 KiB
PHP
<?php
|
|
/*
|
|
* Copyright (c) 2022. Micha Espey <tracer@24unix.net>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*
|
|
*/
|
|
|
|
namespace App\Repository;
|
|
|
|
use App\Entity\AddressBookEntry;
|
|
use App\Service\DatabaseConnection;
|
|
use PDO;
|
|
use PDOException;
|
|
|
|
/**
|
|
* Handles CRUD of Addresses class.
|
|
*/
|
|
class AddressRepository
|
|
{
|
|
public function __construct(private readonly DatabaseConnection $databaseConnection)
|
|
{
|
|
// empty body
|
|
}
|
|
|
|
public function findAll(string $orderBy = 'last'): array
|
|
{
|
|
$sql = "
|
|
SELECT id, owner, first, last, street, zip, city, phone
|
|
FROM " . DatabaseConnection::TABLE_ADDRESSES . "
|
|
ORDER BY :order";
|
|
|
|
try {
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
$statement->bindParam(param: ':order', var: $orderBy);
|
|
|
|
$statement->execute();
|
|
$addresses = [];
|
|
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
|
|
$address = new AddressBookEntry(
|
|
owner: htmlspecialchars(string: $result['owner']),
|
|
first: htmlspecialchars(string: $result['first']),
|
|
last: htmlspecialchars(string: $result['last']),
|
|
street: htmlspecialchars(string: $result['street']),
|
|
zip: htmlspecialchars(string: $result['zip']),
|
|
city: htmlspecialchars(string: $result['city']),
|
|
phone: htmlspecialchars(string: $result['phone']),
|
|
id: htmlspecialchars(string: $result['id']));
|
|
$addresses[] = $address;
|
|
}
|
|
return $addresses;
|
|
} catch (PDOException $e) {
|
|
exit($e->getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
public function findByID(int $id): ?AddressBookEntry
|
|
{
|
|
$sql = "
|
|
SELECT id, owner, first, last, street, zip, city, phone
|
|
FROM " . DatabaseConnection::TABLE_ADDRESSES . "
|
|
WHERE id = :id";
|
|
|
|
try {
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
$statement->bindParam(param: ':id', var: $id);
|
|
$statement->execute();
|
|
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
|
|
return new AddressBookEntry(
|
|
owner: htmlspecialchars(string: $result['owner']),
|
|
first: htmlspecialchars(string: $result['first']),
|
|
last: htmlspecialchars(string: $result['last']),
|
|
street: htmlspecialchars(string: $result['street']),
|
|
zip: htmlspecialchars(string: $result['zip']),
|
|
city: htmlspecialchars(string: $result['city']),
|
|
phone: htmlspecialchars(string: $result['phone']),
|
|
id: htmlspecialchars(string: $result['id']));
|
|
} else {
|
|
return null;
|
|
}
|
|
} catch (PDOException $e) {
|
|
exit($e->getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
public function insert(AddressBookEntry $address): bool|string
|
|
{
|
|
$sql = "
|
|
INSERT INTO " . DatabaseConnection::TABLE_ADDRESSES . " (owner, first, last, city, zip, street, phone)
|
|
VALUES (:owner, :first, :last, :city, :zip, :street, :phone)";
|
|
|
|
try {
|
|
$owner = $address->getOwner();
|
|
$first = $address->getFirst();
|
|
$last = $address->getLast();
|
|
$city = $address->getCity();
|
|
$zip = $address->getZip();
|
|
$street = $address->getStreet();
|
|
$phone = $address->getPhone();
|
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
$statement->bindParam(param: ':owner', var: $owner);
|
|
$statement->bindParam(param: ':first', var: $first);
|
|
$statement->bindParam(param: ':last', var: $last);
|
|
$statement->bindParam(param: ':city', var: $city);
|
|
$statement->bindParam(param: ':zip', var: $zip);
|
|
$statement->bindParam(param: ':street', var: $street);
|
|
$statement->bindParam(param: ':phone', var: $phone);
|
|
$statement->execute();
|
|
|
|
return $this->databaseConnection->getConnection()->lastInsertId();
|
|
} catch (PDOException $e) {
|
|
exit($e->getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
public function update(AddressBookEntry $address): bool|int
|
|
{
|
|
$id = $address->getId();
|
|
$first = $address->getFirst();
|
|
$last = $address->getLast();
|
|
$street = $address->getStreet();
|
|
$zip = $address->getZip();
|
|
$city = $address->getCity();
|
|
$phone = $address->getPhone();
|
|
|
|
$sql = "
|
|
UPDATE " . DatabaseConnection::TABLE_ADDRESSES . " SET
|
|
first = :first,
|
|
last = :last,
|
|
street = :street,
|
|
zip = :zip,
|
|
city = :city,
|
|
phone = :phone
|
|
WHERE id = :id";
|
|
|
|
try {
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
$statement->bindParam(param: 'id', var: $id);
|
|
$statement->bindParam(param: 'first', var: $first);
|
|
$statement->bindParam(param: 'last', var: $last);
|
|
$statement->bindParam(param: 'street', var: $street);
|
|
$statement->bindParam(param: 'zip', var: $zip);
|
|
$statement->bindParam(param: 'city', var: $city);
|
|
$statement->bindParam(param: 'phone', var: $phone);
|
|
$statement->execute();
|
|
|
|
return $statement->rowCount();
|
|
} catch (PDOException $e) {
|
|
echo $e->getMessage();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
public function delete(AddressBookEntry $addressBookEntry): int
|
|
{
|
|
$sql = "
|
|
DELETE FROM " . DatabaseConnection::TABLE_ADDRESSES . "
|
|
WHERE id = :id";
|
|
|
|
try {
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
$id = $addressBookEntry->getId();
|
|
$statement->bindParam(param: 'id', var: $id);
|
|
$statement->execute();
|
|
|
|
return $statement->rowCount();
|
|
} catch (PDOException $e) {
|
|
exit($e->getMessage());
|
|
}
|
|
}
|
|
|
|
} |