|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Modules\Security\Persistence\Doctrine\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use App\Infrastructure\Pagination\Page; |
|
6
|
|
|
use App\Modules\Security\Domain\Dto\UserPostHeaderDto; |
|
7
|
|
|
use App\Modules\Security\Domain\Repository\UserPostHeadersFindingRepositoryInterface; |
|
8
|
|
|
use App\Modules\Security\Persistence\Doctrine\Entity\UserPostHeader; |
|
9
|
|
|
use Symfony\Component\Uid\Ulid; |
|
10
|
|
|
|
|
11
|
|
|
class DoctrineUserPostHeadersFindingRepository extends DoctrineSecurityRepository implements UserPostHeadersFindingRepositoryInterface |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @return array<UserPostHeaderDto> |
|
16
|
|
|
*/ |
|
17
|
|
|
public function findPostHeaders(): array |
|
18
|
|
|
{ |
|
19
|
|
|
$headerClass = UserPostHeader::class; |
|
20
|
|
|
$dtoClass = UserPostHeaderDto::class; |
|
21
|
|
|
$query = $this->getEntityManager()->createQuery( |
|
22
|
|
|
"select new $dtoClass(p.id, p.title, p.summary, p.tags, u.id, u.email, p.createdAt, p.version, p.commentsCount) |
|
23
|
|
|
from $headerClass p join p.user u order by p.createdAt asc" |
|
24
|
|
|
); |
|
25
|
|
|
return $query->getArrayResult(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return Page<UserPostHeaderDto> |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public function findPostsByUserId(Ulid $userId, int $pageNo): Page |
|
32
|
|
|
{ |
|
33
|
1 |
|
$headerClass = UserPostHeader::class; |
|
34
|
1 |
|
$dtoClass = UserPostHeaderDto::class; |
|
35
|
1 |
|
$query = $this->getEntityManager()->createQuery( |
|
36
|
1 |
|
"select new $dtoClass(p.id, p.title, p.summary, p.tags, u.id, u.email, p.createdAt, p.version, p.commentsCount) |
|
37
|
1 |
|
from $headerClass p join p.user u |
|
38
|
|
|
where u.id = :userId |
|
39
|
|
|
order by p.id desc" |
|
40
|
|
|
); |
|
41
|
1 |
|
$data = $query |
|
42
|
1 |
|
->setParameter("userId", $userId, "ulid") |
|
43
|
1 |
|
->setFirstResult(($pageNo - 1) * self::PAGE_SIZE) |
|
44
|
1 |
|
->setMaxResults(self::PAGE_SIZE) |
|
45
|
1 |
|
->getArrayResult(); |
|
46
|
1 |
|
return new Page( |
|
47
|
1 |
|
$data, |
|
48
|
1 |
|
$this->getCount($userId), |
|
49
|
|
|
$pageNo, |
|
50
|
1 |
|
self::PAGE_SIZE |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
/** |
|
55
|
|
|
* @param $userId |
|
56
|
1 |
|
* @return int |
|
57
|
1 |
|
*/ |
|
58
|
1 |
|
private function getCount($userId): int |
|
59
|
|
|
{ |
|
60
|
1 |
|
$postClass = UserPostHeader::class; |
|
61
|
|
|
$query = $this->getEntityManager()->createQuery( |
|
62
|
1 |
|
"select count(p.id) as count from $postClass p join p.user u where u.id = :userId" |
|
63
|
|
|
); |
|
64
|
|
|
return $query->setParameter("userId", $userId, "ulid")->getResult()[0]["count"]; |
|
65
|
|
|
} |
|
66
|
|
|
} |