Skip to content

Commit

Permalink
add init api methode for create redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
milosgagovic committed Oct 5, 2023
1 parent f15f659 commit 6d9fe75
Show file tree
Hide file tree
Showing 9 changed files with 428 additions and 13 deletions.
35 changes: 29 additions & 6 deletions domain/redirectdefinition/api.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,63 @@
package redirectdefinition

import (
"context"
"errors"

redirectcommand "github.com/foomo/redirects/domain/redirectdefinition/command"
redirectrepository "github.com/foomo/redirects/domain/redirectdefinition/repository"
"go.uber.org/zap"
)

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

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

View workflow job for this annotation

GitHub Actions / test

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

Check failure on line 19 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,
repo *redirectrepository.RedirectsDefinitionRepository,
opts ...Option,
) (*API, error) {

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

View workflow job for this annotation

GitHub Actions / test

unnecessary leading newline (whitespace)

inst := &API{
l: l,
//repo: checkoutRepo,
l: l,
repo: repo,
//meter: cmrccommonmetric.NewMeter(l, "checkout", telemetry.Meter()),
}
if inst.l == nil {
return nil, errors.New("missing logger")
}
if inst.repo == nil {
return nil, errors.New("missing cart repository")
}
inst.cmd = Commands{
CreateRedirects: redirectcommand.CreateRedirectsHandlerComposed(
redirectcommand.CreateRedirectsHandler(inst.repo),
),
}

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

return inst, nil
}

// ------------------------------------------------------------------------------------------------
// ~ Public methods
// ------------------------------------------------------------------------------------------------

func (a *API) CreateRedirects(ctx context.Context, cmd redirectcommand.CreateRedirects) (err error) {
if err := a.cmd.CreateRedirects(ctx, a.l, cmd); err != nil {

Check warning on line 59 in domain/redirectdefinition/api.go

View workflow job for this annotation

GitHub Actions / test

if-return: redundant if ...; err != nil check, just return error instead. (revive)
return err
}
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package redirectcommand

import (
"context"
"reflect"
"runtime"
"strings"

"github.com/foomo/contentserver/content"
redirectrepository "github.com/foomo/redirects/domain/redirectdefinition/repository"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
)

type (
// CreateRedirects command
CreateRedirects struct {
OldState map[string]*content.RepoNode `json:"oldState"`
NewState map[string]*content.RepoNode `json:"newState"`
}
// CreateRedirectsHandlerFn handler
CreateRedirectsHandlerFn func(ctx context.Context, l *zap.Logger, cmd CreateRedirects) error
// CreateRedirectsMiddlewareFn middleware
CreateRedirectsMiddlewareFn func(next CreateRedirectsHandlerFn) CreateRedirectsHandlerFn
)

// CreateRedirectsHandler ...
func CreateRedirectsHandler(repo *redirectrepository.RedirectsDefinitionRepository) CreateRedirectsHandlerFn {

Check warning on line 28 in domain/redirectdefinition/command/createredirectsfromcontentserver.go

View workflow job for this annotation

GitHub Actions / test

unused-parameter: parameter 'repo' seems to be unused, consider removing or renaming it as _ (revive)
return func(ctx context.Context, l *zap.Logger, cmd CreateRedirects) error {

Check failure on line 29 in domain/redirectdefinition/command/createredirectsfromcontentserver.go

View workflow job for this annotation

GitHub Actions / test

unnecessary leading newline (whitespace)

return nil //repo.Upsert(ctx, entity)

Check failure on line 31 in domain/redirectdefinition/command/createredirectsfromcontentserver.go

View workflow job for this annotation

GitHub Actions / test

commentFormatting: put a space between `//` and comment text (gocritic)
}
}

// CreateRedirectsHandlerComposed returns the handler with middleware applied to it
func CreateRedirectsHandlerComposed(handler CreateRedirectsHandlerFn, middlewares ...CreateRedirectsMiddlewareFn) CreateRedirectsHandlerFn {
composed := func(next CreateRedirectsHandlerFn) CreateRedirectsHandlerFn {
for _, middleware := range middlewares {
localNext := next
middlewareName := strings.Split(runtime.FuncForPC(reflect.ValueOf(middleware).Pointer()).Name(), ".")[2]
next = middleware(func(ctx context.Context, l *zap.Logger, cmd CreateRedirects) error {
trace.SpanFromContext(ctx).AddEvent(middlewareName)
return localNext(ctx, l, cmd)
})
}
return next
}
handlerName := strings.Split(runtime.FuncForPC(reflect.ValueOf(handler).Pointer()).Name(), ".")[2]
return composed(func(ctx context.Context, l *zap.Logger, cmd CreateRedirects) error {
trace.SpanFromContext(ctx).AddEvent(handlerName)
return handler(ctx, l, cmd)
})
}
5 changes: 5 additions & 0 deletions domain/redirectdefinition/commands.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package redirectdefinition

import (
redirectcommand "github.com/foomo/redirects/domain/redirectdefinition/command"
)

type Commands struct {
CreateRedirects redirectcommand.CreateRedirectsHandlerFn
}
2 changes: 1 addition & 1 deletion domain/redirectdefinition/repository/redirectdefinition.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type (
}
)

func NewRedirectsStore(l *zap.Logger, persistor *keelmongo.Persistor) (rs *RedirectsDefinitionRepository, err error) {
func NewRedirectsDefinitionRepository(l *zap.Logger, persistor *keelmongo.Persistor) (rs *RedirectsDefinitionRepository, err error) {
collection, cErr := persistor.Collection(
"redirects",
keelmongo.CollectionWithIndexes(
Expand Down
19 changes: 16 additions & 3 deletions domain/redirectdefinition/service.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
package redirectdefinition

import (
"context"

"github.com/foomo/contentserver/content"
keelmongo "github.com/foomo/keel/persistence/mongo"
redirectcommand "github.com/foomo/redirects/domain/redirectdefinition/command"
redirectstore "github.com/foomo/redirects/domain/redirectdefinition/store"
"go.uber.org/zap"
)

// Potentially add Nats to service (still not sure)
type Service struct {
l *zap.Logger
l *zap.Logger
api *API
}

func NewService(l *zap.Logger, p *keelmongo.Persistor) (*Service, error) {
func NewService(l *zap.Logger, p *keelmongo.Persistor, api *API) (*Service, error) {

Check warning on line 19 in domain/redirectdefinition/service.go

View workflow job for this annotation

GitHub Actions / test

unused-parameter: parameter 'p' seems to be unused, consider removing or renaming it as _ (revive)
return &Service{
l: l,
l: l,
api: api,
}, nil
}

func (rs *Service) CreateRedirectsFromContentserverexport(old, new map[string]*content.RepoNode) error {
// TODO: Implement
err := rs.api.CreateRedirects(context.Background(),
redirectcommand.CreateRedirects{
OldState: old,
NewState: new,
})
if err != nil {
return err
}
return nil
}

Expand Down
193 changes: 193 additions & 0 deletions domain/redirectdefinition/service/gotsrpc_gen.go

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

Loading

0 comments on commit 6d9fe75

Please sign in to comment.