Skip to content

Commit

Permalink
Merge pull request #7 from PcComponentes/GG-403-add-get-filtered-even…
Browse files Browse the repository at this point in the history
…t-to-repository

Added getEventsFilteredByAggregate to EventStoreRepository
  • Loading branch information
jgmullor authored Jan 12, 2022
2 parents 8577dd7 + ab1761b commit 3c57590
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Repository/EventStoreRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ interface EventStoreRepository extends DddEventStoreRepository
{
public function countEventsFor(Uuid $aggregateId): int;

public function countGivenEventsByAggregate(Uuid $aggregateId, string ...$events): int;

public function countEventsFilteredByAggregate(Uuid $aggregateId, string ...$events): int;

public function countEventsForSince(Uuid $aggregateId, DateTimeValueObject $since): int;

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

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

public function countEventsForSinceVersion(Uuid $aggregateId, int $aggregateVersion): int;

public function getSinceVersion(Uuid $aggregateId, int $aggregateVersion): array;
Expand Down
58 changes: 58 additions & 0 deletions src/Repository/PostgresBaseAggregateRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,35 @@ protected function countByAggregateId(Uuid $aggregateId): int
return $result['count'];
}

protected function countGivenEventsByAggregateId(Uuid $aggregateId, string ...$eventNames): int
{
$stmt = $this->connection
->createQueryBuilder()
->select('count(message_id) as count')
->from($this->tableName())
->where('message_name IN (:eventNames)');

$stmt->setParameter('aggregateId', $aggregateId->value(), \PDO::PARAM_STR);
$stmt->setParameter('eventNames', $eventNames, Connection::PARAM_STR_ARRAY);

return $stmt->execute()->fetchOne();
}

protected function countFilteredEventsByAggregateId(Uuid $aggregateId, string ...$eventNames): int
{
$stmt = $this->connection
->createQueryBuilder()
->select('count(message_id) as count')
->from($this->tableName())
->where('message_name NOT IN (:eventNames)');

$stmt->setParameter('aggregateId', $aggregateId->value(), \PDO::PARAM_STR);
$stmt->setParameter('eventNames', $eventNames, Connection::PARAM_STR_ARRAY);

return $stmt->execute()->fetchOne();
}


protected function countByAggregateIdSince(Uuid $aggregateId, DateTimeValueObject $since): int
{
$stmt = $this->connection->prepare(
Expand Down Expand Up @@ -264,6 +293,35 @@ protected function queryGivenEventsByAggregateIdPaginated(
return $events;
}

protected function queryEventsFilteredByAggregateIdPaginated(
Uuid $aggregateId,
int $offset,
int $limit,
string ...$eventNames
): array {
$stmt = $this->connection
->createQueryBuilder()
->addSelect('a.message_id, a.aggregate_id, a.aggregate_version, a.occurred_on, a.message_name, a.payload')
->from($this->tableName(), 'a')
->where('a.aggregate_id = :aggregateId')
->andWhere('a.message_name NOT IN (:eventNames)')
->setParameter('aggregateId', $aggregateId->value(), \PDO::PARAM_STR)
->setParameter('eventNames', $eventNames, Connection::PARAM_STR_ARRAY)
->setFirstResult($offset)
->setMaxResults($limit)
->orderBy('a.occurred_on', 'DESC')
->addOrderBy('a.aggregate_version', 'ASC')
->execute();

$events = $stmt->fetchAll();

foreach ($events as $key => $event) {
$events[$key]['payload'] = \json_decode($event['payload'], true);
}

return $events;
}

protected function execute(Statement $stmt): void
{
$result = $stmt->execute();
Expand Down
15 changes: 15 additions & 0 deletions src/Repository/PostgresEventStoreRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public function getGivenEventsByAggregate(Uuid $aggregateId, int $offset, int $l
return $this->queryGivenEventsByAggregateIdPaginated($aggregateId, $offset, $limit, ...$events);
}

public function getEventsFilteredByAggregate(Uuid $aggregateId, int $offset, int $limit, string ...$events): array
{
return $this->queryEventsFilteredByAggregateIdPaginated($aggregateId, $offset, $limit, ...$events);
}

public function getSince(Uuid $aggregateId, DateTimeValueObject $since): array
{
return $this->findByAggregateIdSince($aggregateId, $since);
Expand Down Expand Up @@ -62,6 +67,16 @@ public function countEventsFor(Uuid $aggregateId): int
return $this->countByAggregateId($aggregateId);
}

public function countGivenEventsByAggregate(Uuid $aggregateId, string ...$events): int
{
return $this->countGivenEventsByAggregateId( $aggregateId, ...$events);
}

public function countEventsFilteredByAggregate(Uuid $aggregateId, string ...$events): int
{
return $this->countFilteredEventsByAggregateId($aggregateId, ...$events);
}

public function countEventsForSince(Uuid $aggregateId, DateTimeValueObject $since): int
{
return $this->countByAggregateIdSince($aggregateId, $since);
Expand Down

0 comments on commit 3c57590

Please sign in to comment.