~netlandish/links

8dc7e3d6de1b508fca48c8c33ee2c4d0b547a7c9 — Yader Velasquez 9 months ago 7960f43
Fixing psql syntax errors in search
4 files changed, 20 insertions(+), 8 deletions(-)

M api/graph/schema.resolvers.go
M api/loaders/loaders.go
M core/routes.go
M helpers.go
M api/graph/schema.resolvers.go => api/graph/schema.resolvers.go +8 -6
@@ 3752,10 3752,11 @@ func (r *queryResolver) GetUsers(ctx context.Context, input *model.GetUserInput)

	if input.Search != nil && *input.Search != "" {
		// We want to search for partial match
		s := links.ParseSearch(*input.Search)
		opts.Filter = sq.And{
			opts.Filter,
			sq.Expr(`to_tsvector('simple', u.full_name || ' ' || u.email || ' ' || o.slug)
				@@ to_tsquery('simple', ?)`, *input.Search+":*"),
				@@ to_tsquery('simple', ?)`, s),
		}

	}


@@ 3913,10 3914,11 @@ func (r *queryResolver) GetAdminOrganizations(ctx context.Context, input *model.

	if input.Search != nil && *input.Search != "" {
		// We want to search for partial match
		s := links.ParseSearch(*input.Search)
		opts.Filter = sq.And{
			opts.Filter,
			sq.Expr(`to_tsvector('simple', o.name || ' ' || o.slug )
				@@ to_tsquery('simple', ?)`, *input.Search+":*"),
				@@ to_tsquery('simple', ?)`, s),
		}
	}



@@ 4192,11 4194,11 @@ func (r *queryResolver) GetAdminDomains(ctx context.Context, input *model.GetAdm
	}

	if input.Search != nil && *input.Search != "" {
		// We want to search for partial match
		s := links.ParseSearch(*input.Search)
		opts.Filter = sq.And{
			opts.Filter,
			sq.Expr(`to_tsvector('simple', d.name || ' ' || d.lookup_name )
				@@ to_tsquery('simple', ?)`, *input.Search+":*"),
				@@ to_tsquery('simple', ?)`, s),
		}
	}



@@ 4752,11 4754,11 @@ func (r *queryResolver) GetOrgLinks(ctx context.Context, input *model.GetLinkInp
	}

	if input.Search != nil && *input.Search != "" {
		// We want to search for partial match
		s := links.ParseSearch(*input.Search)
		linkOpts.Filter = sq.And{
			linkOpts.Filter,
			sq.Expr(`to_tsvector('simple', ol.title || ' ' || ol.description || ' ' || ol.url)
				@@ to_tsquery('simple', ?)`, *input.Search+":*"),
				@@ to_tsquery('simple', ?)`, s),
		}

	}

M api/loaders/loaders.go => api/loaders/loaders.go +3 -1
@@ 2,6 2,7 @@ package loaders

import (
	"context"
	"links"
	"links/models"
	"strings"
	"time"


@@ 71,10 72,11 @@ func getPopularLinks(ctx context.Context) func(key []string) ([][]*models.BaseUR
		}

		if q != "" {
			s := links.ParseSearch(q)
			opts.Filter = sq.And{
				opts.Filter,
				sq.Expr(`to_tsvector('simple', b.title ||  ' ' || b.url)
					@@ to_tsquery('simple', ?)`, q+":*"),
					@@ to_tsquery('simple', ?)`, s),
			}
		}


M core/routes.go => core/routes.go +2 -1
@@ 2350,9 2350,10 @@ func (s *Service) TagAutocomplete(c echo.Context) error {
	var tags []*models.Tag
	var err error
	if q != "" {
		s := links.ParseSearch(q)
		opts := &database.FilterOptions{
			Filter: sq.And{
				sq.Expr(`to_tsvector('simple', t.name) @@ to_tsquery('simple', ?)`, q+":*"),
				sq.Expr(`to_tsvector('simple', t.name) @@ to_tsquery('simple', ?)`, s),
				sq.Or{
					sq.Eq{"ol.user_id": user.ID},
					sq.Eq{"ll.user_id": user.ID},

M helpers.go => helpers.go +7 -0
@@ 757,6 757,13 @@ func (t TagQuery) GetSubQuery(inputTag, inputExcludeTag *string) (string, []inte
	return subQ.ToSql()
}

// We need to do some parsing to avoid systax error
// for some string chars
func ParseSearch(s string) string {
	s = strings.Replace(s, ":", "\\:", -1)
	return strings.Replace(s, " ", ":* & ", -1) + ":*"
}

func AddQueryElement(q template.URL, param, val string) template.URL {
	query, err := url.ParseQuery(string(q))
	if err != nil {