package main

import (
	"github.com/labstack/echo"
	"net/http"
)

func setupRoutes(e *echo.Echo) {
	e.GET("/", route_main)
	e.GET("/search/:query", route_search)
}

func route_main(c echo.Context) error {
	return c.String(http.StatusOK, "You've reached /")
}

func route_search(c echo.Context) error {
	allResults := make(map[string][]SearchResult)
	query := c.Param("query")

	if len(query) < 5 {
		return c.String(http.StatusBadRequest, "Search query is too short")
	}

	for _, b := range BACKENDS {
		results, err := b.Search(query)
		if err != nil {
			log.Error(err)
			continue
		}
		allResults[b.Name()] = results
	}

	return c.JSON(http.StatusOK, allResults)
}

package main

import (
	"github.com/labstack/echo"
	"github.com/texttheater/golang-levenshtein/levenshtein"
	"net/http"
	"sort"
)

// So that we can sort
var query string

func setupRoutes(e *echo.Echo) {
	e.GET("/", route_main)
	e.GET("/search/:query", route_search)
}

func route_main(c echo.Context) error {
	return c.String(http.StatusOK, "You've reached /")
}

type ByLevenshteinDistance []SearchResult

func (r ByLevenshteinDistance) Len() int {
	return len(r)
}

func (r ByLevenshteinDistance) Swap(i int, j int) {
	r[i], r[j] = r[j], r[i]
}

func (r ByLevenshteinDistance) Less(i int, j int) bool {
	return levenshtein.DistanceForStrings([]rune(query), []rune(r[i].Title), levenshtein.DefaultOptions) < levenshtein.DistanceForStrings([]rune(query), []rune(r[j].Title), levenshtein.DefaultOptions)
}

func route_search(c echo.Context) error {
	allResults := make(map[string][]SearchResult)
	query = c.Param("query")

	if len(query) < 5 {
		return c.String(http.StatusBadRequest, "Search query is too short")
	}

	for _, b := range BACKENDS {
		results, err := b.Search(query)
		if err != nil {
			log.Error(err)
			continue
		}

		sort.Sort(ByLevenshteinDistance(results))

		allResults[b.Name()] = results
	}

	return c.JSON(http.StatusOK, allResults)
}

Sort search results based on Levenshtein distance from query
Sort search results based on Levenshtein distance from query
