Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/foomo/redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
milosgagovic committed Oct 23, 2023
2 parents 9a0f478 + 0764249 commit 7183d2e
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion domain/redirectdefinition/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ func (a *API) GetRedirects(ctx context.Context) (redirects *redirectstore.Redire
return a.qry.GetRedirects(ctx, a.l)
}

func (a *API) Search(ctx context.Context, qry redirectquery.Search) (redirect *redirectstore.RedirectDefinition, err error) {
func (a *API) Search(ctx context.Context, qry redirectquery.Search) (redirect *redirectstore.RedirectDefinitions, err error) {
return a.qry.Search(ctx, a.l, qry)
}
10 changes: 5 additions & 5 deletions domain/redirectdefinition/query/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ type (
Source redirectstore.RedirectSource `json:"source"`
}
// SearchHandlerFn handler
SearchHandlerFn func(ctx context.Context, l *zap.Logger, qry Search) (*redirectstore.RedirectDefinition, error)
SearchHandlerFn func(ctx context.Context, l *zap.Logger, qry Search) (*redirectstore.RedirectDefinitions, error)
// SearchMiddlewareFn middleware
SearchMiddlewareFn func(next SearchHandlerFn) SearchHandlerFn
)

// SearchHandler ...
func SearchHandler(repo *redirectrepository.RedirectsDefinitionRepository) SearchHandlerFn {
return func(ctx context.Context, l *zap.Logger, qry Search) (*redirectstore.RedirectDefinition, error) {
return repo.Find(ctx, qry.ID, string(qry.Source))
return func(ctx context.Context, l *zap.Logger, qry Search) (*redirectstore.RedirectDefinitions, error) {
return repo.FindMany(ctx, qry.ID, string(qry.Source))
}
}

Expand All @@ -37,15 +37,15 @@ func SearchHandlerComposed(handler SearchHandlerFn, middlewares ...SearchMiddlew
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, qry Search) (*redirectstore.RedirectDefinition, error) {
next = middleware(func(ctx context.Context, l *zap.Logger, qry Search) (*redirectstore.RedirectDefinitions, error) {
trace.SpanFromContext(ctx).AddEvent(middlewareName)
return localNext(ctx, l, qry)
})
}
return next
}
handlerName := strings.Split(runtime.FuncForPC(reflect.ValueOf(handler).Pointer()).Name(), ".")[2]
return composed(func(ctx context.Context, l *zap.Logger, qry Search) (*redirectstore.RedirectDefinition, error) {
return composed(func(ctx context.Context, l *zap.Logger, qry Search) (*redirectstore.RedirectDefinitions, error) {
trace.SpanFromContext(ctx).AddEvent(handlerName)
return handler(ctx, l, qry)
})
Expand Down
18 changes: 18 additions & 0 deletions domain/redirectdefinition/repository/redirectdefinition.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

keelmongo "github.com/foomo/keel/persistence/mongo"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.uber.org/zap"
Expand Down Expand Up @@ -52,6 +53,23 @@ func (rs RedirectsDefinitionRepository) Find(ctx context.Context, id, source str
return &result, nil
}

// TODO: DraganaB check if we need to search by id
func (rs RedirectsDefinitionRepository) FindMany(ctx context.Context, id, source string) (*redirectstore.RedirectDefinitions, error) {

Check warning on line 57 in domain/redirectdefinition/repository/redirectdefinition.go

View workflow job for this annotation

GitHub Actions / test

unused-parameter: parameter 'id' seems to be unused, consider removing or renaming it as _ (revive)
var result redirectstore.RedirectDefinitions

// Create a regex pattern for fuzzy match
pattern := primitive.Regex{Pattern: source, Options: "i"} // "i" for case-insensitive match

// Create a filter with the regex pattern
filter := bson.M{"source": primitive.Regex{Pattern: pattern.Pattern, Options: pattern.Options}}

findErr := rs.collection.FindOne(ctx, filter, &result)
if findErr != nil {
return nil, findErr
}
return &result, nil
}

func (rs RedirectsDefinitionRepository) FindAll(ctx context.Context) (*redirectstore.RedirectDefinitions, error) {
var result redirectstore.RedirectDefinitions
err := rs.collection.Find(ctx, bson.M{}, &result)
Expand Down
2 changes: 1 addition & 1 deletion domain/redirectdefinition/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (rs *Service) GetRedirects(w http.ResponseWriter, r *http.Request) (*redire
return rs.api.GetRedirects(r.Context())
}

func (rs *Service) Search(w http.ResponseWriter, r *http.Request, dimension, id, path string) (*redirectstore.RedirectDefinition, error) {
func (rs *Service) Search(w http.ResponseWriter, r *http.Request, dimension, id, path string) (*redirectstore.RedirectDefinitions, error) {
return rs.api.Search(r.Context(), redirectquery.Search{
ID: id,
Source: redirectstore.RedirectSource(path),
Expand Down
6 changes: 3 additions & 3 deletions domain/redirectdefinition/service/gotsrpc_gen.go

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

10 changes: 5 additions & 5 deletions domain/redirectdefinition/service/gotsrpcclient_gen.go

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

2 changes: 1 addition & 1 deletion domain/redirectdefinition/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

type RedirectDefinitionService interface {
CreateRedirectsFromContentserverexport(w http.ResponseWriter, r *http.Request, old, new map[string]*content.RepoNode) error
Search(w http.ResponseWriter, r *http.Request, dimension, id, path string) (*redirectstore.RedirectDefinition, error)
Search(w http.ResponseWriter, r *http.Request, dimension, id, path string) (*redirectstore.RedirectDefinitions, error)
Create(w http.ResponseWriter, r *http.Request, def *redirectstore.RedirectDefinition) error
Delete(w http.ResponseWriter, r *http.Request, path string) error
Update(w http.ResponseWriter, r *http.Request, def *redirectstore.RedirectDefinition) error
Expand Down

0 comments on commit 7183d2e

Please sign in to comment.