Skip to content

Commit

Permalink
Filtering quickstarts by display name
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Jetmar committed Oct 2, 2024
1 parent 987a60b commit 4389d86
Showing 1 changed file with 54 additions and 9 deletions.
63 changes: 54 additions & 9 deletions pkg/routes/quickstarts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ func FindQuickstartById(id int) (models.Quickstart, error) {
return quickStart, err
}

func findQuickstartsByName(name string, pagination Pagination) ([]models.Quickstart, error) {
func findQuickstartsByDisplayName(displayName string, pagination Pagination) ([]models.Quickstart, error) {
var quickStarts []models.Quickstart
err := database.DB.Limit(pagination.Limit).Offset(pagination.Offset).Where("name = ?", name).Find(&quickStarts).Error
if err != nil {
return nil, err
}
err := database.DB.
Limit(pagination.Limit).
Offset(pagination.Offset).
Where("content->'spec'->>'displayName' ILIKE ?", "%"+displayName+"%").
Find(&quickStarts).Error

return quickStarts, nil
return quickStarts, err
}

func findQuickstartsByTags(tagTypes []models.TagType, tagValues [][]string, pagination Pagination) ([]models.Quickstart, error) {
Expand All @@ -50,19 +51,56 @@ func findQuickstartsByTags(tagTypes []models.TagType, tagValues [][]string, pagi
// Add pagination
err := query.Limit(pagination.Limit).Offset(pagination.Offset).Find(&quickstarts).Error

return quickstarts, err
}

// Helper function to find the intersection of two Quickstart arrays
func intersectQuickstarts(a, b []models.Quickstart) []models.Quickstart {
intersection := []models.Quickstart{}
set := make(map[uint]models.Quickstart)

// Populate the map with items from the first array (a), using the ID field
for _, q := range a {
set[q.ID] = q
}

// Check for items in the second array (b) that also exist in the map
for _, q := range b {
if _, exists := set[q.ID]; exists {
intersection = append(intersection, q)
}
}

return intersection
}

func findQuickstartsByTagsAndDisplayName(tagTypes []models.TagType, tagValues [][]string, displayName string, pagination Pagination) ([]models.Quickstart, error) {
var quickstarts []models.Quickstart
var quickstartsFilteredByTags []models.Quickstart
var quickstartsFilteredByDisplayName []models.Quickstart
var err error

quickstartsFilteredByTags, err = findQuickstartsByTags(tagTypes, tagValues, pagination)
if err != nil {
return nil, err
}

return quickstarts, nil
quickstartsFilteredByDisplayName, err = findQuickstartsByDisplayName(displayName, pagination)
quickstarts = intersectQuickstarts(quickstartsFilteredByDisplayName, quickstartsFilteredByTags)

return quickstarts, err
}

func findQuickstarts(tagTypes []models.TagType, tagValues [][]string, name string, pagination Pagination) ([]models.Quickstart, error) {
func findQuickstarts(tagTypes []models.TagType, tagValues [][]string, name string, displayName string, pagination Pagination) ([]models.Quickstart, error) {
var quickstarts []models.Quickstart
var err error

if name != "" {
err = database.DB.Where("name = ?", name).Find(&quickstarts).Error
} else if displayName != "" && len(tagTypes) > 0 {
quickstarts, err = findQuickstartsByTagsAndDisplayName(tagTypes, tagValues, displayName, pagination)
} else if displayName != "" {
quickstarts, err = findQuickstartsByDisplayName(displayName, pagination)
} else if len(tagTypes) > 0 {
quickstarts, err = findQuickstartsByTags(tagTypes, tagValues, pagination)
} else {
Expand Down Expand Up @@ -100,6 +138,13 @@ func GetAllQuickstarts(w http.ResponseWriter, r *http.Request) {
quickstartName = nameQuery[0]
}

quickstartDisplayName := ""
displayNameQuery := r.URL.Query()["display-name"]
if len(displayNameQuery) > 0 {
// array name query is not required and supported
quickstartDisplayName = displayNameQuery[0]
}

allTagTypes = models.TagType.GetAllTags("")

// List of query parameters and corresponding tag types
Expand All @@ -123,7 +168,7 @@ func GetAllQuickstarts(w http.ResponseWriter, r *http.Request) {
pagination := r.Context().Value(PaginationContextKey).(Pagination)
var err error

quickStarts, err = findQuickstarts(tagTypes, tagValues, quickstartName, pagination)
quickStarts, err = findQuickstarts(tagTypes, tagValues, quickstartName, quickstartDisplayName, pagination)

if err != nil {
w.WriteHeader(http.StatusBadRequest)
Expand Down

0 comments on commit 4389d86

Please sign in to comment.