Skip to content

Commit

Permalink
Add SearchRecipe and test cases for it
Browse files Browse the repository at this point in the history
  • Loading branch information
wehmoen committed Aug 15, 2024
1 parent 4221ed3 commit dc6fe4b
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 8 deletions.
50 changes: 47 additions & 3 deletions pkg/client/search_recipes.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,51 @@
package client

import "github.com/wehmoen/go-ck/pkg/types"
import (
"github.com/wehmoen/go-ck/pkg/types"
"strconv"
)

func (c *c) SearchRecipes(query string, limit int) ([]*types.Recipe, error) {
panic("not implemented")
func (c *c) SearchRecipes(query string) ([]*types.Recipe, error) {
return c.searchRecipes(query, 0)
}

func (c *c) searchRecipes(query string, offset int) ([]*types.Recipe, error) {
var recipes []*types.Recipe

for {
var result struct {
Count int `json:"count"`
Results []struct {
Recipe *types.Recipe `json:"recipe"`
Score int `json:"score"`
} `json:"results"`
}

_, err := c.restClient.R().
SetResult(&result).
SetQueryParamsFromValues(map[string][]string{
"query": {query},
"limit": {strconv.Itoa(types.PaginationDefaultLimit)},
"offset": {strconv.Itoa(offset)},
"orderBy": {"2"},
"descendCategories": {"1"},
"order": {"0"},
}).
Get("/recipes")

if err != nil {
return nil, err
}

for _, recipe := range result.Results {
recipes = append(recipes, recipe.Recipe)
}

if len(result.Results) < types.PaginationDefaultLimit {
break
}
offset += len(result.Results)
}

return recipes, nil
}
2 changes: 1 addition & 1 deletion pkg/types/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ type Client interface {
GetComments(recipeId string) ([]*Comment, error)
GetImages(recipeId string) ([]*RecipeImage, error)
GetUser(userId string, includeProfile bool) (*User, error)
SearchRecipes(query string, limit int) ([]*Recipe, error)
SearchRecipes(query string) ([]*Recipe, error)
}
4 changes: 4 additions & 0 deletions test/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"github.com/wehmoen/go-ck/pkg/types"
)

const TestRecipeId = "2529831396465550" // Tasty pancakes
const TestUserId = "bcfce3497b42e48f1210823471c1312f"
const TestSearchQuery = "pizza"

var c types.Client

func init() {
Expand Down
2 changes: 0 additions & 2 deletions test/recipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"testing"
)

const TestRecipeId = "2529831396465550" // Tasty pancakes

func TestGetRecipe(t *testing.T) {
recipe, err := c.GetRecipe(TestRecipeId)
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions test/search_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package test

import "testing"

func TestSearchRecipes(t *testing.T) {
recipes, err := c.SearchRecipes(TestSearchQuery)
if err != nil {
t.Fatalf("Failed to search recipes: %v", err)
}

if len(recipes) == 0 {
t.Fatalf("No recipes found. There should be recipes for the test search query.")
}
}
2 changes: 0 additions & 2 deletions test/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"testing"
)

const TestUserId = "bcfce3497b42e48f1210823471c1312f"

func TestGetUser(t *testing.T) {
user, err := c.GetUser(TestUserId, false)
if err != nil {
Expand Down

0 comments on commit dc6fe4b

Please sign in to comment.