Compare commits
2 Commits
72503d7fe2
...
4665e1706f
Author | SHA1 | Date |
---|---|---|
tracer | 4665e1706f | |
tracer | 8189abfd29 |
|
@ -12,14 +12,14 @@ namespace App\Entity;
|
||||||
class AddressBookEntry
|
class AddressBookEntry
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private int $owner,
|
private int $owner,
|
||||||
private string $first,
|
private string $first,
|
||||||
private string $last,
|
private string $last,
|
||||||
private string $street,
|
private string $street,
|
||||||
private string $zip,
|
private string $zip,
|
||||||
private string $city,
|
private string $city,
|
||||||
private string $phone,
|
private string $phone,
|
||||||
private int $userid = 0,
|
private int $id = 0,
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
// empty body
|
// empty body
|
||||||
|
@ -75,14 +75,14 @@ class AddressBookEntry
|
||||||
$this->phone = $phone;
|
$this->phone = $phone;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getUserid(): int
|
public function getId(): int
|
||||||
{
|
{
|
||||||
return $this->userid;
|
return $this->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setUserid(int $userid): void
|
public function setId(int $id): void
|
||||||
{
|
{
|
||||||
$this->userid = $userid;
|
$this->id = $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getFirst(): string
|
public function getFirst(): string
|
||||||
|
|
|
@ -0,0 +1,170 @@
|
||||||
|
<?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 App\Entity\User;
|
||||||
|
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
|
||||||
|
{
|
||||||
|
$users = [];
|
||||||
|
$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: $result['owner'], first: $result['first'], last: $result['last'], street: $result['street'], zip: $result['zip'], city: $result['city'], phone: $result['phone'], id: $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_USERS . "
|
||||||
|
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: $result['owner'], first: $result['first'], last: $result['last'], street: $result['street'], zip: $result['zip'], city: $result['city'], phone: $result['phone'], id: $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(Address $address): bool|int
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
$id = $user->getId();
|
||||||
|
$nick = $user->getNick();
|
||||||
|
$first = $user->getFirst();
|
||||||
|
$last = $user->getLast();
|
||||||
|
$isAdmin = $user->isAdmin() ? 1 : 0;
|
||||||
|
|
||||||
|
if ($user->getPassword()) {
|
||||||
|
$password = $user->getPassword();
|
||||||
|
} else {
|
||||||
|
$current = $this->findByID(id: $id);
|
||||||
|
$password = $current->getPassword();
|
||||||
|
}
|
||||||
|
|
||||||
|
$sql = "
|
||||||
|
UPDATE " . DatabaseConnection::TABLE_USERS . " SET
|
||||||
|
nick = :nick,
|
||||||
|
password = :password,
|
||||||
|
first = :first,
|
||||||
|
last = :last,
|
||||||
|
is_admin = :is_admin
|
||||||
|
WHERE id = :id";
|
||||||
|
|
||||||
|
try {
|
||||||
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||||
|
$statement->bindParam(param: 'id', var: $id);
|
||||||
|
$statement->bindParam(param: 'nick', var: $nick);
|
||||||
|
$statement->bindParam(param: 'password', var: $password);
|
||||||
|
$statement->bindParam(param: 'first', var: $first);
|
||||||
|
$statement->bindParam(param: 'last', var: $last);
|
||||||
|
$statement->bindParam(param: 'is_admin', var: $isAdmin);
|
||||||
|
$statement->execute();
|
||||||
|
|
||||||
|
return $statement->rowCount();
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
echo $e->getMessage();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue