|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
// src/Controller/Player/Friend/AddFriend.php |
|
4
|
|
|
declare(strict_types=1); |
|
5
|
|
|
|
|
6
|
|
|
namespace VideoGamesRecords\CoreBundle\Controller\Player\Friend; |
|
7
|
|
|
|
|
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
9
|
|
|
use Doctrine\ORM\Exception\ORMException; |
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
use VideoGamesRecords\CoreBundle\Entity\Player; |
|
14
|
|
|
use VideoGamesRecords\CoreBundle\Repository\PlayerRepository; |
|
15
|
|
|
use VideoGamesRecords\CoreBundle\Security\UserProvider; |
|
16
|
|
|
|
|
17
|
|
|
class AddFriend extends AbstractController |
|
18
|
|
|
{ |
|
19
|
|
|
public function __construct( |
|
20
|
|
|
private readonly EntityManagerInterface $em, |
|
21
|
|
|
private readonly UserProvider $userProvider, |
|
22
|
|
|
private readonly PlayerRepository $playerRepository |
|
23
|
|
|
) { |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param Request $request |
|
28
|
|
|
* @return JsonResponse |
|
29
|
|
|
* @throws ORMException |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __invoke(Request $request): JsonResponse |
|
32
|
|
|
{ |
|
33
|
|
|
$data = json_decode($request->getContent(), true); |
|
34
|
|
|
|
|
35
|
|
|
if (!isset($data['friend_id'])) { |
|
36
|
|
|
return new JsonResponse(['error' => 'friend_id is required'], 400); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$friendId = (int) $data['friend_id']; |
|
40
|
|
|
/** @var Player $friend */ |
|
41
|
|
|
$friend = $this->playerRepository->find($friendId); |
|
42
|
|
|
|
|
43
|
|
|
if (!$friend) { |
|
|
|
|
|
|
44
|
|
|
return new JsonResponse(['error' => 'Friend not found'], 404); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$player = $this->userProvider->getPlayer(); |
|
48
|
|
|
$player->addFriend($friend); |
|
49
|
|
|
|
|
50
|
|
|
$this->em->flush(); |
|
51
|
|
|
|
|
52
|
|
|
return new JsonResponse(['success' => true], 200); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|