133 lines
4.7 KiB
PHP
133 lines
4.7 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\Controller;
|
|
|
|
use App\Entity\User;
|
|
use App\Entity\AddressBookEntry;
|
|
use App\Enums\StatusCode;
|
|
use App\Enums\UserAuth;
|
|
use App\Service\Router;
|
|
use App\Service\Template;
|
|
use App\Repository\AddressRepository;
|
|
|
|
class AddressBookController
|
|
{
|
|
public function __construct(
|
|
private readonly Template $template,
|
|
private readonly User $user,
|
|
private readonly AddressRepository $addressRepository,
|
|
private readonly Router $router
|
|
)
|
|
{
|
|
// empty body
|
|
}
|
|
|
|
public function main(): never
|
|
{
|
|
if ($this->user->getAuth() != UserAuth::AUTH_ANONYMOUS) {
|
|
$addresses = $this->addressRepository->findAll();
|
|
}
|
|
|
|
$this->template->render(templateName: 'index.html.php', vars: [
|
|
'user' => $this->user,
|
|
'router' => $this->router,
|
|
'addresses' => $addresses ?? []
|
|
]);
|
|
}
|
|
|
|
public function addAddress(): never
|
|
{
|
|
if (!empty($_POST)) {
|
|
$address = new AddressBookEntry(owner: $_POST['owner'], first: $_POST['first'], last: $_POST['last'], street: $_POST['street'], zip: $_POST['zip'], city: $_POST['city'], phone: $_POST['phone']);
|
|
|
|
if ($this->addressRepository->insert(address: $address)) {
|
|
$addresses = $this->addressRepository->findAll();
|
|
|
|
$this->template->render(templateName: 'index.html.php', vars: [
|
|
'user' => $this->user,
|
|
'addresses' => $addresses,
|
|
'router' => $this->router
|
|
]);
|
|
} else {
|
|
die("Error inserting user");
|
|
}
|
|
|
|
}
|
|
|
|
$this->template->render(templateName: 'addressbook/add_address.html.php', vars: [
|
|
'user' => $this->user,
|
|
'router' => $this->router
|
|
]);
|
|
}
|
|
|
|
|
|
public function updateAddress(): void
|
|
{
|
|
$_POST = json_decode(json: file_get_contents(filename: "php://input"), associative: true);
|
|
|
|
if (empty($_POST)) {
|
|
$this->template->renderJson(results: [
|
|
'status' => 400,
|
|
'message' => 'BAD REQUEST'
|
|
]);
|
|
}
|
|
|
|
if ($address = new AddressBookEntry(owner: $_POST['owner'], first: $_POST['first'], last: $_POST['last'], street: $_POST['street'], zip: $_POST['zip'], city: $_POST['city'], phone: $_POST['phone'], id: $_POST['id'])) {
|
|
if ($this->addressRepository->update(address: $address)) {
|
|
$status = 200;
|
|
$message = 'OK';
|
|
} else {
|
|
$status = 400;
|
|
$message = 'BAD_REQUEST';
|
|
}
|
|
} else {
|
|
$status = 400;
|
|
$message = "BAD REQUEST";
|
|
}
|
|
|
|
$this->template->renderJson(results: [
|
|
'status' => $status,
|
|
'message' => $message
|
|
]);
|
|
}
|
|
|
|
public function deleteAddress(): void
|
|
{
|
|
$_POST = json_decode(json: file_get_contents(filename: "php://input"), associative: true);
|
|
|
|
if (empty($_POST)) {
|
|
$this->template->renderJson(results: [
|
|
'status' => 400,
|
|
'message' => 'BAD REQUEST'
|
|
]);
|
|
}
|
|
|
|
if ($address = $this->addressRepository->findByID(id: $_POST['id'])) {
|
|
if ($this->addressRepository->delete(addressBookEntry: $address)) {
|
|
$this->template->renderJson(results: [
|
|
'status' => 200,
|
|
'message' => 'OK'
|
|
]);
|
|
} else {
|
|
$this->template->renderJson(results: [
|
|
'status' => 400,
|
|
'message' => 'BAD REQUEST'
|
|
]);
|
|
|
|
}
|
|
} else {
|
|
$this->template->renderJson(results: [
|
|
'status' => 400,
|
|
'message' => 'BAD REQUEST'
|
|
]);
|
|
}
|
|
}
|
|
|
|
} |