UnderCurrent/app/Controller/HelloWorldController.php
2023-08-17 15:53:58 +02:00

75 lines
1.9 KiB
PHP

<?php
namespace App\Controller;
use App\Entity\Book;
use App\View\RouteView;
use Ardent\Undercurrent\Attribute\Route;
use Ardent\Undercurrent\Http\GenericResponse;
use Ardent\Undercurrent\Http\ResponseInterface;
use Ardent\Undercurrent\Http\RoutesConfig;
use Ardent\Undercurrent\Http\StatusEnum;
use Ardent\Undercurrent\View\BaseView;
use Ardent\Undercurrent\View\ViewInterface;
use Doctrine\ORM\EntityManager;
class HelloWorldController
{
#[Route('/')]
public function index(): ViewInterface
{
return new BaseView('/index');
}
#[Route('/error')]
public function error(): ResponseInterface
{
return new GenericResponse('error', StatusEnum::NOT_FOUND);
}
#[Route('/hello')]
public function hello(): ResponseInterface
{
return new GenericResponse('Hello World!');
}
#[Route('/view/{name}')]
public function view(string $name): ViewInterface
{
return new BaseView('/home', ['name' => $name]);
}
#[Route('/routes')]
public function routes(RoutesConfig $config): ViewInterface
{
return new RouteView($config);
}
#[Route('/world/{name}')]
public function world(string $name): ResponseInterface
{
return new GenericResponse("Hello $name!");
}
#[Route('/db')]
public function db(EntityManager $em): ResponseInterface
{
$repo = $em->getRepository(Book::class);
dump($repo->findAll());
return new GenericResponse("DB stuff");
}
#[Route('/create')]
public function createDb(EntityManager $em): ResponseInterface
{
$books = $em->getRepository(Book::class)->findAll();
$book = new Book();
$book->setTitle(sprintf('Book %d', count($books) + 1));
$em->persist($book);
$em->flush();
dump($book);
return new GenericResponse("DB stuff");
}
}