added CRUD implementation
This commit is contained in:
parent
cce18f6516
commit
744117d958
|
@ -10,30 +10,78 @@
|
||||||
namespace App\Controller;
|
namespace App\Controller;
|
||||||
|
|
||||||
use App\Entity\User;
|
use App\Entity\User;
|
||||||
|
use App\Entity\AddressBookEntry;
|
||||||
|
use App\Enums\UserAuth;
|
||||||
use App\Service\Router;
|
use App\Service\Router;
|
||||||
use App\Service\Template;
|
use App\Service\Template;
|
||||||
use App\Repository\UserRepository;
|
use App\Repository\AddressRepository;
|
||||||
use JetBrains\PhpStorm\NoReturn;
|
|
||||||
|
|
||||||
class AddressBookController
|
class AddressBookController
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly Template $template,
|
private readonly Template $template,
|
||||||
private readonly User $user,
|
private readonly User $user,
|
||||||
private readonly UserRepository $userRepository,
|
private readonly AddressRepository $addressRepository,
|
||||||
private readonly Router $router
|
private readonly Router $router
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
// empty body
|
// empty body
|
||||||
}
|
}
|
||||||
|
|
||||||
#[NoReturn]
|
public function main(): never
|
||||||
public function main(): void
|
|
||||||
{
|
{
|
||||||
|
if ($this->user->getAuth() != UserAuth::AUTH_ANONYMOUS) {
|
||||||
|
$addresses = $this->addressRepository->findAll();
|
||||||
|
}
|
||||||
|
|
||||||
$this->template->render(templateName: 'index.html.php', vars: [
|
$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
|
'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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue