added CRUD implementation

This commit is contained in:
tracer 2022-10-25 15:52:46 +02:00
parent cce18f6516
commit 744117d958

View File

@ -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);
}
}
}