aboutsummaryrefslogtreecommitdiff
blob: 7005709ea22d53928d61d37c823b1864108d4e76 (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
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
// Used to search for packages

package packages

import (
	"github.com/go-pg/pg"
	"html/template"
	"net/http"
	"soko/pkg/app/handler/feeds"
	"soko/pkg/database"
	"soko/pkg/models"
	"strings"
)

// Search renders a template containing a list of search results
// for a given query of packages
func Search(w http.ResponseWriter, r *http.Request) {

	searchTerm := getParameterValue("q", r)
	var packages []models.Package
	var err error

	if strings.Contains(searchTerm, "*"){
		// if the query contains wildcards
		wildcardSearchTerm := strings.ReplaceAll(searchTerm, "*", "%")
		err = database.DBCon.Model(&packages).
			WhereOr("atom LIKE ? ", wildcardSearchTerm).
			WhereOr("name LIKE ? ", wildcardSearchTerm).
			Relation("Versions").
			OrderExpr("name <-> '" + searchTerm + "'").
			Select()
	}else{
		// if the query contains no wildcards do a fuzzy search
		searchQuery := buildSearchQuery(searchTerm)
		err = database.DBCon.Model(&packages).
			Where(searchQuery).
			WhereOr("atom LIKE ? ", ("%" + searchTerm + "%")).
			Relation("Versions").
			OrderExpr("name <-> '" + searchTerm + "'").
			Select()
	}

	if err != nil && err != pg.ErrNoRows {
		http.Error(w, http.StatusText(http.StatusInternalServerError),
			http.StatusInternalServerError)
		return
	}

	renderPackageTemplate("search",
		"search",
		template.FuncMap{},
		getSearchData(packages, searchTerm),
		w)
}

// Search renders a template containing a list of search results
// for a given query of packages
func SearchFeed(w http.ResponseWriter, r *http.Request) {

	searchTerm := getParameterValue("q", r)
	searchTerm = strings.ReplaceAll(searchTerm, "*", "")
	searchQuery := buildSearchQuery(searchTerm)

	var packages []models.Package
	err := database.DBCon.Model(&packages).
		Where(searchQuery).
		Relation("Versions").
		OrderExpr("name <-> '" + searchTerm + "'").
		Select()
	if err != nil && err != pg.ErrNoRows {
		http.Error(w, http.StatusText(http.StatusInternalServerError),
			http.StatusInternalServerError)
		return
	}

	feeds.Packages(nil, getSearchData(packages, searchTerm), w)
}

func buildSearchQuery(searchString string) string {
	var searchClauses []string
	for _, searchTerm := range strings.Split(searchString, " "){
		searchClauses = append(searchClauses,
			"( (category % '" + searchTerm + "') OR (name % '" + searchTerm + "') OR (atom % '" + searchTerm + "') OR (maintainers @> '[{\"Name\": \"" + searchTerm + "\"}]' OR maintainers @> '[{\"Email\": \"" + searchTerm + "\"}]'))")
	}
	return strings.Join(searchClauses, " AND ")
}