Skip to content

Commit

Permalink
Add relay repository
Browse files Browse the repository at this point in the history
  • Loading branch information
boreq committed Nov 16, 2023
1 parent d0d2e35 commit e180419
Show file tree
Hide file tree
Showing 16 changed files with 285 additions and 87 deletions.
3 changes: 3 additions & 0 deletions cmd/event-service/di/inject_adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ var sqliteTestAdaptersSet = wire.NewSet(
var sqliteTxAdaptersSet = wire.NewSet(
sqlite.NewEventRepository,
wire.Bind(new(app.EventRepository), new(*sqlite.EventRepository)),

sqlite.NewRelayRepository,
wire.Bind(new(app.RelayRepository), new(*sqlite.RelayRepository)),
)

var adaptersSet = wire.NewSet(
Expand Down
6 changes: 6 additions & 0 deletions cmd/event-service/di/inject_pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package di

import (
"github.com/google/wire"
"github.com/planetary-social/nos-event-service/service/adapters/gcp"
"github.com/planetary-social/nos-event-service/service/adapters/memorypubsub"
"github.com/planetary-social/nos-event-service/service/adapters/sqlite"
"github.com/planetary-social/nos-event-service/service/app"
Expand All @@ -26,3 +27,8 @@ var sqliteTxPubsubSet = wire.NewSet(
sqlite.NewPublisher,
wire.Bind(new(app.Publisher), new(*sqlite.Publisher)),
)

var externalPubsubSet = wire.NewSet(
gcp.NewNoopPublisher,
wire.Bind(new(app.ExternalEventPublisher), new(*gcp.NoopPublisher)), // todo
)
2 changes: 1 addition & 1 deletion cmd/event-service/di/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package di

import (
"context"
"github.com/planetary-social/nos-event-service/internal/migrations"

"github.com/boreq/errors"
"github.com/hashicorp/go-multierror"
"github.com/planetary-social/nos-event-service/internal/migrations"
"github.com/planetary-social/nos-event-service/service/app"
"github.com/planetary-social/nos-event-service/service/ports/http"
"github.com/planetary-social/nos-event-service/service/ports/memorypubsub"
Expand Down
1 change: 1 addition & 0 deletions cmd/event-service/di/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func BuildService(context.Context, config.Config) (Service, func(), error) {
adaptersSet,
migrationsAdaptersSet,
domainSet,
externalPubsubSet,
)
return Service{}, nil, nil
}
Expand Down
10 changes: 8 additions & 2 deletions cmd/event-service/di/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions internal/fixtures/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ func SomeBytesOfLen(l int) []byte {
return b
}

func SomeMaybeRelayAddress() domain.MaybeRelayAddress {
return domain.NewMaybeRelayAddress(SomeString())
}

var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

func randSeq(n int) string {
Expand Down
2 changes: 1 addition & 1 deletion internal/migrations/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package migrations_test
import (
"context"
"fmt"
"github.com/planetary-social/nos-event-service/internal/migrations"
"testing"

"github.com/planetary-social/nos-event-service/internal"
"github.com/planetary-social/nos-event-service/internal/fixtures"
"github.com/planetary-social/nos-event-service/internal/logging"
"github.com/planetary-social/nos-event-service/internal/migrations"
"github.com/stretchr/testify/require"
)

Expand Down
64 changes: 0 additions & 64 deletions service/adapters/cache.go

This file was deleted.

2 changes: 1 addition & 1 deletion service/adapters/sqlite/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package sqlite
import (
"context"
"database/sql"
"github.com/planetary-social/nos-event-service/internal/migrations"

"github.com/boreq/errors"
"github.com/planetary-social/nos-event-service/internal/migrations"
)

func NewMigrations(fns *MigrationFns) (migrations.Migrations, error) {
Expand Down
2 changes: 1 addition & 1 deletion service/adapters/sqlite/migrations_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package sqlite
import (
"database/sql"
"encoding/json"
"github.com/planetary-social/nos-event-service/internal/migrations"

"github.com/boreq/errors"
"github.com/planetary-social/nos-event-service/internal/migrations"
)

type MigrationsStorage struct {
Expand Down
2 changes: 1 addition & 1 deletion service/adapters/sqlite/migrations_storage_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package sqlite_test

import (
"github.com/planetary-social/nos-event-service/internal/migrations"
"testing"

"github.com/planetary-social/nos-event-service/internal/fixtures"
"github.com/planetary-social/nos-event-service/internal/migrations"
"github.com/stretchr/testify/require"
)

Expand Down
78 changes: 78 additions & 0 deletions service/adapters/sqlite/relay_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package sqlite

import (
"context"
"database/sql"

"github.com/boreq/errors"
"github.com/planetary-social/nos-event-service/service/domain"
)

type RelayRepository struct {
tx *sql.Tx
}

func NewRelayRepository(tx *sql.Tx) *RelayRepository {
return &RelayRepository{tx: tx}
}

func (r *RelayRepository) Save(ctx context.Context, eventID domain.EventId, relayAddress domain.MaybeRelayAddress) error {
_, err := r.tx.Exec(`
INSERT OR IGNORE INTO relays(address)
VALUES($1)`,
relayAddress.String(),
)
if err != nil {
return errors.Wrap(err, "error inserting the relay address")
}

row := r.tx.QueryRow(`
SELECT id FROM relays
WHERE address=$1`,
relayAddress.String(),
)
var dbRelayId int
if err := row.Scan(&dbRelayId); err != nil {
return errors.Wrap(err, "error getting the relay id")

}

row = r.tx.QueryRow(`
SELECT id FROM events
WHERE event_id=$1`,
eventID.Hex(),
)
var dbEventId int
if err := row.Scan(&dbEventId); err != nil {
return errors.Wrap(err, "error getting the event id")
}

_, err = r.tx.Exec(`
INSERT OR IGNORE INTO events_to_relays(event_id, relay_id)
VALUES($1, $2)`,
dbEventId, dbRelayId,
)
if err != nil {
return errors.Wrap(err, "error inserting the relationship")
}

return nil
}

func (r *RelayRepository) List(ctx context.Context) ([]domain.MaybeRelayAddress, error) {
rows, err := r.tx.Query(`SELECT address FROM relays`)
if err != nil {
return nil, errors.Wrap(err, "error selecting addresses")
}

var result []domain.MaybeRelayAddress
for rows.Next() {
var addressString string
if err := rows.Scan(&addressString); err != nil {
return nil, errors.Wrap(err, "error scanning")
}
result = append(result, domain.NewMaybeRelayAddress(addressString))
}

return result, nil
}
Loading

0 comments on commit e180419

Please sign in to comment.