Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app/Entity/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace App\Entity;

use App\Enum\BookCategory;
use App\Grid\BookGrid;
use App\Repository\BookRepository;
use App\Responder\ExportGridToCsvResponder;
Expand Down Expand Up @@ -79,6 +80,9 @@ class Book implements ResourceInterface
#[NotBlank]
private ?string $authorName = null;

#[ORM\Column(type: 'enum', length: 255, nullable: true)]
private ?BookCategory $category = null;

#[ORM\Column(type: 'datetime_immutable')]
private \DateTimeImmutable $createdAt;

Expand Down Expand Up @@ -121,4 +125,14 @@ public function setCreatedAt(\DateTimeImmutable $createdAt): void
{
$this->createdAt = $createdAt;
}

public function getCategory(): ?BookCategory
{
return $this->category;
}

public function setCategory(?BookCategory $category): void
{
$this->category = $category;
}
}
42 changes: 42 additions & 0 deletions app/Enum/BookCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Enum;

enum BookCategory: string
{
case FICTION = 'fiction';
case NON_FICTION = 'non-fiction';
case MYSTERY = 'mystery';
case THRILLER = 'thriller';
case SCIENCE_FICTION = 'science-fiction';
case FANTASY = 'fantasy';
case ROMANCE = 'romance';
case HORROR = 'horror';
case BIOGRAPHY = 'biography';
case HISTORY = 'history';
case SCIENCE = 'science';
case TECHNOLOGY = 'technology';
case BUSINESS = 'business';
case COOKING = 'cooking';
case TRAVEL = 'travel';
case POETRY = 'poetry';
case DRAMA = 'drama';
case CHILDREN = 'children';
case YOUNG_ADULT = 'young adult';
case RELIGION = 'religion';
case PHILOSOPHY = 'philosophy';
case ART = 'art';
case MANGA = 'manga';
case COMICS = 'comics';
}
7 changes: 7 additions & 0 deletions app/Factory/BookFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace App\Factory;

use App\Entity\Book;
use App\Enum\BookCategory;
use Zenstruck\Foundry\Persistence\PersistentObjectFactory;

/**
Expand All @@ -36,11 +37,17 @@ public function withAuthorName(string $authorName): self
return $this->with(['authorName' => $authorName]);
}

public function withCategory(string $category): self
{
return $this->with(['category' => $category]);
}

protected function defaults(): array|callable
{
return [
'title' => ucfirst(self::faker()->words(3, true)),
'authorName' => self::faker()->firstName() . ' ' . self::faker()->lastName(),
'category' => self::faker()->randomElement(BookCategory::cases()),
];
}
}
13 changes: 12 additions & 1 deletion app/Grid/BookGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace App\Grid;

use App\Entity\Book;
use App\Enum\BookCategory;
use Sylius\Bundle\GridBundle\Builder\Action\Action;
use Sylius\Bundle\GridBundle\Builder\Action\CreateAction;
use Sylius\Bundle\GridBundle\Builder\Action\DeleteAction;
Expand All @@ -22,7 +23,9 @@
use Sylius\Bundle\GridBundle\Builder\ActionGroup\BulkActionGroup;
use Sylius\Bundle\GridBundle\Builder\ActionGroup\ItemActionGroup;
use Sylius\Bundle\GridBundle\Builder\ActionGroup\MainActionGroup;
use Sylius\Bundle\GridBundle\Builder\Field\EnumField;
use Sylius\Bundle\GridBundle\Builder\Field\StringField;
use Sylius\Bundle\GridBundle\Builder\Filter\EnumFilter;
use Sylius\Bundle\GridBundle\Builder\Filter\StringFilter;
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
Expand All @@ -39,9 +42,12 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void
{
$gridBuilder
->orderBy('title')
->addFilter(
->withFilters(
StringFilter::create('search', ['title', 'authorName'])
->setLabel('sylius.ui.search'),
EnumFilter::create(name: 'category', enumClass: BookCategory::class, field: 'category')
->addFormOption('choice_label', fn (BookCategory $choice) => $choice->value)
->setLabel('app.ui.category'),
)
->addField(
StringField::create('title')
Expand All @@ -53,6 +59,11 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void
->setLabel('app.ui.author_name')
->setSortable(true),
)
->addField(
EnumField::create('category')
->setLabel('app.ui.category')
->setSortable(true),
)
->addActionGroup(
MainActionGroup::create(
CreateAction::create(),
Expand Down
Loading
Loading