Skip to content

Commit

Permalink
Add Add published at
Browse files Browse the repository at this point in the history
  • Loading branch information
AchillesKal committed Apr 7, 2024
1 parent 9d629bd commit 6802389
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ APP_SECRET=this_is_secret
# DATABASE_URL="mysql://app:[email protected]:3306/app?serverVersion=10.11.2-MariaDB&charset=utf8mb4"
DATABASE_URL="postgresql://app:[email protected]:5432/app?serverVersion=16&charset=utf8"
###< doctrine/doctrine-bundle ###

DEFAULT_USERNAME=admin
DEFAULT_PASSWORD=admin
2 changes: 2 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:
banner_directory: '%kernel.project_dir%/public/uploads/banners'
default_username: '%env(DEFAULT_USERNAME)%'
default_password: '%env(DEFAULT_PASSWORD)%'

services:
# default configuration for services in *this* file
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/BlogPostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class BlogPostController extends AbstractController
#[Route('', name: 'app_blog_post_index', methods: ['GET'])]
public function index(BlogPostRepository $blogPostRepository, Request $request): Response
{
$queryBuilder = $blogPostRepository->createOrderVyCreatedAtQueryBuilder();
$queryBuilder = $blogPostRepository->createOrderByPublishedAtQueryBuilder();

$adapter = new QueryAdapter($queryBuilder);
$pagerfanta = Pagerfanta::createForCurrentPageWithMaxPerPage(
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/BlogPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function getPublishedAt(): ?\DateTimeImmutable
return $this->publishedAt;
}

public function setPublishedAt(\DateTimeImmutable $publishedAt): static
public function setPublishedAt(?\DateTimeImmutable $publishedAt): static
{
$this->publishedAt = $publishedAt;

Expand Down
1 change: 1 addition & 0 deletions src/Factory/BlogPostFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ protected function getDefaults(): array
'summary' => self::faker()->text,
'content' => self::faker()->randomHtml(3, 6),
'banner' => $file->getFilename(),
'publishedAt' => self::faker()->dateTimeBetween('-3 month'),
];
}

Expand Down
12 changes: 8 additions & 4 deletions src/Factory/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Entity\User;
use App\Repository\UserRepository;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\PasswordHasher\PasswordHasherInterface;
use Zenstruck\Foundry\ModelFactory;
Expand Down Expand Up @@ -36,8 +37,11 @@ final class UserFactory extends ModelFactory
*
* @todo inject services if required
*/
public function __construct(private UserPasswordHasherInterface $passwordHasher)
{
public function __construct(
private UserPasswordHasherInterface $passwordHasher,
#[Autowire('%env(DEFAULT_USERNAME)%')] private readonly string $defaultUsername,
#[Autowire('%env(DEFAULT_PASSWORD)%')] private readonly string $defaultPassword,
) {
parent::__construct();
}

Expand All @@ -49,8 +53,8 @@ public function __construct(private UserPasswordHasherInterface $passwordHasher)
protected function getDefaults(): array
{
return [
'email' => '[email protected]',
'password' => '1234',
'email' => $this->defaultUsername,
'password' => $this->defaultPassword,
'roles' => [],
];
}
Expand Down
10 changes: 10 additions & 0 deletions src/Form/BlogPostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand Down Expand Up @@ -53,6 +54,15 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'autocomplete' => true,
'required' => false,
])
->add('publishedAt', DateTimeType::class,[
'required' => false,
'widget' => 'single_text',
'html5' => false,
'format' => 'yyyy-MM-dd HH:mm',
'attr' => [
'class' => 'app-datepicker',
],
])
;

// Conditionally add the field only for the edit form
Expand Down
5 changes: 3 additions & 2 deletions src/Repository/BlogPostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ public function __construct(ManagerRegistry $registry)
parent::__construct($registry, BlogPost::class);
}

public function createOrderVyCreatedAtQueryBuilder(): QueryBuilder
public function createOrderByPublishedAtQueryBuilder(): QueryBuilder
{
return $this->createQueryBuilder('bp')
->orderBy('bp.createdAt', 'DESC');
->andWhere('bp.publishedAt IS NOT NULL')
->orderBy('bp.publishedAt', 'DESC');
}


Expand Down
6 changes: 6 additions & 0 deletions templates/blog_post/_form.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
{{ form_widget(form.title, {'attr': {'class': 'shadow-inner w-full bg-gray-100 border hover:border-gray-900 transition-all text-gray-900 mt-2 p-4 rounded-xl focus:outline-none focus:shadow-outline', 'placeholder': 'Add title'}}) }}
</div>

{# add published at field#}
<div>
<label for="blog_post_publishedAt" class="text-sm font-medium">Published At</label>
{{ form_widget(form.publishedAt, {'attr': {'class': 'shadow-inner w-full bg-gray-100 hover:border-gray-900 transition-all border text-gray-900 mt-2 p-4 rounded-xl focus:outline-none focus:shadow-outline', 'placeholder': 'Add published at'}}) }}
</div>

<div>
<label for="blog_post_tags" class="text-sm font-medium">Tags</label>
{{ form_widget(form.tags, {'attr': {'class': 'shadow-inner w-full bg-gray-100 hover:border-gray-900 transition-all border text-gray-900 mt-2 p-4 rounded-xl focus:outline-none focus:shadow-outline', 'multiple': 'multiple'}}) }}
Expand Down
13 changes: 12 additions & 1 deletion templates/blog_post/edit.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{% block javascripts %}
{{ parent() }}
<script src="https://cdn.ckeditor.com/ckeditor5/41.2.1/classic/ckeditor.js"></script>

<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
Expand All @@ -30,7 +30,18 @@
.catch( error => {
console.error( error );
} );
flatpickr("#blog_post_publishedAt", {
enableTime: true,
dateFormat: "Y-m-d H:i",
time_24hr: true,
});
});
</script>
{% endblock %}

{% block stylesheets %}
{{ parent() }}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
{% endblock %}

3 changes: 3 additions & 0 deletions templates/blog_post/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
<a href="{{ path('app_blog_post_show', {'slug': blog_post.slug}) }}">
<h2 class="text-balance @xl:leading-tight text-2xl font-bold @xl:text-5xl text-black">{{ blog_post.title }}</h2>
</a>
<div>
<span class="text-gray-500">{{ blog_post.publishedAt|date('F j, Y H:i') }}</span>
</div>
<p class="@xl:text-2xl text-md font-light text-black max-w-4xl line-clamp-3 @xl:leading-9 leading-relaxed">
{{ blog_post.summary|purify }}
</p>
Expand Down
13 changes: 13 additions & 0 deletions templates/blog_post/new.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
{% block javascripts %}
{{ parent() }}
<script src="https://cdn.ckeditor.com/ckeditor5/41.2.1/classic/ckeditor.js"></script>
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
ClassicEditor
Expand All @@ -24,6 +25,18 @@
.catch(error => {
console.error(error);
});
flatpickr("#blog_post_publishedAt", {
enableTime: true,
dateFormat: "Y-m-d H:i",
time_24hr: true,
});
});
</script>
{% endblock %}

{% block stylesheets %}
{{ parent() }}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
{% endblock %}

0 comments on commit 6802389

Please sign in to comment.