/
FoodVote.php
50 lines (41 loc) · 1.18 KB
/
FoodVote.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Twig;
use App\Entity\Food;
use App\Repository\FoodRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveArg;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\DefaultActionTrait;
#[AsLiveComponent]
class FoodVote extends AbstractController
{
use DefaultActionTrait;
#[LiveProp]
public Food $food;
#[LiveProp]
public bool $hasVoted = false;
public function __construct(private FoodRepository $foodRepository)
{
}
#[LiveAction]
public function vote(#[LiveArg] string $direction)
{
if ('up' === $direction) {
$this->food->upVote();
} else {
$this->food->downVote();
}
$this->foodRepository->add($this->food, true);
$this->hasVoted = true;
}
}