Skip to content

Commit

Permalink
Use multiple authors instead of mainAuthor
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioMendolia committed Sep 6, 2023
1 parent 0897f57 commit d2d0d01
Show file tree
Hide file tree
Showing 26 changed files with 1,066 additions and 233 deletions.
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"require": {
"php": ">=8.1",
"ext-ctype": "*",
"ext-exif": "*",
"ext-iconv": "*",
"ext-zip": "*",
"doctrine/annotations": "^2.0",
Expand All @@ -22,6 +23,7 @@
"liip/imagine-bundle": "^2.10",
"phpdocumentor/reflection-docblock": "^5.3",
"phpstan/phpdoc-parser": "^1.18",
"scienta/doctrine-json-functions": "^5.3",
"sensio/framework-extra-bundle": "^6.1",
"symfony/apache-pack": "^1.0",
"symfony/asset": "^6.2",
Expand Down Expand Up @@ -57,8 +59,7 @@
"symfony/webpack-encore-bundle": "^2.0",
"symfony/yaml": "^6.2",
"twig/extra-bundle": "^2.12|^3.0",
"twig/twig": "^2.12|^3.0",
"ext-exif": "*"
"twig/twig": "^2.12|^3.0"
},
"config": {
"allow-plugins": {
Expand Down Expand Up @@ -128,6 +129,8 @@
}
},
"require-dev": {
"roave/security-advisories": "dev-latest"
,
"friendsofphp/php-cs-fixer": "^3.25",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-doctrine": "^1.3",
Expand Down
741 changes: 739 additions & 2 deletions composer.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions config/packages/doctrine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ doctrine:
prefix: Gedmo\Timestampable
dir: "%kernel.project_dir%/vendor/gedmo/doctrine-extensions/src/Timestampable"

dql:
string_functions:
JSON_CONTAINS: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonContains

when@test:
doctrine:
dbal:
Expand Down
41 changes: 41 additions & 0 deletions src/Command/MoveMainAuthorCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Command;

use App\Repository\BookRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(
name: 'app:move-main-author',
description: 'Add a short description for your command',
)]
class MoveMainAuthorCommand extends Command
{
public function __construct(private EntityManagerInterface $entityManager, private BookRepository $bookRepository)
{
parent::__construct();
}

protected function configure(): void
{
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

$books = $this->bookRepository->findAll();
foreach ($books as $book) {
$book->addAuthor($book->getMainAuthor());
}

$this->entityManager->flush();

return Command::SUCCESS;
}
}
60 changes: 60 additions & 0 deletions src/Command/RenameAuthorCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Command;

use App\Entity\Book;
use App\Repository\BookRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(
name: 'app:rename-author',
description: 'Add a short description for your command',
)]
class RenameAuthorCommand extends Command
{
public function __construct(private EntityManagerInterface $entityManager, private BookRepository $bookRepository)
{
parent::__construct();
}

protected function configure(): void
{
$this
->addArgument('author', InputArgument::REQUIRED, 'Author to rename')
->addArgument('newname', InputArgument::REQUIRED, 'new name')
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$toRename = $input->getArgument('author');
$newName = $input->getArgument('newname');

if (!is_string($toRename) || !is_string($newName)) {
throw new \Exception('Arguments must be strings');
}

/** @var Book[] $books */
$books = $this->bookRepository->getByAuthorQuery($toRename)->getResult();

foreach ($books as $book) {
$io->writeln('Renaming '.$toRename.' to '.$newName.' in '.$book->getTitle());

$book->removeAuthor($toRename);
if ($newName !== '') {
$book->addAuthor($newName);
}
}

$this->entityManager->flush();

return Command::SUCCESS;
}
}
56 changes: 56 additions & 0 deletions src/Command/RenameSerieCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\Command;

use App\Entity\Book;
use App\Repository\BookRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\String\Slugger\SluggerInterface;

#[AsCommand(
name: 'app:rename-serie',
description: 'Add a short description for your command',
)]
class RenameSerieCommand extends Command
{
public function __construct(private EntityManagerInterface $entityManager, private BookRepository $bookRepository, private SluggerInterface $slugger)
{
parent::__construct();
}

protected function configure(): void
{
$this
->addArgument('serie', InputArgument::REQUIRED, 'Serie to rename')
->addArgument('newname', InputArgument::REQUIRED, 'new name')
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$toRename = $input->getArgument('serie');
$newName = $input->getArgument('newname');

if (!is_string($toRename) || !is_string($newName)) {
throw new \Exception('Arguments must be strings');
}
/** @var Book[] $books */
$books = $this->bookRepository->getBySerieQuery($this->slugger->slug($toRename))->getResult();

foreach ($books as $book) {
$io->writeln('Renaming '.$toRename.' to '.$newName.' in '.$book->getTitle());
$book->setSerie($newName);
}

$this->entityManager->flush();

return Command::SUCCESS;
}
}
1 change: 0 additions & 1 deletion src/Controller/Admin/BookCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public function configureFields(string $pageName): iterable
TextEditorField::new('summary')->hideOnIndex(),
TextField::new('serie'),
NumberField::new('serieIndex'),
TextField::new('mainAuthor'),
TextField::new('language'),
TextField::new('publisher')->hideOnIndex(),
DateField::new('publishDate')->hideOnIndex(),
Expand Down
18 changes: 7 additions & 11 deletions src/Controller/AuthorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,30 @@ class AuthorController extends AbstractController
#[Route('/{page}', name: 'app_authors', requirements: ['page' => '\d+'])]
public function index(BookRepository $bookRepository, PaginatorInterface $paginator, int $page = 1): Response
{
$authors = $bookRepository->getAllAuthors()->getResult();
$authors = $bookRepository->getAllAuthors();

$pagination = $paginator->paginate($authors, $page, 18);

return $this->render('group/index.html.twig', [
'pagination' => $pagination,
'page' => $page,
'type' => 'mainAuthor',
'type' => 'authors',
]);
}

#[Route('/{slug}/{page}', name: 'app_mainAuthor_detail', requirements: ['page' => '\d+'])]
#[Route('/{slug}/{page}', name: 'app_authors_detail', requirements: ['page' => '\d+'])]
public function detail(string $slug, BookRepository $bookRepository, PaginatorInterface $paginator, int $page = 1): Response
{
$authors = $bookRepository->getAllAuthors()->getResult();
if (!is_array($authors)) {
throw $this->createNotFoundException('No authors found');
}
$author = array_filter($authors, static fn ($serie) => $serie['slug'] === $slug);
$authors = $bookRepository->getAllAuthors();

$author = current($author);
$author = $authors[$slug] ?? null;

if (false === $author) {
if (null === $author) {
return $this->redirectToRoute('app_authors');
}

$pagination = $paginator->paginate(
$bookRepository->getByAuthorQuery($slug),
$bookRepository->getByAuthorQuery($author['item']),
$page,
18
);
Expand Down
3 changes: 2 additions & 1 deletion src/Controller/AutocompleteGroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public function index(Request $request, BookRepository $bookRepository, string $
/** @var array<GroupType> $group */
$group = match ($type) {
'serie' => $bookRepository->getAllSeries()->getResult(),
'mainAuthor' => $bookRepository->getAllAuthors()->getResult(),
'authors' => $bookRepository->getAllAuthors(),
'tags' => $bookRepository->getAllTags(),
default => [],
};

Expand Down
8 changes: 3 additions & 5 deletions src/Controller/BookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
#[Route('/books')]
class BookController extends AbstractController
{
#[Route('/{authorSlug}/{book}/{slug}', name: 'app_book')]
public function index(string $authorSlug, Book $book, string $slug, BookSuggestions $bookSuggestions): Response
#[Route('/{book}/{slug}', name: 'app_book')]
public function index(Book $book, string $slug, BookSuggestions $bookSuggestions): Response
{
if ($authorSlug !== $book->getAuthorSlug() || $slug !== $book->getSlug()) {
if ($slug !== $book->getSlug()) {
return $this->redirectToRoute('app_book', [
'authorSlug' => $book->getAuthorSlug(),
'book' => $book->getId(),
'slug' => $book->getSlug(),
], 301);
Expand Down Expand Up @@ -48,7 +47,6 @@ public function downloadImage(Book $book, string $image, BookSuggestions $bookSu
$entityManager->flush();

return $this->redirectToRoute('app_book', [
'authorSlug' => $book->getAuthorSlug(),
'book' => $book->getId(),
'slug' => $book->getSlug(),
], 301);
Expand Down
33 changes: 16 additions & 17 deletions src/Entity/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ class Book
#[Gedmo\Slug(fields: ['serie'], style: 'lower', unique: false)]
private string $serieSlug;

#[ORM\Column(length: 128, unique: false)]
#[Gedmo\Slug(fields: ['mainAuthor'], style: 'lower', unique: false)]
private string $authorSlug;

#[ORM\Column(type: Types::DATE_MUTABLE)]
#[Gedmo\Timestampable(on: 'create')]
private \DateTimeInterface $created;
Expand Down Expand Up @@ -80,7 +76,7 @@ class Book
/**
* @var array<string>
*/
#[ORM\Column(type: Types::ARRAY)]
#[ORM\Column(type: Types::JSON)]
private array $authors = [];

#[ORM\Column(length: 5)]
Expand All @@ -98,7 +94,7 @@ class Book
/**
* @var array<string>|null
*/
#[ORM\Column(type: Types::ARRAY, nullable: true)]
#[ORM\Column(type: Types::JSON, nullable: true)]
private ?array $tags = null;

#[ORM\Column(nullable: false)]
Expand Down Expand Up @@ -339,7 +335,20 @@ public function setAuthors(array $authors): static

public function addAuthor(string $author): static
{
$this->authors[] = $author;
if (!in_array($author, $this->authors, true)) {
$this->authors[] = $author;
}

return $this;
}

public function removeAuthor(string $author): static
{
foreach ($this->authors as $key => $value) {
if ($value === $author) {
unset($this->authors[$key]);
}
}

return $this;
}
Expand Down Expand Up @@ -406,16 +415,6 @@ public function setSerieSlug(string $serieSlug): void
$this->serieSlug = $serieSlug;
}

public function getAuthorSlug(): string
{
return $this->authorSlug;
}

public function setAuthorSlug(string $authorSlug): void
{
$this->authorSlug = $authorSlug;
}

/**
* @return array<string>|null
*/
Expand Down
Loading

0 comments on commit d2d0d01

Please sign in to comment.