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