aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArthur Zamarin <arthurzam@gentoo.org>2024-04-19 14:22:04 +0300
committerArthur Zamarin <arthurzam@gentoo.org>2024-04-19 14:22:04 +0300
commit697f0fbd6bb5a971c7e229ad7ffa7958b33b12ed (patch)
tree33a1246eab789c581dbbf10791f4c19c2a6c8048
parentuser perf: drop useflags preferences (diff)
downloadsoko-697f0fbd6bb5a971c7e229ad7ffa7958b33b12ed.tar.gz
soko-697f0fbd6bb5a971c7e229ad7ffa7958b33b12ed.tar.bz2
soko-697f0fbd6bb5a971c7e229ad7ffa7958b33b12ed.zip
app/search: fix for empty description + optimize query
Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
-rw-r--r--pkg/app/handler/index/index.templ2
-rw-r--r--pkg/app/handler/packages/search.go32
-rw-r--r--pkg/app/handler/packages/search.templ7
-rw-r--r--pkg/app/handler/packages/suggest.go6
4 files changed, 30 insertions, 17 deletions
diff --git a/pkg/app/handler/index/index.templ b/pkg/app/handler/index/index.templ
index c3ddab1..7631bf0 100644
--- a/pkg/app/handler/index/index.templ
+++ b/pkg/app/handler/index/index.templ
@@ -35,7 +35,7 @@ templ header(packageCount int) {
</small>
<br/>
<small class="px-5 text-muted" style="font-size: 12px;">
- You can search by <a href="/packages/search?q=sys-kernel/gentoo-sources">atom</a>, <a href="/packages/search?q=sys-kernel">category</a>, <a href="/packages/search?q=gentoo-sources">name</a>, <a href="/packages/search?q=kernel@gentoo.org">maintainer</a> or <a href="/packages/search?q=x11-wm%20haskell@gentoo.org">combine</a> queries. Results similar to your query will be found as well.
+ You can search by <a href="/packages/search?q=sys-kernel/gentoo-sources">atom</a>, <a href="/packages/search?q=sys-kernel/">category</a>, <a href="/packages/search?q=gentoo-sources">name</a>, <a href="/packages/search?q=kernel@gentoo.org">maintainer</a> or <a href="/packages/search?q=x11-wm%20haskell@gentoo.org">combine</a> queries. Results similar to your query will be found as well.
</small>
</div>
</div>
diff --git a/pkg/app/handler/packages/search.go b/pkg/app/handler/packages/search.go
index ade3982..5a61ee7 100644
--- a/pkg/app/handler/packages/search.go
+++ b/pkg/app/handler/packages/search.go
@@ -14,6 +14,12 @@ import (
"github.com/go-pg/pg/v10"
)
+type searchResults struct {
+ Name string `json:"name"`
+ Category string `json:"category"`
+ Description string `json:"description"`
+}
+
// Search renders a template containing a list of search results
// for a given query of packages
func Search(w http.ResponseWriter, r *http.Request) {
@@ -29,6 +35,13 @@ func Search(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/maintainer/"+searchTerm, http.StatusMovedPermanently)
return
}
+ } else if searchTerm[len(searchTerm)-1] == '/' {
+ categoryName := searchTerm[:len(searchTerm)-1]
+ count, err := database.DBCon.Model((*models.Category)(nil)).Where("name = ?", categoryName).Count()
+ if err == nil && count > 0 {
+ http.Redirect(w, r, "/categories/"+categoryName, http.StatusMovedPermanently)
+ return
+ }
} else if strings.Contains(searchTerm, "/") {
var packages []models.Package
database.DBCon.Model(&packages).Where("atom = ?", searchTerm).Select()
@@ -38,9 +51,14 @@ func Search(w http.ResponseWriter, r *http.Request) {
}
}
- var packages []models.Package
- query := database.DBCon.Model(&packages).
- Relation("Versions")
+ var results []searchResults
+ descriptionQuery := database.DBCon.Model((*models.Version)(nil)).
+ Column("description").
+ Where("atom = package.atom").
+ Limit(1)
+ query := database.DBCon.Model((*models.Package)(nil)).
+ Column("name", "category").
+ ColumnExpr("(?) AS description", descriptionQuery)
if strings.Contains(searchTerm, "*") {
// if the query contains wildcards
@@ -55,18 +73,18 @@ func Search(w http.ResponseWriter, r *http.Request) {
}
err := query.OrderExpr("name <-> ?", searchTerm).
- Select()
+ Select(&results)
if err != nil && err != pg.ErrNoRows {
http.Error(w, http.StatusText(http.StatusInternalServerError),
http.StatusInternalServerError)
return
}
- if len(packages) == 1 {
- http.Redirect(w, r, "/packages/"+packages[0].Atom, http.StatusMovedPermanently)
+ if len(results) == 1 {
+ http.Redirect(w, r, "/packages/"+results[0].Category+"/"+results[0].Name, http.StatusMovedPermanently)
return
}
- layout.Layout(searchTerm, "packages", search(searchTerm, packages)).Render(r.Context(), w)
+ layout.Layout(searchTerm, "packages", search(searchTerm, results)).Render(r.Context(), w)
}
// Search renders a template containing a list of search results
diff --git a/pkg/app/handler/packages/search.templ b/pkg/app/handler/packages/search.templ
index 6fb168f..24b740d 100644
--- a/pkg/app/handler/packages/search.templ
+++ b/pkg/app/handler/packages/search.templ
@@ -1,9 +1,8 @@
package packages
import "strconv"
-import "soko/pkg/models"
-templ search(query string, packages []models.Package) {
+templ search(query string, packages []searchResults) {
<div class="container mb-5">
<div class="row">
<div class="col-12">
@@ -20,9 +19,9 @@ templ search(query string, packages []models.Package) {
</div>
<div class="list-group">
for _, pkg := range packages {
- <a class="list-group-item list-group-item-action" href={ templ.URL("/packages/" + pkg.Atom) }>
+ <a class="list-group-item list-group-item-action" href={ templ.URL("/packages/" + pkg.Category+"/"+pkg.Name) }>
<h3 class="kk-search-result-header"><span class="text-muted">{ pkg.Category }/</span>{ pkg.Name }</h3>
- { pkg.Versions[0].Description }
+ { pkg.Description }
</a>
}
</div>
diff --git a/pkg/app/handler/packages/suggest.go b/pkg/app/handler/packages/suggest.go
index 2d9759b..6bfc9b3 100644
--- a/pkg/app/handler/packages/suggest.go
+++ b/pkg/app/handler/packages/suggest.go
@@ -17,11 +17,7 @@ func Suggest(w http.ResponseWriter, r *http.Request) {
searchTerm := getParameterValue("q", r)
var suggestions struct {
- Results []struct {
- Name string `json:"name"`
- Category string `json:"category"`
- Description string `json:"description"`
- } `json:"results"`
+ Results []searchResults `json:"results"`
}
descriptionQuery := database.DBCon.Model((*models.Version)(nil)).