Skip to content

Commit

Permalink
WIP ArtifactRepository.Get()
Browse files Browse the repository at this point in the history
  • Loading branch information
emacsway committed May 24, 2024
1 parent 5fcbd15 commit fa8287d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func NewArtifactRepository(currentSession session.DbSession) *ArtifactRepository {
return &ArtifactRepository{
session: currentSession,
eventStore: repository.NewEventStore(currentSession, "Artifact", eventQuery),
eventStore: repository.NewEventStore(currentSession, "Artifact", eventToQuery),
}
}

Expand All @@ -32,10 +32,35 @@ func (r *ArtifactRepository) NextId(tenantId tenantVal.TenantId) (artifactVal.Ar
return q.Get(r.session)
}

func eventQuery(iEvent aggregate.PersistentDomainEvent) (q session.EventSourcedQueryEvaluator) {
func (r *ArtifactRepository) Get(id artifactVal.ArtifactId) (*artifact.Artifact, error) {
idExporter := &artifactVal.ArtifactIdExporter{}
id.Export(idExporter)
streamId, err := r.eventStore.NewStreamId(int(idExporter.TenantId), idExporter.ArtifactId.String())
if err != nil {
return nil, err
}
q := repository.EventGetQuery{
StreamId: streamId,
EventReconstitutor: rowsToEvent,
}
stream, err := q.Stream(r.session)
if err != nil {
return nil, err
}
rec := &artifact.ArtifactReconstitutor{
PastEvents: stream,
}
return rec.Reconstitute()
}

func eventToQuery(iEvent aggregate.PersistentDomainEvent) (q session.EventSourcedQueryEvaluator) {
switch event := iEvent.(type) {
case *events.ArtifactProposed:
q = queries.NewArtifactProposedQuery(event)
}
return q
}

func rowsToEvent(*session.Rows) (aggregate.PersistentDomainEvent, error) {

Check failure on line 64 in grade/internal/infrastructure/repositories/artifact/artifact_repository.go

View workflow job for this annotation

GitHub Actions / lint

ptrToRefParam: consider to make non-pointer type for `*session.Rows` (gocritic)
return nil, nil
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"github.com/emacsway/grade/grade/internal/infrastructure/seedwork/session"
)

type EventReconstitutor func(*session.Rows) (aggregate.PersistentDomainEvent, error)

type EventGetQuery struct {
StreamId StreamId
SincePosition uint
StreamId StreamId
SincePosition uint
EventReconstitutor EventReconstitutor
}

func (q EventGetQuery) sql() string {
Expand All @@ -32,10 +35,12 @@ func (q *EventGetQuery) Stream(s session.DbSessionQuerier) ([]aggregate.Persiste
}
defer rows.Close()
for rows.Next() {
err := rows.Scan() // TODO: implement me
// err := rows.Scan() // TODO: implement me
event, err := q.EventReconstitutor(&rows)
if err != nil {
return nil, err
}
stream = append(stream, event)
}
err = rows.Err()
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ type EventStore struct {
eventQuery EventQueryFactory
}

func (r EventStore) NewStreamId(
tenantId int,
streamId string,
) (StreamId, error) {
return NewStreamId(tenantId, r.streamType, streamId)
}

func (r *EventStore) Save(
agg aggregate.DomainEventAccessor[aggregate.PersistentDomainEvent],
eventMeta aggregate.EventMeta,
Expand Down

0 comments on commit fa8287d

Please sign in to comment.