diff --git a/.env b/.env index 7171b18..8977b9e 100644 --- a/.env +++ b/.env @@ -28,3 +28,6 @@ APP_SECRET=this_is_secret # DATABASE_URL="mysql://app:!ChangeMe!@127.0.0.1:3306/app?serverVersion=10.11.2-MariaDB&charset=utf8mb4" DATABASE_URL="postgresql://app:!ChangeMe!@127.0.0.1:5432/app?serverVersion=16&charset=utf8" ###< doctrine/doctrine-bundle ### + +DEFAULT_USERNAME=admin +DEFAULT_PASSWORD=admin diff --git a/config/services.yaml b/config/services.yaml index 45eb517..cb60ac3 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -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 diff --git a/src/Controller/BlogPostController.php b/src/Controller/BlogPostController.php index d05b1df..4e30414 100644 --- a/src/Controller/BlogPostController.php +++ b/src/Controller/BlogPostController.php @@ -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( diff --git a/src/Entity/BlogPost.php b/src/Entity/BlogPost.php index 0c2e1d4..3307f76 100644 --- a/src/Entity/BlogPost.php +++ b/src/Entity/BlogPost.php @@ -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; diff --git a/src/Factory/BlogPostFactory.php b/src/Factory/BlogPostFactory.php index b653d25..24c3927 100644 --- a/src/Factory/BlogPostFactory.php +++ b/src/Factory/BlogPostFactory.php @@ -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'), ]; } diff --git a/src/Factory/UserFactory.php b/src/Factory/UserFactory.php index f6581ed..c56fa56 100644 --- a/src/Factory/UserFactory.php +++ b/src/Factory/UserFactory.php @@ -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; @@ -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(); } @@ -49,8 +53,8 @@ public function __construct(private UserPasswordHasherInterface $passwordHasher) protected function getDefaults(): array { return [ - 'email' => 'test@mail.com', - 'password' => '1234', + 'email' => $this->defaultUsername, + 'password' => $this->defaultPassword, 'roles' => [], ]; } diff --git a/src/Form/BlogPostType.php b/src/Form/BlogPostType.php index 135e8bd..25aae83 100644 --- a/src/Form/BlogPostType.php +++ b/src/Form/BlogPostType.php @@ -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; @@ -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 diff --git a/src/Repository/BlogPostRepository.php b/src/Repository/BlogPostRepository.php index f8ee31e..6aa78b3 100644 --- a/src/Repository/BlogPostRepository.php +++ b/src/Repository/BlogPostRepository.php @@ -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'); } diff --git a/templates/blog_post/_form.html.twig b/templates/blog_post/_form.html.twig index 463834f..1f5a501 100644 --- a/templates/blog_post/_form.html.twig +++ b/templates/blog_post/_form.html.twig @@ -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'}}) }} +{# add published at field#} +
{{ blog_post.summary|purify }}
diff --git a/templates/blog_post/new.html.twig b/templates/blog_post/new.html.twig index d75a394..38c630f 100644 --- a/templates/blog_post/new.html.twig +++ b/templates/blog_post/new.html.twig @@ -12,6 +12,7 @@ {% block javascripts %} {{ parent() }} + {% endblock %} + +{% block stylesheets %} + {{ parent() }} + +{% endblock %} +