Update code to handle actually creating the channel.

This commit is contained in:
Dave Smith-Hayes 2024-12-16 22:03:25 -05:00
parent b8034b6a0a
commit be92fda17b
5 changed files with 43 additions and 30 deletions

View File

@ -12,6 +12,8 @@ use Slovocast\Domain\Repository\User\UserRepositoryInterface;
class CreateChannelAction extends Controller class CreateChannelAction extends Controller
{ {
const CREATE_TEMPLATE_PATH = 'channel/create.twig';
public function __construct( public function __construct(
protected SessionInterface $session, protected SessionInterface $session,
protected LoggerInterface $logger, protected LoggerInterface $logger,
@ -24,10 +26,10 @@ class CreateChannelAction extends Controller
$userData = $this->session->get('user'); $userData = $this->session->get('user');
$formData = $this->request->getParsedBody(); $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->session->getFlash()->add("error", "Unable to create channel.");
$this->logger->error("Session User ID and Form User ID do no match."); $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; $formData['explicit'] = $formData['explicit'] === "yes" ? true : false;
@ -44,7 +46,7 @@ class CreateChannelAction extends Controller
$this->logger->error($e->getMessage()); $this->logger->error($e->getMessage());
$this->session->getFlash()->add("error", "Unable to create new channel."); $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);
} }
} }
} }

View File

@ -3,23 +3,35 @@
namespace Slovocast\Controller; namespace Slovocast\Controller;
use Odan\Session\SessionInterface; use Odan\Session\SessionInterface;
use Psr\Log\LoggerInterface;
use Slovocast\Domain\Repository\Channel\ChannelRepositoryInterface; use Slovocast\Domain\Repository\Channel\ChannelRepositoryInterface;
use Slovocast\Domain\Repository\User\UserRepositoryInterface; use Slovocast\Domain\Repository\User\UserRepositoryInterface;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Slovocast\Exception\EntityNotFoundException;
class DashboardPage extends Controller class DashboardPage extends Controller
{ {
public function __construct( public function __construct(
protected UserRepositoryInterface $userRepository, protected UserRepositoryInterface $userRepository,
protected ChannelRepositoryInterface $channelRepository, protected ChannelRepositoryInterface $channelRepository,
protected SessionInterface $session protected SessionInterface $session,
protected LoggerInterface $logger,
) { } ) { }
public function handle(): Response public function handle(): Response
{ {
$userData = $this->session->get("user"); $userData = $this->session->get("user");
$user = $this->userRepository->get($userData['id']); $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 ]); return $this->render('dashboard.twig', [ 'channel' => $channel ]);
} }
} }

View File

@ -35,7 +35,7 @@ class ChannelRepository implements ChannelRepositoryInterface
'copyright' => $results['copyright'], 'copyright' => $results['copyright'],
'explicit' => $results['explicit'], 'explicit' => $results['explicit'],
'createdAt' => new DateTimeImmutable($results['created_at']), '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"); 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 public function getFromUser(User $user): Channel
{ {
$query = "SELECT * FROM channels WHERE owner_id = ?"; $query = "SELECT * FROM channels WHERE owner_id = :id";
$statement = this->db->getConnection()->prepare($query); $results = $this->db->query($query, [ ':id' => $user->getId() ]);
$statement->execute([ ':id' => $user->getId() ]);
$results = $statement->fetch(\PDO::FETCH_ASSOC);
if (!is_array($results) || !count($results)) { if (!is_array($results) || !count($results)) {
throw new EntityNotFoundException("Unable to find Channel"); 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"); 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 public function create(Channel $channel, User $owner): Channel
{ {
$query = " $query = "
INSERT INTO channels (name, slug, description, link, INSERT INTO channels (name, slug, description, link, language,
language, copyright, explicit, owner_id) copyright, explicit, owner_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?) VALUES (:name, :slug, :description, :link, :language, :copyright,
:explicit, :owner_id)
"; ";
$statement = $this->db->getConnection()->prepare($query); $results = $this->db->query($query, [
$statement->execute([ ':name' => $channel->getName(),
$channel->getName(), ':slug' => $channel->getSlug(),
$channel->getSlug(), ':description' => $channel->getDescription(),
$channel->getDescription(), ':link' => $channel->getLink(),
$channel->getLink(), ':language' => $channel->getLanguage(),
$channel->getLanguage(), ':copyright' => $channel->getCopyright(),
$channel->getCopyright(), ':explicit' => (int) $channel->isExplicit(),
$channel->isExplicit(), ':owner_id' => $owner->getId()
$owner->getId()
]); ]);
$insertId = $this->db->getConnection()->lastInsertId(); $insertId = $this->db->getConnection()->lastInsertId();
$channel->setId($insertId); $channel->setId($insertId);
$channel->setCreatedAt(new DateTimeImmutable()); $channel->setCreatedAt(new DateTimeImmutable("now"));
$channel->setUpdatedAt(new DateTime()); $channel->setUpdatedAt(new DateTime("now"));
return $channel; return $channel;
} }

View File

@ -58,7 +58,7 @@ class UserRepository implements UserRepositoryInterface
throw new EntityNotFoundException("Unable to find User"); throw new EntityNotFoundException("Unable to find User");
} }
return $this->userFromQueryResults($results); return $this->userFromQueryResults(array_shift($results));
} }
/** /**

View File

@ -27,7 +27,7 @@
<div> <div>
<label for="explicit">Explicit<br> <label for="explicit">Explicit<br>
<select> <select name="explicit">
<option value="yes">Yes</option> <option value="yes">Yes</option>
<option value="no" selected>No</option> <option value="no" selected>No</option>
</select> </select>