Skip to content

Commit 1256a81

Browse files
author
Mario Marco
committed
GG-403 added getEventsFilteredByAggregate to EventStoreRepository
1 parent 8577dd7 commit 1256a81

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/Repository/EventStoreRepository.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public function countEventsForSince(Uuid $aggregateId, DateTimeValueObject $sinc
1515

1616
public function getGivenEventsByAggregate(Uuid $aggregateId, int $offset, int $limit, string ...$events): array;
1717

18+
public function getEventsFilteredByAggregate(Uuid $aggregateId, int $offset, int $limit, string ...$events): array;
19+
1820
public function countEventsForSinceVersion(Uuid $aggregateId, int $aggregateVersion): int;
1921

2022
public function getSinceVersion(Uuid $aggregateId, int $aggregateVersion): array;

src/Repository/PostgresBaseAggregateRepository.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,35 @@ protected function queryGivenEventsByAggregateIdPaginated(
264264
return $events;
265265
}
266266

267+
protected function queryEventsFilteredByAggregateIdPaginated(
268+
Uuid $aggregateId,
269+
int $offset,
270+
int $limit,
271+
string ...$eventNames
272+
): array {
273+
$stmt = $this->connection
274+
->createQueryBuilder()
275+
->addSelect('a.message_id, a.aggregate_id, a.aggregate_version, a.occurred_on, a.message_name, a.payload')
276+
->from($this->tableName(), 'a')
277+
->where('a.aggregate_id = :aggregateId')
278+
->andWhere('a.message_name NOT IN (:eventNames)')
279+
->setParameter('aggregateId', $aggregateId->value(), \PDO::PARAM_STR)
280+
->setParameter('eventNames', $eventNames, Connection::PARAM_STR_ARRAY)
281+
->setFirstResult($offset)
282+
->setMaxResults($limit)
283+
->orderBy('a.occurred_on', 'DESC')
284+
->addOrderBy('a.aggregate_version', 'ASC')
285+
->execute();
286+
287+
$events = $stmt->fetchAll();
288+
289+
foreach ($events as $key => $event) {
290+
$events[$key]['payload'] = \json_decode($event['payload'], true);
291+
}
292+
293+
return $events;
294+
}
295+
267296
protected function execute(Statement $stmt): void
268297
{
269298
$result = $stmt->execute();

src/Repository/PostgresEventStoreRepository.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public function getGivenEventsByAggregate(Uuid $aggregateId, int $offset, int $l
3232
return $this->queryGivenEventsByAggregateIdPaginated($aggregateId, $offset, $limit, ...$events);
3333
}
3434

35+
public function getEventsFilteredByAggregate(Uuid $aggregateId, int $offset, int $limit, string ...$events): array
36+
{
37+
return $this->queryEventsFilteredByAggregateIdPaginated($aggregateId, $offset, $limit, ...$events);
38+
}
39+
3540
public function getSince(Uuid $aggregateId, DateTimeValueObject $since): array
3641
{
3742
return $this->findByAggregateIdSince($aggregateId, $since);

0 commit comments

Comments
 (0)