-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add init api methode for create redirects
- Loading branch information
1 parent
f15f659
commit 6d9fe75
Showing
9 changed files
with
428 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
cmd Commands | ||
repo *redirectrepository.RedirectsDefinitionRepository | ||
l *zap.Logger | ||
//meter *cmrccommonmetric.Meter | ||
} | ||
Option func(api *API) | ||
) | ||
|
||
func NewAPI( | ||
l *zap.Logger, | ||
repo *redirectrepository.RedirectsDefinitionRepository, | ||
opts ...Option, | ||
) (*API, error) { | ||
|
||
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 { | ||
return err | ||
} | ||
return nil | ||
} |
53 changes: 53 additions & 0 deletions
53
domain/redirectdefinition/command/createredirectsfromcontentserver.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
return func(ctx context.Context, l *zap.Logger, cmd CreateRedirects) error { | ||
|
||
return nil //repo.Upsert(ctx, entity) | ||
} | ||
} | ||
|
||
// 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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.