From 697f0fbd6bb5a971c7e229ad7ffa7958b33b12ed Mon Sep 17 00:00:00 2001 From: Arthur Zamarin Date: Fri, 19 Apr 2024 14:22:04 +0300 Subject: app/search: fix for empty description + optimize query Signed-off-by: Arthur Zamarin --- pkg/app/handler/index/index.templ | 2 +- pkg/app/handler/packages/search.go | 32 +++++++++++++++++++++++++------- pkg/app/handler/packages/search.templ | 7 +++---- pkg/app/handler/packages/suggest.go | 6 +----- 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) {
- You can search by atom, category, name, maintainer or combine queries. Results similar to your query will be found as well. + You can search by atom, category, name, maintainer or combine queries. Results similar to your query will be found as well. 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) {
@@ -20,9 +19,9 @@ templ search(query string, packages []models.Package) {
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)). -- cgit v1.2.3-65-gdbad