Skip to content

Commit 3c57590

Browse files
authored
Merge pull request #7 from PcComponentes/GG-403-add-get-filtered-event-to-repository
Added getEventsFilteredByAggregate to EventStoreRepository
2 parents 8577dd7 + ab1761b commit 3c57590

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

src/Repository/EventStoreRepository.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ interface EventStoreRepository extends DddEventStoreRepository
1111
{
1212
public function countEventsFor(Uuid $aggregateId): int;
1313

14+
public function countGivenEventsByAggregate(Uuid $aggregateId, string ...$events): int;
15+
16+
public function countEventsFilteredByAggregate(Uuid $aggregateId, string ...$events): int;
17+
1418
public function countEventsForSince(Uuid $aggregateId, DateTimeValueObject $since): int;
1519

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

22+
public function getEventsFilteredByAggregate(Uuid $aggregateId, int $offset, int $limit, string ...$events): array;
23+
1824
public function countEventsForSinceVersion(Uuid $aggregateId, int $aggregateVersion): int;
1925

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

src/Repository/PostgresBaseAggregateRepository.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,35 @@ protected function countByAggregateId(Uuid $aggregateId): int
169169
return $result['count'];
170170
}
171171

172+
protected function countGivenEventsByAggregateId(Uuid $aggregateId, string ...$eventNames): int
173+
{
174+
$stmt = $this->connection
175+
->createQueryBuilder()
176+
->select('count(message_id) as count')
177+
->from($this->tableName())
178+
->where('message_name IN (:eventNames)');
179+
180+
$stmt->setParameter('aggregateId', $aggregateId->value(), \PDO::PARAM_STR);
181+
$stmt->setParameter('eventNames', $eventNames, Connection::PARAM_STR_ARRAY);
182+
183+
return $stmt->execute()->fetchOne();
184+
}
185+
186+
protected function countFilteredEventsByAggregateId(Uuid $aggregateId, string ...$eventNames): int
187+
{
188+
$stmt = $this->connection
189+
->createQueryBuilder()
190+
->select('count(message_id) as count')
191+
->from($this->tableName())
192+
->where('message_name NOT IN (:eventNames)');
193+
194+
$stmt->setParameter('aggregateId', $aggregateId->value(), \PDO::PARAM_STR);
195+
$stmt->setParameter('eventNames', $eventNames, Connection::PARAM_STR_ARRAY);
196+
197+
return $stmt->execute()->fetchOne();
198+
}
199+
200+
172201
protected function countByAggregateIdSince(Uuid $aggregateId, DateTimeValueObject $since): int
173202
{
174203
$stmt = $this->connection->prepare(
@@ -264,6 +293,35 @@ protected function queryGivenEventsByAggregateIdPaginated(
264293
return $events;
265294
}
266295

296+
protected function queryEventsFilteredByAggregateIdPaginated(
297+
Uuid $aggregateId,
298+
int $offset,
299+
int $limit,
300+
string ...$eventNames
301+
): array {
302+
$stmt = $this->connection
303+
->createQueryBuilder()
304+
->addSelect('a.message_id, a.aggregate_id, a.aggregate_version, a.occurred_on, a.message_name, a.payload')
305+
->from($this->tableName(), 'a')
306+
->where('a.aggregate_id = :aggregateId')
307+
->andWhere('a.message_name NOT IN (:eventNames)')
308+
->setParameter('aggregateId', $aggregateId->value(), \PDO::PARAM_STR)
309+
->setParameter('eventNames', $eventNames, Connection::PARAM_STR_ARRAY)
310+
->setFirstResult($offset)
311+
->setMaxResults($limit)
312+
->orderBy('a.occurred_on', 'DESC')
313+
->addOrderBy('a.aggregate_version', 'ASC')
314+
->execute();
315+
316+
$events = $stmt->fetchAll();
317+
318+
foreach ($events as $key => $event) {
319+
$events[$key]['payload'] = \json_decode($event['payload'], true);
320+
}
321+
322+
return $events;
323+
}
324+
267325
protected function execute(Statement $stmt): void
268326
{
269327
$result = $stmt->execute();

src/Repository/PostgresEventStoreRepository.php

Lines changed: 15 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);
@@ -62,6 +67,16 @@ public function countEventsFor(Uuid $aggregateId): int
6267
return $this->countByAggregateId($aggregateId);
6368
}
6469

70+
public function countGivenEventsByAggregate(Uuid $aggregateId, string ...$events): int
71+
{
72+
return $this->countGivenEventsByAggregateId( $aggregateId, ...$events);
73+
}
74+
75+
public function countEventsFilteredByAggregate(Uuid $aggregateId, string ...$events): int
76+
{
77+
return $this->countFilteredEventsByAggregateId($aggregateId, ...$events);
78+
}
79+
6580
public function countEventsForSince(Uuid $aggregateId, DateTimeValueObject $since): int
6681
{
6782
return $this->countByAggregateIdSince($aggregateId, $since);

0 commit comments

Comments
 (0)