File tree 6 files changed +66
-8
lines changed
6 files changed +66
-8
lines changed Original file line number Diff line number Diff line change 1
1
package client
2
2
3
- import "github.com/wehmoen/go-ck/pkg/types"
3
+ import (
4
+ "github.com/wehmoen/go-ck/pkg/types"
5
+ "strconv"
6
+ )
4
7
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
7
51
}
Original file line number Diff line number Diff line change @@ -5,5 +5,5 @@ type Client interface {
5
5
GetComments (recipeId string ) ([]* Comment , error )
6
6
GetImages (recipeId string ) ([]* RecipeImage , error )
7
7
GetUser (userId string , includeProfile bool ) (* User , error )
8
- SearchRecipes (query string , limit int ) ([]* Recipe , error )
8
+ SearchRecipes (query string ) ([]* Recipe , error )
9
9
}
Original file line number Diff line number Diff line change @@ -5,6 +5,10 @@ import (
5
5
"github.com/wehmoen/go-ck/pkg/types"
6
6
)
7
7
8
+ const TestRecipeId = "2529831396465550" // Tasty pancakes
9
+ const TestUserId = "bcfce3497b42e48f1210823471c1312f"
10
+ const TestSearchQuery = "pizza"
11
+
8
12
var c types.Client
9
13
10
14
func init () {
Original file line number Diff line number Diff line change 4
4
"testing"
5
5
)
6
6
7
- const TestRecipeId = "2529831396465550" // Tasty pancakes
8
-
9
7
func TestGetRecipe (t * testing.T ) {
10
8
recipe , err := c .GetRecipe (TestRecipeId )
11
9
if err != nil {
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 4
4
"testing"
5
5
)
6
6
7
- const TestUserId = "bcfce3497b42e48f1210823471c1312f"
8
-
9
7
func TestGetUser (t * testing.T ) {
10
8
user , err := c .GetUser (TestUserId , false )
11
9
if err != nil {
You can’t perform that action at this time.
0 commit comments