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

View File

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

View File

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

View File

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

View File

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