1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Modules\Posts\Persistence\Doctrine\Repository; |
4
|
|
|
|
5
|
|
|
use App\Infrastructure\Pagination\Page; |
6
|
|
|
use App\Modules\Posts\Domain\Dto\PostDto; |
7
|
|
|
use App\Modules\Posts\Domain\Dto\PostForBaselineDto; |
8
|
|
|
use App\Modules\Posts\Domain\Dto\PostHeaderDto; |
9
|
|
|
use App\Modules\Posts\Domain\Repository\PostsFindingRepositoryInterface; |
10
|
|
|
use App\Modules\Posts\Persistence\Doctrine\Entity\Post; |
11
|
|
|
use App\Modules\Posts\Persistence\Doctrine\Entity\PostComments; |
12
|
|
|
use Doctrine\ORM\NonUniqueResultException; |
13
|
|
|
use Doctrine\ORM\NoResultException; |
14
|
|
|
use Symfony\Component\Uid\Ulid; |
15
|
|
|
|
16
|
|
|
class DoctrinePostsFindingRepository extends DoctrinePostsRepository implements PostsFindingRepositoryInterface |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param Ulid $id |
21
|
4 |
|
* @return PostDto|null |
22
|
|
|
*/ |
23
|
4 |
|
public function findPost(Ulid $id): ?PostDto |
24
|
4 |
|
{ |
25
|
4 |
|
$dtoClass = PostDto::class; |
26
|
4 |
|
$postClass = Post::class; |
27
|
|
|
$commentClass = PostComments::class; |
28
|
4 |
|
$query = $this->getEntityManager()->createQuery( |
29
|
|
|
"select new $dtoClass( |
30
|
4 |
|
p.id, p.title, p.body, p.tags, c.comments, p.createdById, p.createdByName, p.createdAt, p.updatedAt, p.version |
31
|
4 |
|
) from $postClass p , $commentClass c where c.postId = p.id and p.deletedAt is null and p.id = :id" |
32
|
|
|
); |
33
|
|
|
$query->setParameter("id", $id, "ulid"); |
34
|
|
|
return $query->getResult()[0]; |
35
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
2 |
|
/** |
39
|
|
|
* @param int $pageNo |
40
|
2 |
|
* @return Page<PostHeaderDto> |
41
|
2 |
|
*/ |
42
|
2 |
|
public function findPosts(int $pageNo): Page |
43
|
2 |
|
{ |
44
|
|
|
$dtoClass = PostHeaderDto::class; |
45
|
2 |
|
$postClass = Post::class; |
46
|
|
|
$commentClass = PostComments::class; |
47
|
|
|
$query = $this->getEntityManager()->createQuery( |
48
|
|
|
"select new $dtoClass( |
49
|
2 |
|
p.id, p.title, p.summary, p.tags, c.commentsCount, p.createdById, p.createdByName, p.createdAt |
50
|
2 |
|
) from $postClass p, $commentClass c |
51
|
|
|
where c.postId = p.id and p.deletedAt is null |
52
|
2 |
|
order by p.id desc" |
53
|
2 |
|
) |
54
|
2 |
|
->setFirstResult(($pageNo - 1) * self::PAGE_SIZE) |
55
|
|
|
->setMaxResults(self::PAGE_SIZE); |
56
|
2 |
|
|
57
|
|
|
return new Page( |
58
|
|
|
$query->getArrayResult(), |
59
|
|
|
$this->getCount(), |
60
|
|
|
$pageNo, |
61
|
|
|
self::PAGE_SIZE |
62
|
|
|
); |
63
|
2 |
|
} |
64
|
|
|
|
65
|
2 |
|
/** |
66
|
2 |
|
* @return int |
67
|
2 |
|
*/ |
68
|
|
|
private function getCount(): int |
69
|
2 |
|
{ |
70
|
|
|
$postClass = Post::class; |
71
|
|
|
$query = $this->getEntityManager()->createQuery( |
72
|
|
|
"select count(p.id) as count from $postClass p where p.deletedAt is null" |
73
|
|
|
); |
74
|
|
|
return $query->getScalarResult()[0]["count"]; |
75
|
|
|
} |
76
|
1 |
|
|
77
|
|
|
/** |
78
|
1 |
|
* @param \DateTime|null $from |
79
|
1 |
|
* @return array<PostForBaselineDto> |
80
|
1 |
|
*/ |
81
|
|
|
public function findExistingPostsForBaseline(?\DateTime $from): array |
82
|
1 |
|
{ |
83
|
1 |
|
$dtoClass = PostForBaselineDto::class; |
84
|
|
|
$postClass = Post::class; |
85
|
|
|
$dql = "select new $dtoClass( |
86
|
1 |
|
p.id, p.title, p.body, p.summary, p.tags, p.updatedAt, p.version |
87
|
1 |
|
) from $postClass p where p.deletedAt is null"; |
88
|
|
|
if ($from != null) { |
89
|
1 |
|
$dql = $dql . " and p.createdAt >= :from"; |
90
|
|
|
} |
91
|
|
|
$query = $this->getEntityManager()->createQuery( |
92
|
1 |
|
$dql |
93
|
|
|
); |
94
|
|
|
if ($from != null) { |
95
|
|
|
$query->setParameter("from", $from); |
96
|
|
|
} |
97
|
|
|
return $query->getResult(); |
98
|
|
|
} |
99
|
1 |
|
|
100
|
|
|
/** |
101
|
1 |
|
* @param \DateTime|null $from |
102
|
1 |
|
* @return array<Ulid> |
103
|
1 |
|
*/ |
104
|
|
|
public function findDeletedPostIdsForBaseline(?\DateTime $from): array |
105
|
|
|
{ |
106
|
1 |
|
$postClass = Post::class; |
107
|
1 |
|
$dql = "select p.id as postId from $postClass p where p.deletedAt is not null"; |
108
|
|
|
if ($from != null) { |
109
|
1 |
|
$dql = $dql . " and p.deletedAt >= :from"; |
110
|
|
|
} |
111
|
|
|
$query = $this->getEntityManager()->createQuery( |
112
|
1 |
|
$dql |
113
|
|
|
); |
114
|
1 |
|
if ($from != null) { |
115
|
|
|
$query->setParameter("from", $from); |
116
|
|
|
} |
117
|
|
|
return array_column($query->getArrayResult(), 'postId'); |
118
|
|
|
} |
119
|
|
|
} |