Skip to content

Commit ce35666

Browse files
committed
Import reviews (failed)
1 parent 267c04c commit ce35666

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
namespace AppBundle\Action;
44

55
use AppBundle\Entity\Book;
6+
use AppBundle\Entity\Review;
7+
use Doctrine\ORM\EntityManager;
68
use Psr\Log\LoggerInterface;
79
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
810
use Symfony\Component\HttpFoundation\Request;
911

10-
class BuyBook
12+
class ImportReviews
1113
{
1214
private $logger;
1315

@@ -18,22 +20,18 @@ public function __construct(LoggerInterface $logger)
1820

1921
/**
2022
* @Route(
21-
* "/books/{id}/buy",
22-
* name="book_buy",
23+
* "/books/{id}/import-reviews",
24+
* name="book_import_reviews",
2325
* methods={"POST"},
24-
* defaults={"_api_resource_class"=Book::class, "_api_item_operation_name"="buy"}
26+
* defaults={"_api_resource_class"=Book::class, "_api_item_operation_name"="import-reviews"}
2527
* )
2628
*/
27-
public function __invoke(Book $book, Request $request)
29+
public function __invoke(Book $data, Request $request)
2830
{
29-
$this->logger->info('The user wants to buy a book', [
30-
'uuid' => $book->getUuid(),
31-
'name' => $book->getName(),
32-
'contexts' => $request->getContent(),
33-
]);
31+
array_map(function($contents) use ($data) {
32+
$data->getReviews()->add(new Review($data, $contents));
33+
}, explode('¯\_(ツ)_/¯', $request->getContent()));
3434

35-
$book->setName($book->getName() . ' (Bought)');
36-
37-
return $book;
35+
return $data;
3836
}
3937
}

src/AppBundle/Entity/Book.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* @ApiResource(itemOperations={
1212
* "get"={"method"="GET"},
13-
* "buy"={"route_name"="book_buy"}
13+
* "buy"={"route_name"="book_import_reviews"}
1414
* })
1515
*
1616
* @ORM\Entity
@@ -34,7 +34,7 @@ class Book
3434
private $name;
3535

3636
/**
37-
* @ORM\OneToMany(targetEntity="Review", mappedBy="book")
37+
* @ORM\OneToMany(targetEntity="Review", mappedBy="book", cascade={"persist"})
3838
*
3939
* @var Collection
4040
*/

src/AppBundle/Entity/Review.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,34 @@ class Review
3535
* @var Book
3636
*/
3737
private $book;
38+
39+
public function __construct(Book $book, string $contents)
40+
{
41+
$this->contents = $contents;
42+
$this->book = $book;
43+
}
44+
45+
/**
46+
* @return string
47+
*/
48+
public function getUuid(): string
49+
{
50+
return $this->uuid;
51+
}
52+
53+
/**
54+
* @return string
55+
*/
56+
public function getContents(): string
57+
{
58+
return $this->contents;
59+
}
60+
61+
/**
62+
* @return Book
63+
*/
64+
public function getBook(): Book
65+
{
66+
return $this->book;
67+
}
3868
}

0 commit comments

Comments
 (0)