Compare commits
No commits in common. "4665e1706f5dc1037a8ff27cae43f6bbe9c19b15" and "72503d7fe25ed211e73b32f55b2aee6fb2d94009" have entirely different histories.
4665e1706f
...
72503d7fe2
|
@ -19,7 +19,7 @@ class AddressBookEntry
|
|||
private string $zip,
|
||||
private string $city,
|
||||
private string $phone,
|
||||
private int $id = 0,
|
||||
private int $userid = 0,
|
||||
)
|
||||
{
|
||||
// empty body
|
||||
|
@ -75,14 +75,14 @@ class AddressBookEntry
|
|||
$this->phone = $phone;
|
||||
}
|
||||
|
||||
public function getId(): int
|
||||
public function getUserid(): int
|
||||
{
|
||||
return $this->id;
|
||||
return $this->userid;
|
||||
}
|
||||
|
||||
public function setId(int $id): void
|
||||
public function setUserid(int $userid): void
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->userid = $userid;
|
||||
}
|
||||
|
||||
public function getFirst(): string
|
||||
|
|
|
@ -1,170 +0,0 @@
|
|||
<?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