|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Modules\Posts\Api\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use App\Infrastructure\Security\Permission; |
|
6
|
|
|
use App\Modules\Posts\Api\Command\CreatePostCommand; |
|
7
|
|
|
use App\Modules\Posts\Api\Command\DeletePostCommand; |
|
8
|
|
|
use App\Modules\Posts\Api\Command\UpdatePostCommand; |
|
9
|
|
|
use App\Modules\Posts\Api\PostsApiInterface; |
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
12
|
|
|
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; |
|
13
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
14
|
|
|
use Symfony\Component\Uid\Ulid; |
|
15
|
|
|
|
|
16
|
|
|
#[Route('/api/admin/posts')] |
|
17
|
|
|
class PostsCommandController extends AbstractController |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @param PostsApiInterface $postsApi |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct( |
|
23
|
7 |
|
private PostsApiInterface $postsApi |
|
24
|
|
|
) |
|
25
|
|
|
{ |
|
26
|
|
|
} |
|
27
|
7 |
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param CreatePostCommand $command |
|
30
|
|
|
* @return Response |
|
31
|
|
|
*/ |
|
32
|
|
|
#[Route('/', methods: ['POST'])] |
|
33
|
7 |
|
public function createPost(CreatePostCommand $command): Response |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->json( |
|
36
|
7 |
|
$this->postsApi->createPost($command) |
|
37
|
7 |
|
); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param UpdatePostCommand $command |
|
42
|
|
|
* @param string $id |
|
43
|
|
|
* @return Response |
|
44
|
|
|
*/ |
|
45
|
|
|
#[Route('/{id}', methods: ['PUT'])] |
|
46
|
1 |
|
public function updatePost(UpdatePostCommand $command, string $id): Response |
|
47
|
|
|
{ |
|
48
|
|
|
$command->setId(new Ulid($id)); |
|
49
|
1 |
|
if(!$this->isGranted(Permission::EDIT, $command)) { |
|
50
|
1 |
|
throw new UnauthorizedHttpException(Permission::EDIT); |
|
51
|
|
|
} |
|
52
|
|
|
$this->postsApi->updatePost($command); |
|
|
|
|
|
|
53
|
1 |
|
return new Response(); |
|
54
|
1 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $id |
|
58
|
|
|
* @return Response |
|
59
|
|
|
*/ |
|
60
|
|
|
#[Route('/{id}', methods: ['DELETE'])] |
|
61
|
1 |
|
public function deletePost(string $id): Response |
|
62
|
|
|
{ |
|
63
|
|
|
$command = new DeletePostCommand(new Ulid($id)); |
|
64
|
1 |
|
if(!$this->isGranted(Permission::DELETE, $command)) { |
|
65
|
1 |
|
throw new UnauthorizedHttpException(Permission::DELETE); |
|
66
|
|
|
} |
|
67
|
|
|
$this->postsApi->deletePost($command); |
|
|
|
|
|
|
68
|
1 |
|
return new Response(); |
|
69
|
1 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
} |