Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor config repo #798

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 12 additions & 11 deletions datastore/mongo/configuration.go
Expand Up @@ -14,27 +14,30 @@ import (
type configRepo struct {
innerDB *mongo.Database
client *mongo.Collection
store datastore.Store
}

func NewConfigRepo(db *mongo.Database) datastore.ConfigurationRepository {
func NewConfigRepo(db *mongo.Database, store datastore.Store) datastore.ConfigurationRepository {
return &configRepo{
innerDB: db,
client: db.Collection(ConfigCollection)}
client: db.Collection(ConfigCollection),
store: store,
}
}

func (c *configRepo) CreateConfiguration(ctx context.Context, config *datastore.Configuration) error {
config.ID = primitive.NewObjectID()

_, err := c.client.InsertOne(ctx, config)
err := c.store.Save(ctx, config, nil)
return err
}

func (c *configRepo) LoadConfiguration(ctx context.Context) (*datastore.Configuration, error) {
config := &datastore.Configuration{}

filter := bson.M{"document_status": datastore.ActiveDocumentStatus}
filter := bson.M{}

err := c.client.FindOne(ctx, filter).Decode(&config)
err := c.store.FindOne(ctx, filter, nil, config)

if errors.Is(err, mongo.ErrNoDocuments) {
return nil, datastore.ErrConfigNotFound
Expand All @@ -44,15 +47,13 @@ func (c *configRepo) LoadConfiguration(ctx context.Context) (*datastore.Configur
}

func (c *configRepo) UpdateConfiguration(ctx context.Context, config *datastore.Configuration) error {
filter := bson.M{"uid": config.UID, "document_status": datastore.ActiveDocumentStatus}
filter := bson.M{"uid": config.UID}

update := bson.D{
primitive.E{Key: "$set", Value: bson.D{
primitive.E{Key: "is_analytics_enabled", Value: config.IsAnalyticsEnabled},
primitive.E{Key: "updated_at", Value: primitive.NewDateTimeFromTime(time.Now())},
}},
primitive.E{Key: "is_analytics_enabled", Value: config.IsAnalyticsEnabled},
primitive.E{Key: "updated_at", Value: primitive.NewDateTimeFromTime(time.Now())},
}

_, err := c.client.UpdateOne(ctx, filter, update)
err := c.store.UpdateOne(ctx, filter, update)
return err
}
9 changes: 6 additions & 3 deletions datastore/mongo/configuration_test.go
Expand Up @@ -17,7 +17,8 @@ func Test_CreateConfiguration(t *testing.T) {
db, closeFn := getDB(t)
defer closeFn()

configRepo := NewConfigRepo(db)
store := getStore(db, ConfigCollection)
configRepo := NewConfigRepo(db, store)
config := generateConfig()

require.NoError(t, configRepo.CreateConfiguration(context.Background(), config))
Expand All @@ -33,7 +34,8 @@ func Test_LoadConfiguration(t *testing.T) {
db, closeFn := getDB(t)
defer closeFn()

configRepo := NewConfigRepo(db)
store := getStore(db, ConfigCollection)
configRepo := NewConfigRepo(db, store)
config := generateConfig()

_, err := configRepo.LoadConfiguration(context.Background())
Expand All @@ -53,7 +55,8 @@ func Test_UpdateConfiguration(t *testing.T) {
db, closeFn := getDB(t)
defer closeFn()

configRepo := NewConfigRepo(db)
store := getStore(db, ConfigCollection)
configRepo := NewConfigRepo(db, store)
config := generateConfig()

require.NoError(t, configRepo.CreateConfiguration(context.Background(), config))
Expand Down
3 changes: 2 additions & 1 deletion datastore/mongo/mongo.go
Expand Up @@ -77,6 +77,7 @@ func New(cfg config.Configuration) (datastore.DatabaseClient, error) {
orgs := datastore.New(conn, OrganisationCollection)
org_member := datastore.New(conn, OrganisationMembersCollection)
org_invite := datastore.New(conn, OrganisationInvitesCollection)
config := datastore.New(conn, ConfigCollection)

c := &Client{
db: conn,
Expand All @@ -91,7 +92,7 @@ func New(cfg config.Configuration) (datastore.DatabaseClient, error) {
orgMemberRepo: NewOrgMemberRepo(conn, org_member),
orgInviteRepo: NewOrgInviteRepo(conn, org_invite),
userRepo: NewUserRepo(conn),
configRepo: NewConfigRepo(conn),
configRepo: NewConfigRepo(conn, config),
}

c.ensureMongoIndices()
Expand Down