Skip to content

Commit dc6fe4b

Browse files
author
Nico Wehmöller
committed
Add SearchRecipe and test cases for it
1 parent 4221ed3 commit dc6fe4b

File tree

6 files changed

+66
-8
lines changed

6 files changed

+66
-8
lines changed

pkg/client/search_recipes.go

+47-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,51 @@
11
package client
22

3-
import "github.com/wehmoen/go-ck/pkg/types"
3+
import (
4+
"github.com/wehmoen/go-ck/pkg/types"
5+
"strconv"
6+
)
47

5-
func (c *c) SearchRecipes(query string, limit int) ([]*types.Recipe, error) {
6-
panic("not implemented")
8+
func (c *c) SearchRecipes(query string) ([]*types.Recipe, error) {
9+
return c.searchRecipes(query, 0)
10+
}
11+
12+
func (c *c) searchRecipes(query string, offset int) ([]*types.Recipe, error) {
13+
var recipes []*types.Recipe
14+
15+
for {
16+
var result struct {
17+
Count int `json:"count"`
18+
Results []struct {
19+
Recipe *types.Recipe `json:"recipe"`
20+
Score int `json:"score"`
21+
} `json:"results"`
22+
}
23+
24+
_, err := c.restClient.R().
25+
SetResult(&result).
26+
SetQueryParamsFromValues(map[string][]string{
27+
"query": {query},
28+
"limit": {strconv.Itoa(types.PaginationDefaultLimit)},
29+
"offset": {strconv.Itoa(offset)},
30+
"orderBy": {"2"},
31+
"descendCategories": {"1"},
32+
"order": {"0"},
33+
}).
34+
Get("/recipes")
35+
36+
if err != nil {
37+
return nil, err
38+
}
39+
40+
for _, recipe := range result.Results {
41+
recipes = append(recipes, recipe.Recipe)
42+
}
43+
44+
if len(result.Results) < types.PaginationDefaultLimit {
45+
break
46+
}
47+
offset += len(result.Results)
48+
}
49+
50+
return recipes, nil
751
}

pkg/types/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ type Client interface {
55
GetComments(recipeId string) ([]*Comment, error)
66
GetImages(recipeId string) ([]*RecipeImage, error)
77
GetUser(userId string, includeProfile bool) (*User, error)
8-
SearchRecipes(query string, limit int) ([]*Recipe, error)
8+
SearchRecipes(query string) ([]*Recipe, error)
99
}

test/init_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import (
55
"github.com/wehmoen/go-ck/pkg/types"
66
)
77

8+
const TestRecipeId = "2529831396465550" // Tasty pancakes
9+
const TestUserId = "bcfce3497b42e48f1210823471c1312f"
10+
const TestSearchQuery = "pizza"
11+
812
var c types.Client
913

1014
func init() {

test/recipe_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"testing"
55
)
66

7-
const TestRecipeId = "2529831396465550" // Tasty pancakes
8-
97
func TestGetRecipe(t *testing.T) {
108
recipe, err := c.GetRecipe(TestRecipeId)
119
if err != nil {

test/search_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package test
2+
3+
import "testing"
4+
5+
func TestSearchRecipes(t *testing.T) {
6+
recipes, err := c.SearchRecipes(TestSearchQuery)
7+
if err != nil {
8+
t.Fatalf("Failed to search recipes: %v", err)
9+
}
10+
11+
if len(recipes) == 0 {
12+
t.Fatalf("No recipes found. There should be recipes for the test search query.")
13+
}
14+
}

test/user_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"testing"
55
)
66

7-
const TestUserId = "bcfce3497b42e48f1210823471c1312f"
8-
97
func TestGetUser(t *testing.T) {
108
user, err := c.GetUser(TestUserId, false)
119
if err != nil {

0 commit comments

Comments
 (0)