Skip to content

Commit

Permalink
Adding command query structure with api
Browse files Browse the repository at this point in the history
  • Loading branch information
milosgagovic committed Sep 25, 2023
1 parent 0e4190b commit 8bb5a52
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
40 changes: 40 additions & 0 deletions domain/redirectdefinition/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package redirectdefinition

import (
"errors"

"go.uber.org/zap"
)

// API for the domain
type (
API struct {
Queries Queries
Commands Commands
//repo *cmrccheckoutrepo.CheckoutRepository

Check failure on line 14 in domain/redirectdefinition/api.go

View workflow job for this annotation

GitHub Actions / test

commentFormatting: put a space between `//` and comment text (gocritic)
l *zap.Logger
//meter *cmrccommonmetric.Meter

Check failure on line 16 in domain/redirectdefinition/api.go

View workflow job for this annotation

GitHub Actions / test

commentFormatting: put a space between `//` and comment text (gocritic)
}
Option func(api *API)
)

func NewAPI(
l *zap.Logger,
opts ...Option,
) (*API, error) {

Check failure on line 24 in domain/redirectdefinition/api.go

View workflow job for this annotation

GitHub Actions / test

unnecessary leading newline (whitespace)

inst := &API{
l: l,
//repo: checkoutRepo,
//meter: cmrccommonmetric.NewMeter(l, "checkout", telemetry.Meter()),
}
if inst.l == nil {
return nil, errors.New("missing logger")
}

for _, opt := range opts {
opt(inst)
}

return inst, nil
}
4 changes: 4 additions & 0 deletions domain/redirectdefinition/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package redirectdefinition

type Commands struct {
}
4 changes: 4 additions & 0 deletions domain/redirectdefinition/queries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package redirectdefinition

type Queries struct {
}
115 changes: 115 additions & 0 deletions domain/redirectdefinition/repository/redirectdefinition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package redirectdefinition

import (
"context"

keelmongo "github.com/foomo/keel/persistence/mongo"

Check failure on line 6 in domain/redirectdefinition/repository/redirectdefinition.go

View workflow job for this annotation

GitHub Actions / test

import 'github.com/foomo/keel/persistence/mongo' is not allowed from list 'Main' (depguard)
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.uber.org/zap"
)

type RedirectSource string
type RedirectID string
type RedirectTarget string
type RedirectRequest string
type RedirectResponse string
type RedirectCode int

const (
RedirectCodePermanent RedirectCode = 301
RedirectCodeTemporary RedirectCode = 307 // will this be needed?
)

type RedirectDefinition struct {
ID RedirectID `json:"id" bson:"id"`
Source RedirectSource `json:"source" bson:"source"`
Target RedirectTarget `json:"target" bson:"target"`
Code RedirectCode `json:"code" bson:"code"`
RespectParams bool `json:"respectparams" bson:"respectparams"`
TransferParams bool `json:"transferparams" bson:"transferparams"`
}

type RedirectsDefinitionRepository struct {
l *zap.Logger
persistor *keelmongo.Persistor
collection *keelmongo.Collection
}

func NewRedirectsStore(l *zap.Logger, persistor *keelmongo.Persistor) (rs *RedirectsDefinitionRepository, err error) {
collection, cErr := persistor.Collection(
"redirects",
keelmongo.CollectionWithIndexes(
mongo.IndexModel{
Keys: bson.M{
"id": 1,
},
Options: options.Index().SetUnique(true),
},
),
)

if cErr != nil {
return nil, cErr
}
return &RedirectsDefinitionRepository{
l: l,
persistor: persistor,
collection: collection}, nil
}

func (rs RedirectsDefinitionRepository) Find(ctx context.Context, id string) (*RedirectDefinition, error) {
var result RedirectDefinition
findErr := rs.collection.FindOne(ctx, bson.M{"id": id}, &result)
if findErr != nil {
return nil, findErr
}
return &result, nil
}

func (rs RedirectsDefinitionRepository) Insert(ctx context.Context, def *RedirectDefinition) error {
_, err := rs.collection.Col().InsertOne(ctx, def)
return err
}

func (rs RedirectsDefinitionRepository) Update(ctx context.Context, def *RedirectDefinition) error {
filter := bson.D{{Key: "id", Value: def.ID}}
update := bson.D{{Key: "$set", Value: def}}

_, err := rs.collection.Col().UpdateOne(ctx, filter, update)
return err

Check failure on line 82 in domain/redirectdefinition/repository/redirectdefinition.go

View workflow job for this annotation

GitHub Actions / test

unnecessary trailing newline (whitespace)
}

// maybe will be needed for migrating manual redirections?
func (rs RedirectsDefinitionRepository) UpsertMany(ctx context.Context, defs []*RedirectDefinition) error {

Check failure on line 86 in domain/redirectdefinition/repository/redirectdefinition.go

View workflow job for this annotation

GitHub Actions / test

unnecessary leading newline (whitespace)

var operations []mongo.WriteModel

Check failure on line 88 in domain/redirectdefinition/repository/redirectdefinition.go

View workflow job for this annotation

GitHub Actions / test

Consider pre-allocating `operations` (prealloc)

for _, def := range defs {
operation := mongo.NewUpdateOneModel()
operation.SetFilter(bson.M{
"id": def.ID,
})
operation.SetUpdate(bson.D{{Key: "$set", Value: def}})
operation.SetUpsert(true)
operations = append(operations, operation)
}
bulkOption := options.BulkWriteOptions{}
bulkOption.SetOrdered(false)

_, err := rs.collection.Col().BulkWrite(ctx, operations, &bulkOption)
if err != nil {
return err
}

return err
}

func (rs RedirectsDefinitionRepository) Delete(ctx context.Context, id string) error {
filter := bson.D{{Key: "id", Value: id}}

_, err := rs.collection.Col().DeleteOne(ctx, filter)
return err
}

0 comments on commit 8bb5a52

Please sign in to comment.