Skip to content

Commit

Permalink
searches with spaces work
Browse files Browse the repository at this point in the history
  • Loading branch information
bengarrett committed Oct 30, 2023
1 parent 100d0f0 commit 8e2d46b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 34 deletions.
2 changes: 0 additions & 2 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ The following options can be added to [Override].
# Bugs
- `public/text/osd.xml` needs updating to use current search links.
- also check groups for the same problem.
- ren x_pression to x_pression-design
# New features to deliver
Expand Down
70 changes: 48 additions & 22 deletions handler/app/render_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
namer "github.com/Defacto2/releaser/name"
"github.com/Defacto2/server/internal/helper"
"github.com/Defacto2/server/internal/postgres"
"github.com/Defacto2/server/internal/postgres/models"
"github.com/Defacto2/server/model"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
Expand All @@ -27,12 +26,12 @@ const (

// SearchDesc is the handler for the Search for file descriptions page.
func SearchDesc(z *zap.SugaredLogger, c echo.Context) error {
const title, name = "Search for files", "searchPost"
const title, name = "Search titles and descriptions", "searchPost"
if z == nil {
return InternalErr(z, c, name, ErrZap)
}
data := empty()
data["description"] = "Search form to discover file descriptions."
data["description"] = "Search form to scan through file descriptions."
data["logo"] = title
data["title"] = title
data["info"] = "A search for file descriptions"
Expand All @@ -45,12 +44,48 @@ func SearchDesc(z *zap.SugaredLogger, c echo.Context) error {

// PostDescriptions is the handler for the Search for file descriptions form post page.
func PostDescriptions(z *zap.SugaredLogger, c echo.Context) error {
return Post(z, c, Descriptions)
return PostDesc(z, c, Descriptions)
}

// PostDesc is the handler for the Search for filenames form post page.
func PostDesc(z *zap.SugaredLogger, c echo.Context, mode FileSearch) error {
const name = "files"
ctx := context.Background()
db, err := postgres.ConnectDB()
if err != nil {
return DatabaseErr(z, c, name, err)
}
defer db.Close()

input := c.FormValue("search-term-query")
terms := helper.SearchTerm(input)
rel := model.Files{}

fs, _ := rel.SearchDescription(ctx, db, terms)
if err != nil {
return DatabaseErr(z, c, name, err)
}
d := mode.postStats(ctx, db, terms)
s := strings.Join(terms, ", ")
data := emptyFiles()
data["title"] = "Title and description results"
data["h1"] = "Title and description search"
data["lead"] = fmt.Sprintf("Results for %q", s)
data["logo"] = s + " results"
data["description"] = "Title and description search results for " + s + "."
data["unknownYears"] = false
data[records] = fs
data["stats"] = d
err = c.Render(http.StatusOK, "files", data)
if err != nil {
return InternalErr(z, c, name, err)
}
return nil
}

// SearchFile is the handler for the Search for files page.
func SearchFile(z *zap.SugaredLogger, c echo.Context) error {
const title, name = "Search for files", "searchPost"
const title, name = "Search for filenames", "searchPost"
if z == nil {
return InternalErr(z, c, name, ErrZap)
}
Expand All @@ -68,11 +103,11 @@ func SearchFile(z *zap.SugaredLogger, c echo.Context) error {

// PostFilename is the handler for the Search for filenames form post page.
func PostFilename(z *zap.SugaredLogger, c echo.Context) error {
return Post(z, c, Filenames)
return PostName(z, c, Filenames)
}

// PostFilename is the handler for the Search for filenames form post page.
func Post(z *zap.SugaredLogger, c echo.Context, mode FileSearch) error {
// PostName is the handler for the Search for filenames form post page.
func PostName(z *zap.SugaredLogger, c echo.Context, mode FileSearch) error {
const name = "files"
ctx := context.Background()
db, err := postgres.ConnectDB()
Expand All @@ -85,20 +120,11 @@ func Post(z *zap.SugaredLogger, c echo.Context, mode FileSearch) error {
terms := helper.SearchTerm(input)
rel := model.Files{}

fs := models.FileSlice{}
switch mode {
case Filenames:
fs, _ = rel.SearchFilename(ctx, db, terms)
if err != nil {
return DatabaseErr(z, c, name, err)
}
case Descriptions:
fs, _ = rel.SearchDescription(ctx, db, terms)
if err != nil {
return DatabaseErr(z, c, name, err)
}
fs, _ := rel.SearchFilename(ctx, db, terms)
if err != nil {
return DatabaseErr(z, c, name, err)
}
d := mode.postFileStats(ctx, db, terms)
d := mode.postStats(ctx, db, terms)
s := strings.Join(terms, ", ")
data := emptyFiles()
data["title"] = "Filename results"
Expand All @@ -116,7 +142,7 @@ func Post(z *zap.SugaredLogger, c echo.Context, mode FileSearch) error {
return nil
}

func (mode FileSearch) postFileStats(ctx context.Context, db *sql.DB, terms []string) map[string]string {
func (mode FileSearch) postStats(ctx context.Context, db *sql.DB, terms []string) map[string]string {
if db == nil {
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions model/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package model
import (
"context"
"database/sql"
"fmt"

"github.com/Defacto2/server/internal/postgres"
"github.com/Defacto2/server/internal/postgres/models"
Expand Down Expand Up @@ -74,6 +75,7 @@ func (f *Files) SearchDescription(ctx context.Context, db *sql.DB, terms []strin
const clauseT = "to_tsvector(record_title) @@ to_tsquery(?)"
const clauseC = "to_tsvector(comment) @@ to_tsquery(?)"
for i, term := range terms {
term = fmt.Sprintf("'%s'", term) // the single quotes are required for terms containing spaces
if i == 0 {
mods = append(mods, qm.Where(clauseT, term))
mods = append(mods, qm.Or(clauseC, term))
Expand Down
22 changes: 13 additions & 9 deletions model/repair.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,23 @@ func RepairReleasers(w io.Writer, ctx context.Context, db *sql.DB) error {
// fix bad imported names, such as those from Demozoo data imports
// each one of these fixes need a redirect
const (
acidbad = "ACID"
acidfix = "ACID PRODUCTIONS"
icebad = "ICE"
icefix = "INSANE CREATORS ENTERPRISE"
pwabad = "pirates with attitude"
pwafix = "pirates with attitudes"
trsibad = "TRISTAR AND RED SECTOR INC"
trsifix = "TRISTAR & RED SECTOR INC"
acidbad = "ACID"
acidfix = "ACID PRODUCTIONS"
icebad = "ICE"
icefix = "INSANE CREATORS ENTERPRISE"
pwabad = "pirates with attitude"
pwafix = "pirates with attitudes"
trsibad = "TRISTAR AND RED SECTOR INC"
trsifix = "TRISTAR & RED SECTOR INC"
xpress = "X-PRESSION"
xpressfix = "X-PRESSION DESIGN"
)
fixes := map[string]string{
trsibad: trsifix,
acidbad: acidfix,
icebad: icefix,
pwabad: pwafix,
trsibad: trsifix,
xpress: xpressfix,
}
rowsAff := int64(0)
for bad, fix := range fixes {
Expand Down
2 changes: 1 addition & 1 deletion model/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (r *Summary) SearchDesc(ctx context.Context, db *sql.DB, terms []string) er
s = fmt.Sprintf("%sOR %s($%d) ", s, clauseT, i+1)
}
s = strings.TrimSpace(s)
if err := queries.Raw(s, strings.Join(terms, ",")).Bind(ctx, db, r); err != nil {
if err := queries.Raw(s, "'"+strings.Join(terms, "','")+"'").Bind(ctx, db, r); err != nil {
return err
}
return nil
Expand Down

0 comments on commit 8e2d46b

Please sign in to comment.