blob: 6bfc9b33a06f56329b4cd698e58141f8fc4e4f3b (
plain)
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
|
// Used to create package suggestions
package packages
import (
"encoding/json"
"net/http"
"soko/pkg/database"
"soko/pkg/models"
"github.com/go-pg/pg"
)
// Suggest returns json encoded suggestions of
// packages based on the given query
func Suggest(w http.ResponseWriter, r *http.Request) {
searchTerm := getParameterValue("q", r)
var suggestions struct {
Results []searchResults `json:"results"`
}
descriptionQuery := database.DBCon.Model((*models.Version)(nil)).
Column("description").
Where("atom = package.atom").
Limit(1)
err := database.DBCon.Model((*models.Package)(nil)).
Column("name", "category").
ColumnExpr("(?) AS description", descriptionQuery).
Where("atom LIKE ? ", "%"+searchTerm+"%").
OrderExpr("name <-> ?", searchTerm).
Limit(10).
Select(&suggestions.Results)
if err != nil && err != pg.ErrNoRows {
http.Error(w, http.StatusText(http.StatusInternalServerError),
http.StatusInternalServerError)
return
}
b, err := json.Marshal(suggestions)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError),
http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}
|