-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SearchRecipe and test cases for it
- Loading branch information
Showing
6 changed files
with
66 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters