Skip to content

Commit

Permalink
feature(api): add searching by created by parametr (#908)
Browse files Browse the repository at this point in the history
  • Loading branch information
almostinf authored Sep 19, 2023
1 parent 6b96719 commit 2203388
Show file tree
Hide file tree
Showing 15 changed files with 986 additions and 485 deletions.
37 changes: 18 additions & 19 deletions api/controller/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ func GetAllTriggers(database moira.Database) (*dto.TriggersList, *api.ErrorRespo
}

// SearchTriggers gets trigger page and filter trigger by tags and search request terms
func SearchTriggers(database moira.Database, searcher moira.Searcher, page int64, size int64, onlyErrors bool, filterTags []string, searchString string, createPager bool, pagerID string) (*dto.TriggersList, *api.ErrorResponse) { //nolint
func SearchTriggers(database moira.Database, searcher moira.Searcher, options moira.SearchOptions) (*dto.TriggersList, *api.ErrorResponse) { //nolint
var searchResults []*moira.SearchResult
var total int64
pagerShouldExist := pagerID != ""
pagerShouldExist := options.PagerID != ""

if pagerShouldExist && (searchString != "" || len(filterTags) > 0) {
if pagerShouldExist && (options.SearchString != "" || len(options.Tags) > 0) {
return nil, api.ErrorInvalidRequest(fmt.Errorf("cannot handle request with search string or tags and pager ID set"))
}
if pagerShouldExist {
var err error
searchResults, total, err = database.GetTriggersSearchResults(pagerID, page, size)
searchResults, total, err = database.GetTriggersSearchResults(options.PagerID, options.Page, options.Size)
if err != nil {
return nil, api.ErrorInternalServer(err)
}
Expand All @@ -82,33 +82,32 @@ func SearchTriggers(database moira.Database, searcher moira.Searcher, page int64
}
} else {
var err error
var passSize = size
if createPager {
passSize = pageSizeUnlimited
if options.CreatePager {
options.Size = pageSizeUnlimited
}
searchResults, total, err = searcher.SearchTriggers(filterTags, searchString, onlyErrors, page, passSize)
searchResults, total, err = searcher.SearchTriggers(options)
if err != nil {
return nil, api.ErrorInternalServer(err)
}
}

if createPager && !pagerShouldExist {
if options.CreatePager && !pagerShouldExist {
uuid4, err := uuid.NewV4()
if err != nil {
return nil, api.ErrorInternalServer(err)
}
pagerID = uuid4.String()
err = database.SaveTriggersSearchResults(pagerID, searchResults)
options.PagerID = uuid4.String()
err = database.SaveTriggersSearchResults(options.PagerID, searchResults)
if err != nil {
return nil, api.ErrorInternalServer(err)
}
}

if createPager {
if options.CreatePager {
var from, to int64 = 0, int64(len(searchResults))
if size >= 0 {
from = int64(math.Min(float64(page*size), float64(len(searchResults))))
to = int64(math.Min(float64(from+size), float64(len(searchResults))))
if options.Size >= 0 {
from = int64(math.Min(float64(options.Page*options.Size), float64(len(searchResults))))
to = int64(math.Min(float64(from+options.Size), float64(len(searchResults))))
}
searchResults = searchResults[from:to]
}
Expand All @@ -124,15 +123,15 @@ func SearchTriggers(database moira.Database, searcher moira.Searcher, page int64
}

var pagerIDPtr *string
if pagerID != "" {
pagerIDPtr = &pagerID
if options.PagerID != "" {
pagerIDPtr = &options.PagerID
}

triggersList := dto.TriggersList{
List: make([]moira.TriggerCheck, 0),
Total: &total,
Page: &page,
Size: &size,
Page: &options.Page,
Size: &options.Size,
Pager: pagerIDPtr,
}

Expand Down
Loading

0 comments on commit 2203388

Please sign in to comment.