/
search.go
192 lines (175 loc) · 5.55 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package models
import "github.com/jmoiron/sqlx"
const (
// SortRecipeByName represents the value to use in RecipesFilter.SortBy
// in order to sort by the recipe name
SortRecipeByName string = "name"
// SortRecipeByID represents the value to use in RecipesFilter.SortBy
// in order to sort by the recipe ID
SortRecipeByID string = "id"
// SortRecipeByRating represents the value to use in RecipesFilter.SortBy
// in order to sort by the recipe rating
SortRecipeByRating string = "rating"
// SortTagByText represents the value to use in TagsFilter.SortBy
// in order to sort by the tag value
SortTagByText string = "tag"
// SortTagByFrequency represents the value to use in TagsFilter.SortBy
// in order to sort by the number of recipes using a tag
SortTagByFrequency string = "frequency"
// SortByRandom represents the value to use in RecipesFilter.SortBy
// and TagsFilter.SortBy in order to sort the results randomly
SortByRandom string = "random"
// SortDirAsc represents the value to use in RecipesFilter.SortDir
// and TagsFilter.SortDir in order to sort the results in ascending order.
SortDirAsc string = "asc"
// SortDirDesc represents the value to use in RecipesFilter.SortDir
// and TagsFilter.SortDir in order to sort the results in descending order.
SortDirDesc string = "desc"
)
// SearchModel provides functionality to search recipes.
type SearchModel struct {
*Model
}
// RecipesFilter is the primary model class for recipe search
type RecipesFilter struct {
Query string `json:"query"`
Tags []string `json:"tags"`
SortBy string `json:"sortBy"`
SortDir string `json:"sortDir"`
Page int64 `json:"page"`
Count int64 `json:"count"`
}
// TagsFilter is the primary model class for tag search
type TagsFilter struct {
SortBy string `json:"sortBy"`
SortDir string `json:"sortDir"`
Count int64 `json:"count"`
}
// FindRecipes retrieves all recipes matching the specified search filter and within the range specified.
func (m *SearchModel) FindRecipes(filter RecipesFilter) (*Recipes, int64, error) {
var total int64
var search string
if filter.Query == "" {
search = "%"
} else {
search = "%" + filter.Query + "%"
}
partialStmt := "FROM recipe AS r " +
"WHERE (r.name ILIKE ? OR r.Ingredients ILIKE ? OR r.directions ILIKE ? OR EXISTS (SELECT 1 FROM recipe_tag as t WHERE t.recipe_id = r.id AND t.tag ILIKE ?))"
if len(filter.Tags) > 0 {
partialStmt += " AND EXISTS (SELECT 1 FROM recipe_tag AS t WHERE t.recipe_id = r.id AND t.tag IN (?))"
}
countStmt := "SELECT count(r.id) " + partialStmt
var err error
var countArgs []interface{}
if len(filter.Tags) == 0 {
countStmt, countArgs, err = sqlx.In(countStmt, search, search, search, search)
} else {
countStmt, countArgs, err = sqlx.In(countStmt, search, search, search, search, filter.Tags)
}
if err != nil {
return nil, 0, err
}
countStmt = m.db.Rebind(countStmt)
if err := m.db.Get(&total, countStmt, countArgs...); err != nil {
return nil, 0, err
}
offset := filter.Count * (filter.Page - 1)
selectStmt := "SELECT " +
"r.id, r.name, r.serving_size, r.nutrition_info, r.ingredients, r.directions, COALESCE((SELECT g.rating FROM recipe_rating AS g WHERE g.recipe_id = r.id), 0) AS avg_rating, COALESCE((SELECT thumbnail_url FROM recipe_image WHERE id = r.image_id), '')" +
partialStmt
switch filter.SortBy {
case SortRecipeByID:
selectStmt += " ORDER BY r.id"
case SortRecipeByRating:
selectStmt += " ORDER BY avg_rating"
case SortByRandom:
selectStmt += " ORDER BY RANDOM()"
case SortRecipeByName:
fallthrough
default:
selectStmt += " ORDER BY r.name"
}
switch filter.SortDir {
case SortDirDesc:
selectStmt += " DESC"
case SortDirAsc:
fallthrough
default:
selectStmt += " ASC"
}
selectStmt += " LIMIT ? OFFSET ?"
var selectArgs []interface{}
if len(filter.Tags) == 0 {
selectStmt, selectArgs, err = sqlx.In(selectStmt, search, search, search, search, filter.Count, offset)
} else {
selectStmt, selectArgs, err = sqlx.In(selectStmt, search, search, search, search, filter.Tags, filter.Count, offset)
}
if err != nil {
return nil, 0, err
}
selectStmt = m.db.Rebind(selectStmt)
rows, err := m.db.Query(selectStmt, selectArgs...)
if err != nil {
return nil, 0, err
}
defer rows.Close()
var recipes Recipes
for rows.Next() {
recipe := Recipe{
MainImage: RecipeImage{},
}
err = rows.Scan(
&recipe.ID,
&recipe.Name,
&recipe.ServingSize,
&recipe.NutritionInfo,
&recipe.Ingredients,
&recipe.Directions,
&recipe.AvgRating,
&recipe.MainImage.ThumbnailURL)
if err != nil {
return nil, 0, err
}
recipes = append(recipes, recipe)
}
if err := rows.Err(); err != nil {
return nil, 0, err
}
return &recipes, total, nil
}
// FindTags retrieves all tags matching the specified search filter and within the range specified.
func (m *SearchModel) FindTags(filter TagsFilter) (*[]string, error) {
selectStmt := "SELECT tag, COUNT(tag) AS dups FROM recipe_tag GROUP BY tag"
switch filter.SortBy {
case SortTagByText:
selectStmt += " ORDER BY tag"
case SortTagByFrequency:
selectStmt += " ORDER BY dups"
case SortByRandom:
selectStmt += " ORDER BY RANDOM()"
}
if filter.SortDir == SortDirDesc {
selectStmt += " DESC"
}
selectStmt += " LIMIT $1"
rows, err := m.db.Query(
selectStmt, filter.Count)
if err != nil {
return nil, err
}
defer rows.Close()
var tags []string
for rows.Next() {
var tag string
var throwAway int
if err := rows.Scan(&tag, &throwAway); err != nil {
return nil, err
}
tags = append(tags, tag)
}
if err := rows.Err(); err != nil {
return nil, err
}
return &tags, nil
}