Skip to content

Commit

Permalink
Add QOTD context
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx committed Jan 4, 2024
1 parent 512bb2b commit 800f051
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 15 deletions.
31 changes: 31 additions & 0 deletions migrations/Version20240103154351.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20240103154351 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add context to QOTD';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE qotd ADD context TEXT');
$this->addSql('UPDATE qotd SET context = \'\'');
$this->addSql('ALTER TABLE qotd ALTER context SET NOT NULL');
$this->addSql('ALTER TABLE qotd ADD updated_at TIMESTAMP(0) WITHOUT TIME ZONE');
$this->addSql('COMMENT ON COLUMN qotd.updated_at IS \'(DC2Type:datetime_immutable)\'');
$this->addSql('UPDATE qotd SET updated_at = NOW()');
$this->addSql('ALTER TABLE qotd ALTER updated_at SET NOT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE qotd DROP context');
$this->addSql('ALTER TABLE qotd DROP updated_at');
}
}
17 changes: 4 additions & 13 deletions src/Controller/QotdController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Controller;

use App\Entity\Qotd;
use App\Form\Type\QotdType;
use App\Repository\Model\QotdDirection;
use App\Repository\Model\QotdVote;
use App\Repository\QotdRepository;
Expand All @@ -15,8 +16,6 @@
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Attribute\CurrentUser;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;

class QotdController extends AbstractController
{
Expand Down Expand Up @@ -87,17 +86,9 @@ public function edit(
Request $request,
#[MapEntity()] Qotd $qotd,
): Response {
$form = $this->createFormBuilder(
$qotd,
[
'action' => $this->generateUrl('qotd_show_edit', ['id' => $qotd->id]),
])
->add('message', null, [
'constraints' => [new NotBlank(), new Length(min: 10)],
'required' => false,
])
->getForm()
;
$form = $this->createForm(QotdType::class, $qotd, [
'action' => $this->generateUrl('qotd_show_edit', ['id' => $qotd->id]),
]);

if ($form->handleRequest($request)->isSubmitted() && $form->isValid()) {
$this->em->flush();
Expand Down
22 changes: 22 additions & 0 deletions src/Entity/Qotd.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
use App\Repository\QotdRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;

#[ORM\Entity(repositoryClass: QotdRepository::class)]
#[HasLifecycleCallbacks]
class Qotd
{
#[Groups(['qotd:read'])]
Expand All @@ -29,6 +32,13 @@ class Qotd
#[ORM\Column(type: Types::JSON, options: ['jsonb' => true])]
public array $images = [];

#[Assert\Length(min: 5)]
#[ORM\Column(type: Types::TEXT)]
public string $context = '';

#[ORM\Column()]
public \DateTimeImmutable $updatedAt;

public function __construct(
#[Groups(['qotd:read'])]
#[ORM\Column(type: Types::DATE_IMMUTABLE)]
Expand All @@ -47,6 +57,7 @@ public function __construct(
public readonly string $username,
) {
$this->id = uuid_create();
$this->updatedAt = new \DateTimeImmutable();
}

public function applyVote(QotdVote $vote, UserInterface $user): void
Expand Down Expand Up @@ -86,4 +97,15 @@ public function getImageUrls(): array
{
return array_map(fn (string $image) => sprintf('uploads/%s---%s', $this->id, $image), $this->images);
}

#[ORM\PreUpdate]
public function onUpdate(): void
{
$this->updatedAt = new \DateTimeImmutable();
}

public function getContext(): string
{
return $this->context;
}
}
22 changes: 22 additions & 0 deletions src/Form/Extension/TextTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Form\Extension;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class TextTypeExtension extends AbstractTypeExtension
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'empty_data' => '',
]);
}

public static function getExtendedTypes(): iterable
{
yield TextType::class;
}
}
19 changes: 19 additions & 0 deletions src/Form/Type/QotdType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;

class QotdType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('context', TextareaType::class, [
'required' => false,
])
;
}
}
7 changes: 6 additions & 1 deletion templates/macros/qotd/message.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{% macro message(qotd) %}
{% cache "qotd-message-#{qotd.message}" %}
{% cache "qotd-message-#{qotd.id}-#{qotd.updatedAt.format('c')}" %}
{# we use |replace because it better like that! #}
{% if qotd.context %}
<blockquote class="text-body-secondary">
{{ qotd.context|replace_emoji|replace_username|replace({'\n': '\n\n'})|markdown_to_html }}
</blockquote>
{% endif %}
{{ qotd.message|replace_emoji|replace_username|replace({'\n': '\n\n'})|markdown_to_html }}
{% endcache %}
{% endmacro %}
8 changes: 8 additions & 0 deletions templates/qotd/show_details.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
</div>
</td>
</tr>
<tr>
<th>Raw Context</th>
<td>
<div class="overflow-x-scroll" style="max-width:calc(var(--bs-modal-width) - 10em)">
<pre><code>{{ qotd.context|nl2br }}</code></pre>
</div>
</td>
</tr>
<tr>
<th>Message</th>
<td>{{ qotd_macro.message(qotd) }}</td>
Expand Down
2 changes: 1 addition & 1 deletion tests/Controller/QotdControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function testNotVotedYet(): void
self::assertCount(20, $quotes);

$firstQuote = $quotes->first();
$id = str_replace('qotd-default-', '', $firstQuote->attr('id'));
$id = str_replace('qotd-', '', $firstQuote->attr('id'));

try {
$form = $firstQuote->filter('[data-test=vote-up]')->form();
Expand Down

0 comments on commit 800f051

Please sign in to comment.