From be92fda17b879182204e89fdd7cdfdc390c67168 Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Mon, 16 Dec 2024 22:03:25 -0500 Subject: [PATCH] Update code to handle actually creating the channel. --- .../Channel/CreateChannelAction.php | 8 ++-- app/src/Controller/DashboardPage.php | 16 ++++++- .../Repository/Channel/ChannelRepository.php | 45 +++++++++---------- .../Domain/Repository/User/UserRepository.php | 2 +- app/templates/channel/create.twig | 2 +- 5 files changed, 43 insertions(+), 30 deletions(-) diff --git a/app/src/Controller/Channel/CreateChannelAction.php b/app/src/Controller/Channel/CreateChannelAction.php index 8f02f40..eae7660 100644 --- a/app/src/Controller/Channel/CreateChannelAction.php +++ b/app/src/Controller/Channel/CreateChannelAction.php @@ -12,6 +12,8 @@ use Slovocast\Domain\Repository\User\UserRepositoryInterface; class CreateChannelAction extends Controller { + const CREATE_TEMPLATE_PATH = 'channel/create.twig'; + public function __construct( protected SessionInterface $session, protected LoggerInterface $logger, @@ -24,10 +26,10 @@ class CreateChannelAction extends Controller $userData = $this->session->get('user'); $formData = $this->request->getParsedBody(); - if ($userData['id'] !== $formData['user_id']) { + if ($userData['id'] != $formData['user_id']) { $this->session->getFlash()->add("error", "Unable to create channel."); $this->logger->error("Session User ID and Form User ID do no match."); - return $this->render('channel/create.twig')->withStatus(400); + return $this->render(self::CREATE_TEMPLATE_PATH)->withStatus(400); } $formData['explicit'] = $formData['explicit'] === "yes" ? true : false; @@ -44,7 +46,7 @@ class CreateChannelAction extends Controller $this->logger->error($e->getMessage()); $this->session->getFlash()->add("error", "Unable to create new channel."); - return $this->render('channe/create.twig')->withStatus(500); + return $this->render(self::CREATE_TEMPLATE_PATH)->withStatus(500); } } } diff --git a/app/src/Controller/DashboardPage.php b/app/src/Controller/DashboardPage.php index c802d1b..b7f1716 100644 --- a/app/src/Controller/DashboardPage.php +++ b/app/src/Controller/DashboardPage.php @@ -3,23 +3,35 @@ namespace Slovocast\Controller; use Odan\Session\SessionInterface; +use Psr\Log\LoggerInterface; use Slovocast\Domain\Repository\Channel\ChannelRepositoryInterface; use Slovocast\Domain\Repository\User\UserRepositoryInterface; use Psr\Http\Message\ResponseInterface as Response; +use Slovocast\Exception\EntityNotFoundException; class DashboardPage extends Controller { public function __construct( protected UserRepositoryInterface $userRepository, protected ChannelRepositoryInterface $channelRepository, - protected SessionInterface $session + protected SessionInterface $session, + protected LoggerInterface $logger, ) { } public function handle(): Response { $userData = $this->session->get("user"); $user = $this->userRepository->get($userData['id']); - $channel = $this->channelRepository->getFromUser($user); + + try { + $channel = $this->channelRepository->getFromUser($user); + } catch (EntityNotFoundException $e) { + $this->logger->info($e->getMessage(), [ + 'user' => $user->getEmail(), + ]); + $channel = null; + } + return $this->render('dashboard.twig', [ 'channel' => $channel ]); } } diff --git a/app/src/Domain/Repository/Channel/ChannelRepository.php b/app/src/Domain/Repository/Channel/ChannelRepository.php index 3e56378..65a840f 100644 --- a/app/src/Domain/Repository/Channel/ChannelRepository.php +++ b/app/src/Domain/Repository/Channel/ChannelRepository.php @@ -35,7 +35,7 @@ class ChannelRepository implements ChannelRepositoryInterface 'copyright' => $results['copyright'], 'explicit' => $results['explicit'], 'createdAt' => new DateTimeImmutable($results['created_at']), - 'updatedAt' => new DateTime($results['updated_at']) + 'updatedAt' => new DateTime($results['updated_at'] ?? "now") ]); } @@ -56,7 +56,7 @@ class ChannelRepository implements ChannelRepositoryInterface throw new EntityNotFoundException("Unable to find Channel"); } - return $this->createChannelFromResults($results); + return $this->createChannelFromResults(array_shift($results)); } /** @@ -65,16 +65,14 @@ class ChannelRepository implements ChannelRepositoryInterface */ public function getFromUser(User $user): Channel { - $query = "SELECT * FROM channels WHERE owner_id = ?"; - $statement = this->db->getConnection()->prepare($query); - $statement->execute([ ':id' => $user->getId() ]); - $results = $statement->fetch(\PDO::FETCH_ASSOC); + $query = "SELECT * FROM channels WHERE owner_id = :id"; + $results = $this->db->query($query, [ ':id' => $user->getId() ]); if (!is_array($results) || !count($results)) { throw new EntityNotFoundException("Unable to find Channel"); } - return $this->createChannelFromResults($results); + return $this->createChannelFromResults(array_shift($results)); } /** @@ -92,7 +90,7 @@ class ChannelRepository implements ChannelRepositoryInterface throw new EntityNotFoundException("Unable to find Channel"); } - return $this->createChannelFromResults($results); + return $this->createChannelFromResults(array_shift($results)); } /** @@ -101,27 +99,28 @@ class ChannelRepository implements ChannelRepositoryInterface public function create(Channel $channel, User $owner): Channel { $query = " - INSERT INTO channels (name, slug, description, link, - language, copyright, explicit, owner_id) - VALUES (?, ?, ?, ?, ?, ?, ?, ?) + INSERT INTO channels (name, slug, description, link, language, + copyright, explicit, owner_id) + VALUES (:name, :slug, :description, :link, :language, :copyright, + :explicit, :owner_id) "; - $statement = $this->db->getConnection()->prepare($query); - $statement->execute([ - $channel->getName(), - $channel->getSlug(), - $channel->getDescription(), - $channel->getLink(), - $channel->getLanguage(), - $channel->getCopyright(), - $channel->isExplicit(), - $owner->getId() + $results = $this->db->query($query, [ + ':name' => $channel->getName(), + ':slug' => $channel->getSlug(), + ':description' => $channel->getDescription(), + ':link' => $channel->getLink(), + ':language' => $channel->getLanguage(), + ':copyright' => $channel->getCopyright(), + ':explicit' => (int) $channel->isExplicit(), + ':owner_id' => $owner->getId() ]); $insertId = $this->db->getConnection()->lastInsertId(); $channel->setId($insertId); - $channel->setCreatedAt(new DateTimeImmutable()); - $channel->setUpdatedAt(new DateTime()); + $channel->setCreatedAt(new DateTimeImmutable("now")); + $channel->setUpdatedAt(new DateTime("now")); + return $channel; } diff --git a/app/src/Domain/Repository/User/UserRepository.php b/app/src/Domain/Repository/User/UserRepository.php index 7de4056..6d3e560 100644 --- a/app/src/Domain/Repository/User/UserRepository.php +++ b/app/src/Domain/Repository/User/UserRepository.php @@ -58,7 +58,7 @@ class UserRepository implements UserRepositoryInterface throw new EntityNotFoundException("Unable to find User"); } - return $this->userFromQueryResults($results); + return $this->userFromQueryResults(array_shift($results)); } /** diff --git a/app/templates/channel/create.twig b/app/templates/channel/create.twig index c82177a..e2a6d02 100644 --- a/app/templates/channel/create.twig +++ b/app/templates/channel/create.twig @@ -27,7 +27,7 @@