From 744117d958d83230f20137f08501af7d35f26c78 Mon Sep 17 00:00:00 2001 From: tracer Date: Tue, 25 Oct 2022 15:52:46 +0200 Subject: [PATCH] added CRUD implementation --- src/Controller/AddressBookController.php | 66 ++++++++++++++++++++---- 1 file changed, 57 insertions(+), 9 deletions(-) diff --git a/src/Controller/AddressBookController.php b/src/Controller/AddressBookController.php index d3a6c6c..a88ed69 100644 --- a/src/Controller/AddressBookController.php +++ b/src/Controller/AddressBookController.php @@ -10,30 +10,78 @@ namespace App\Controller; use App\Entity\User; +use App\Entity\AddressBookEntry; +use App\Enums\UserAuth; use App\Service\Router; use App\Service\Template; -use App\Repository\UserRepository; -use JetBrains\PhpStorm\NoReturn; +use App\Repository\AddressRepository; class AddressBookController { public function __construct( - private readonly Template $template, - private readonly User $user, - private readonly UserRepository $userRepository, - private readonly Router $router + private readonly Template $template, + private readonly User $user, + private readonly AddressRepository $addressRepository, + private readonly Router $router ) { // empty body } - #[NoReturn] - public function main(): void + 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, + '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); + + $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']); + $this->addressRepository->update(address: $address); + } + + public function deleteAddress(): void + { + echo "in del"; + $_POST = json_decode(json: file_get_contents(filename: "php://input"), associative: true); + if ($address = $this->addressRepository->findByID(id: $_POST['id'])) { + $this->addressRepository->delete(addressBookEntry: $address); + } + } + } \ No newline at end of file