From 4665e1706f5dc1037a8ff27cae43f6bbe9c19b15 Mon Sep 17 00:00:00 2001
From: tracer <tracer@24unix.net>
Date: Tue, 25 Oct 2022 13:47:30 +0200
Subject: [PATCH] initial commit

---
 src/Repository/AddressRepository.php | 170 +++++++++++++++++++++++++++
 1 file changed, 170 insertions(+)
 create mode 100644 src/Repository/AddressRepository.php

diff --git a/src/Repository/AddressRepository.php b/src/Repository/AddressRepository.php
new file mode 100644
index 0000000..53582d3
--- /dev/null
+++ b/src/Repository/AddressRepository.php
@@ -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());
+        }
+    }
+
+}
\ No newline at end of file