Skip to content

Commit

Permalink
Rename package search to data as it now has many non-search methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Nov 28, 2021
1 parent 5775371 commit 9c7f0ab
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 114 deletions.
42 changes: 21 additions & 21 deletions cmd/dictmaker/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"

"github.com/go-chi/chi"
"github.com/knadh/dictmaker/internal/search"
"github.com/knadh/dictmaker/internal/data"
)

// handleGetConfig returns the language configuration.
Expand All @@ -19,10 +19,10 @@ func handleGetConfig(w http.ResponseWriter, r *http.Request) {
)

out := struct {
RootURL string `json:"root_url"`
Languages search.LangMap `json:"languages"`
Version string `json:"version"`
}{app.constants.RootURL, app.search.Langs, buildString}
RootURL string `json:"root_url"`
Languages data.LangMap `json:"languages"`
Version string `json:"version"`
}{app.constants.RootURL, app.data.Langs, buildString}

sendResponse(out, http.StatusOK, w)
}
Expand All @@ -33,7 +33,7 @@ func handleGetStats(w http.ResponseWriter, r *http.Request) {
app = r.Context().Value("app").(*App)
)

out, err := app.search.GetStats()
out, err := app.data.GetStats()
if err != nil {
sendErrorResponse(err.Error(), http.StatusInternalServerError, nil, w)
return
Expand All @@ -56,7 +56,7 @@ func handleInsertEntry(w http.ResponseWriter, r *http.Request) {
app = r.Context().Value("app").(*App)
)

var e search.Entry
var e data.Entry
if err := json.NewDecoder(r.Body).Decode(&e); err != nil {
sendErrorResponse(fmt.Sprintf("error parsing request: %v", err), http.StatusBadRequest, nil, w)
return
Expand All @@ -67,7 +67,7 @@ func handleInsertEntry(w http.ResponseWriter, r *http.Request) {
return
}

guid, err := app.search.InsertEntry(e)
guid, err := app.data.InsertEntry(e)
if err != nil {
sendErrorResponse(fmt.Sprintf("error inserting entry: %v", err), http.StatusInternalServerError, nil, w)
return
Expand All @@ -88,18 +88,18 @@ func handleUpdateEntry(w http.ResponseWriter, r *http.Request) {
guid = chi.URLParam(r, "guid")
)

var e search.Entry
var e data.Entry
if err := json.NewDecoder(r.Body).Decode(&e); err != nil {
sendErrorResponse(fmt.Sprintf("error parsing request: %v", err), http.StatusBadRequest, nil, w)
return
}

if err := app.search.UpdateEntry(guid, e); err != nil {
if err := app.data.UpdateEntry(guid, e); err != nil {
sendErrorResponse(fmt.Sprintf("error updating entry: %v", err), http.StatusInternalServerError, nil, w)
return
}

sendResponse(app.search.Langs, http.StatusOK, w)
sendResponse(app.data.Langs, http.StatusOK, w)
}

// handleDeleteEntry deletes a dictionary entry.
Expand All @@ -109,7 +109,7 @@ func handleDeleteEntry(w http.ResponseWriter, r *http.Request) {
guid = chi.URLParam(r, "guid")
)

if err := app.search.DeleteEntry(guid); err != nil {
if err := app.data.DeleteEntry(guid); err != nil {
sendErrorResponse(fmt.Sprintf("error deleting entry: %v", err), http.StatusInternalServerError, nil, w)
return
}
Expand All @@ -125,18 +125,18 @@ func handleAddRelation(w http.ResponseWriter, r *http.Request) {
toGuid = chi.URLParam(r, "toGuid")
)

var rel search.Relation
var rel data.Relation
if err := json.NewDecoder(r.Body).Decode(&rel); err != nil {
sendErrorResponse(fmt.Sprintf("error parsing request: %v", err), http.StatusBadRequest, nil, w)
return
}

if err := app.search.InsertRelation(fromGuid, toGuid, rel); err != nil {
if err := app.data.InsertRelation(fromGuid, toGuid, rel); err != nil {
sendErrorResponse(fmt.Sprintf("error updating relation: %v", err), http.StatusInternalServerError, nil, w)
return
}

sendResponse(app.search.Langs, http.StatusOK, w)
sendResponse(app.data.Langs, http.StatusOK, w)
}

// handleUpdateRelation updates a relation's properties.
Expand All @@ -146,18 +146,18 @@ func handleUpdateRelation(w http.ResponseWriter, r *http.Request) {
relID, _ = strconv.Atoi(chi.URLParam(r, "relID"))
)

var rel search.Relation
var rel data.Relation
if err := json.NewDecoder(r.Body).Decode(&rel); err != nil {
sendErrorResponse(fmt.Sprintf("error parsing request: %v", err), http.StatusBadRequest, nil, w)
return
}

if err := app.search.UpdateRelation(relID, rel); err != nil {
if err := app.data.UpdateRelation(relID, rel); err != nil {
sendErrorResponse(fmt.Sprintf("error updating relation: %v", err), http.StatusInternalServerError, nil, w)
return
}

sendResponse(app.search.Langs, http.StatusOK, w)
sendResponse(app.data.Langs, http.StatusOK, w)
}

// handleReorderRelations reorders the weights of the relation IDs in the given order.
Expand All @@ -175,7 +175,7 @@ func handleReorderRelations(w http.ResponseWriter, r *http.Request) {
return
}

if err := app.search.ReorderRelations(ids); err != nil {
if err := app.data.ReorderRelations(ids); err != nil {
sendErrorResponse(fmt.Sprintf("error reordering relations: %v", err), http.StatusInternalServerError, nil, w)
return
}
Expand All @@ -191,7 +191,7 @@ func handleDeleteRelation(w http.ResponseWriter, r *http.Request) {
toGuid = chi.URLParam(r, "toGuid")
)

if err := app.search.DeleteRelation(fromGuid, toGuid); err != nil {
if err := app.data.DeleteRelation(fromGuid, toGuid); err != nil {
sendErrorResponse(fmt.Sprintf("error deleting entry: %v", err), http.StatusInternalServerError, nil, w)
return
}
Expand All @@ -217,7 +217,7 @@ func adminPage(tpl string) http.HandlerFunc {
})
}

func validateEntry(e search.Entry) error {
func validateEntry(e data.Entry) error {
if strings.TrimSpace(e.Content) == "" {
return errors.New("invalid `content`.")
}
Expand Down
60 changes: 30 additions & 30 deletions cmd/dictmaker/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"strings"

"github.com/go-chi/chi"
"github.com/knadh/dictmaker/internal/search"
"github.com/knadh/dictmaker/internal/data"
"github.com/knadh/paginator"
)

Expand All @@ -23,19 +23,19 @@ const (

// Results represents a set of results.
type Results struct {
FromLang string `json:"-"`
ToLang string `json:"-"`
Entries []search.Entry `json:"entries"`
FromLang string `json:"-"`
ToLang string `json:"-"`
Entries []data.Entry `json:"entries"`

// Pagination fields.
paginator.Set
}

// Glossary represents a set of glossary words.
type Glossary struct {
FromLang string `json:"-"`
ToLang string `json:"-"`
Words []search.GlossaryWord `json:"entries"`
FromLang string `json:"-"`
ToLang string `json:"-"`
Words []data.GlossaryWord `json:"entries"`

// Pagination fields.
paginator.Set
Expand Down Expand Up @@ -94,7 +94,7 @@ func handleGetEntry(w http.ResponseWriter, r *http.Request) {
guid = chi.URLParam(r, "guid")
)

e, err := app.search.GetEntry(guid)
e, err := app.data.GetEntry(guid)
if err != nil {
if err == sql.ErrNoRows {
sendErrorResponse("entry not found", http.StatusBadRequest, nil, w)
Expand All @@ -105,10 +105,10 @@ func handleGetEntry(w http.ResponseWriter, r *http.Request) {
return
}

e.Relations = make(search.Entries, 0)
e.Relations = make(data.Entries, 0)

entries := search.Entries{e}
if err := entries.SearchAndLoadRelations(search.Query{}, app.queries.SearchRelations); err != nil {
entries := data.Entries{e}
if err := entries.SearchAndLoadRelations(data.Query{}, app.queries.SearchRelations); err != nil {
app.logger.Printf("error querying db for defs: %v", err)
sendErrorResponse(fmt.Sprintf("error loading relations: %v", err), http.StatusBadRequest, nil, w)
}
Expand All @@ -123,7 +123,7 @@ func handleGetParentEntries(w http.ResponseWriter, r *http.Request) {
guid = chi.URLParam(r, "guid")
)

out, err := app.search.GetParentEntries(guid)
out, err := app.data.GetParentEntries(guid)
if err != nil {
sendErrorResponse(err.Error(), http.StatusInternalServerError, nil, w)
return
Expand All @@ -134,7 +134,7 @@ func handleGetParentEntries(w http.ResponseWriter, r *http.Request) {

// doSearch is a helper function that takes an HTTP query context,
// gets search params from it, performs a search and returns results.
func doSearch(r *http.Request, app *App) (search.Query, *Results, error) {
func doSearch(r *http.Request, app *App) (data.Query, *Results, error) {
var (
fromLang = chi.URLParam(r, "fromLang")
toLang = chi.URLParam(r, "toLang")
Expand All @@ -151,47 +151,47 @@ func doSearch(r *http.Request, app *App) (search.Query, *Results, error) {

q, err := url.QueryUnescape(q)
if err != nil {
return search.Query{}, nil, fmt.Errorf("error parsing query: %v", err)
return data.Query{}, nil, fmt.Errorf("error parsing query: %v", err)
}

q = strings.TrimSpace(q)

if _, ok := app.search.Langs[fromLang]; !ok {
return search.Query{}, nil, errors.New("unknown `from` language")
if _, ok := app.data.Langs[fromLang]; !ok {
return data.Query{}, nil, errors.New("unknown `from` language")
}

if toLang == "*" {
toLang = ""
} else {
if _, ok := app.search.Langs[toLang]; !ok {
return search.Query{}, nil, errors.New("unknown `to` language")
if _, ok := app.data.Langs[toLang]; !ok {
return data.Query{}, nil, errors.New("unknown `to` language")
}
}

// Search query.
query := search.Query{
query := data.Query{
FromLang: fromLang,
ToLang: toLang,
Types: qp["type"],
Tags: qp["tag"],
Query: q,
Status: search.StatusEnabled,
Status: data.StatusEnabled,
Offset: pg.Offset,
Limit: pg.Limit,
}

if err = validateSearchQuery(query, app.search.Langs); err != nil {
if err = validateSearchQuery(query, app.data.Langs); err != nil {
return query, out, err
}

// HTTP response.
out = &Results{}
out.Page = pg.Page
out.PerPage = pg.PerPage
out.Entries = search.Entries{}
out.Entries = data.Entries{}

// Find entries matching the query.
res, total, err := app.search.Search(query)
res, total, err := app.data.Search(query)
if err != nil {
if err == sql.ErrNoRows {
return query, out, nil
Expand All @@ -206,11 +206,11 @@ func doSearch(r *http.Request, app *App) (search.Query, *Results, error) {
}

// Load relations into the matches.
if err := res.SearchAndLoadRelations(search.Query{
if err := res.SearchAndLoadRelations(data.Query{
ToLang: toLang,
Offset: pg.Offset,
Limit: pg.Limit,
Status: search.StatusEnabled,
Status: data.StatusEnabled,
}, app.queries.SearchRelations); err != nil {
app.logger.Printf("error querying db for defs: %v", err)
return query, nil, errors.New("error querying db for definitions")
Expand All @@ -219,7 +219,7 @@ func doSearch(r *http.Request, app *App) (search.Query, *Results, error) {
// Replace nulls with [].
for i := range res {
if res[i].Relations == nil {
res[i].Relations = search.Entries{}
res[i].Relations = data.Entries{}
}
}

Expand All @@ -236,10 +236,10 @@ func getGlossaryWords(lang, initial string, pg paginator.Set, app *App) (*Glossa
out := &Glossary{}
out.Page = pg.Page
out.PerPage = pg.PerPage
out.Words = []search.GlossaryWord{}
out.Words = []data.GlossaryWord{}

// Get glossary words.
res, total, err := app.search.GetGlossaryWords(lang, initial, pg.Offset, pg.Limit)
res, total, err := app.data.GetGlossaryWords(lang, initial, pg.Offset, pg.Limit)
if err != nil {
if err == sql.ErrNoRows {
return out, nil
Expand Down Expand Up @@ -332,8 +332,8 @@ func (p *pagination) GenerateNumbers() {
}

// validateSearchQuery does basic validation and sanity checks
// on search.Query (useful for params coming from the outside world).
func validateSearchQuery(q search.Query, langs search.LangMap) error {
// on data.Query (useful for params coming from the outside world).
func validateSearchQuery(q data.Query, langs data.LangMap) error {
if q.Query == "" {
return errors.New("empty search query")
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/dictmaker/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/jmoiron/sqlx"
"github.com/knadh/dictmaker/internal/search"
"github.com/knadh/dictmaker/internal/data"
"github.com/knadh/koanf"
"github.com/knadh/stuffbin"
)
Expand Down Expand Up @@ -109,9 +109,9 @@ func initAdminTemplates(path string) *template.Template {
return t
}

// loadTokenizerPlugin loads a tokenizer plugin that implements search.Tokenizer
// loadTokenizerPlugin loads a tokenizer plugin that implements data.Tokenizer
// from the given path.
func loadTokenizerPlugin(path string) (search.Tokenizer, error) {
func loadTokenizerPlugin(path string) (data.Tokenizer, error) {
plg, err := plugin.Open(path)
if err != nil {
return nil, fmt.Errorf("error loading tokenizer plugin '%s': %v", path, err)
Expand All @@ -122,7 +122,7 @@ func loadTokenizerPlugin(path string) (search.Tokenizer, error) {
return nil, fmt.Errorf("New() function not found in plugin '%s': %v", path, err)
}

f, ok := newFunc.(func() (search.Tokenizer, error))
f, ok := newFunc.(func() (data.Tokenizer, error))
if !ok {
return nil, fmt.Errorf("New() function is of invalid type in plugin '%s'", path)
}
Expand Down Expand Up @@ -182,12 +182,12 @@ func initHandlers(r *chi.Mux, app *App) {
}

// initLangs loads language configuration into a given *App instance.
func initLangs(ko *koanf.Koanf) search.LangMap {
out := make(search.LangMap)
func initLangs(ko *koanf.Koanf) data.LangMap {
out := make(data.LangMap)

// Language configuration.
for _, l := range ko.MapKeys("lang") {
lang := search.Lang{Types: make(map[string]string)}
lang := data.Lang{Types: make(map[string]string)}
if err := ko.UnmarshalWithConf("lang."+l, &lang, koanf.UnmarshalConf{Tag: "json"}); err != nil {
log.Fatalf("error loading languages: %v", err)
}
Expand Down

0 comments on commit 9c7f0ab

Please sign in to comment.