Skip to content

Commit

Permalink
Fixs
Browse files Browse the repository at this point in the history
  • Loading branch information
DevFabi committed Aug 12, 2020
1 parent 3b74265 commit 83475dc
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 36 deletions.
10 changes: 5 additions & 5 deletions features/admin_create.feature
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ Feature:
Given I am on "/admin/users"
And I should see "Users handler"
When I press "Create"
And I fill "user_email" with "toto@gmail.com"
And I fill "user_firstName" with "Toto"
And I fill "user_lastName" with "Titi"
And I fill "user_password" with "titutoti"
And I fill "user_passwordConfirm" with "titutoti"
And I fill "account_email" with "toto@gmail.com"
And I fill "account_firstName" with "Toto"
And I fill "account_lastName" with "Titi"
And I fill "account_password" with "titutoti"
And I fill "account_passwordConfirm" with "titutoti"
And I press "submit"
Then I should be on "/admin/users"
20 changes: 13 additions & 7 deletions src/Command/UpdateBooksCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use App\Entity\Author;
use App\Events\Events;
use App\Service\Books\SaveBook;
use App\Service\BookUploader\BookUploaderInterface;
use App\Specifications\SaveBooks;
use App\Specifications\CanSaveBooksSpecification;
use App\UseCases\CreateBookUseCase;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
Expand All @@ -25,7 +25,7 @@ class UpdateBooksCommand extends Command
protected static $defaultName = 'app:update-books';

private $em;
private $saveBook;
private $canSaveBooksSpecification;
/**
* @var BookUploaderInterface
*/
Expand All @@ -38,15 +38,20 @@ class UpdateBooksCommand extends Command
* @var LoggerInterface
*/
private $logger;
/**
* @var CreateBookUseCase
*/
private $createBookUseCase;

public function __construct(BookUploaderInterface $bookUploader, EntityManagerInterface $em, SaveBooks $saveBook,EventDispatcherInterface $eventDispatcher, LoggerInterface $logger)
public function __construct(BookUploaderInterface $bookUploader, EntityManagerInterface $em, CanSaveBooksSpecification $canSaveBooksSpecification,EventDispatcherInterface $eventDispatcher, LoggerInterface $logger, CreateBookUseCase $createBookUseCase)
{
$this->bookUploader = $bookUploader;
$this->em = $em;
$this->saveBook = $saveBook;
$this->canSaveBooksSpecification = $canSaveBooksSpecification;
parent::__construct();
$this->eventDispatcher = $eventDispatcher;
$this->logger = $logger;
$this->createBookUseCase = $createBookUseCase;
}

protected function configure()
Expand All @@ -71,10 +76,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeln(count($books).' books (not saved)');

// 3/ Save books in database
// $savedBooks = $this->saveBook->save($books);
foreach ($books as $bookToSave)
{
$this->saveBook->isSatisfiedBy($bookToSave);
if ($this->canSaveBooksSpecification->isSatisfiedBy($bookToSave)) {
$this->createBookUseCase->create($bookToSave);
}
}

// 4/ Send user subscribers
Expand Down
4 changes: 2 additions & 2 deletions src/Controller/Admin/AdminAuthorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function index(Request $request, PaginatorInterface $paginator): Response
}

/**
* @Route("/admin/author/create", name="admin_create_author")
* @Route("/admin/author/create", name="admin_create_author", methods={"GET","POST"})
*/
public function create(Request $request): Response
{
Expand All @@ -64,7 +64,7 @@ public function create(Request $request): Response
}

/**
* @Route("/admin/author/{id}/edit", name="admin_edit_author")
* @Route("/admin/author/{id}/edit", name="admin_edit_author", methods={"GET","PUT"})
*/
public function edit(Author $author, Request $request): Response
{
Expand Down
6 changes: 3 additions & 3 deletions src/Form/AccountType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->add('password', PasswordType::class)
->add('passwordConfirm', PasswordType::class)
;
if ($options['validation_groups'][0] == 'admin_edit')
if (in_array('admin_edit', $options['validation_groups'] ))
{
$builder
->remove('email')
->add('email', EmailType::class, ['disabled' => true])
->remove('password')
->remove('passwordConfirm');
}
if ($options['validation_groups'][0] == 'user_edit')
if (in_array('user_edit', $options['validation_groups'] ))
{
$builder
->remove('password')
->remove('passwordConfirm');
}
if ($options['validation_groups'][0] == 'registration')
if (in_array('registration', $options['validation_groups'] ))
{
$builder
->remove('subscriptions');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

use App\Entity\Author;
use App\Entity\Book;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;

class SaveBooks implements SpecificationInterface
class CanSaveBooksSpecification implements SpecificationInterface
{
private $em;

Expand Down Expand Up @@ -40,24 +39,7 @@ public function isSatisfiedBy($bookToSave): bool
return false;
}

$this->createEntity($bookToSave, $book);

return true;
}

private function createEntity(array $bookToSave, Book $book)
{
$book
->setVolumeId($bookToSave["id"])
->setTitle($bookToSave["volumeInfo"]["title"])
->setDescription($bookToSave["volumeInfo"]["description"])
->setImage($bookToSave["volumeInfo"]["imageLinks"]["thumbnail"]);

if (key_exists("publishedDate",$bookToSave["volumeInfo"])){
$book->setPublishedDate(new DateTime($bookToSave["volumeInfo"]["publishedDate"]));
}

$this->em->persist($book);
$this->em->flush();
}
}
47 changes: 47 additions & 0 deletions src/UseCases/CreateBookUseCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php


namespace App\UseCases;


use App\Entity\Author;
use App\Entity\Book;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;

class CreateBookUseCase
{

private $em;

public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}

public function create(array $bookToSave)
{
$book = new Book();

$book
->setVolumeId($bookToSave["id"])
->setTitle($bookToSave["volumeInfo"]["title"])
->setDescription($bookToSave["volumeInfo"]["description"])
->setImage($bookToSave["volumeInfo"]["imageLinks"]["thumbnail"]);

foreach ($bookToSave["volumeInfo"]["authors"] as $author) {
$foundAuthor = $this->em->getRepository(Author::class)->findOneBy(["name" => $author]);
if ($foundAuthor !== null) {
$book->setAuthors($foundAuthor);
continue;
}
}

if (key_exists("publishedDate",$bookToSave["volumeInfo"])){
$book->setPublishedDate(new DateTime($bookToSave["volumeInfo"]["publishedDate"]));
}
$this->em->persist($book);
$this->em->flush();
}

}

0 comments on commit 83475dc

Please sign in to comment.