From 70f92f299e32b857f67d630cf886086bdb0037d9 Mon Sep 17 00:00:00 2001 From: Ambrose Chua Date: Sun, 22 Nov 2020 16:59:41 +0800 Subject: [PATCH] Initial scaffold for search --- app.go | 12 ++++++++---- search.go | 11 +++++++++++ template.go | 11 +++++++++-- templates/search.json | 2 +- 4 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 search.go diff --git a/app.go b/app.go index db928e3..3c9a0dd 100644 --- a/app.go +++ b/app.go @@ -53,8 +53,8 @@ type appRequest struct { } type appSearch struct { - App Datetime - Req map[string]string + App Datetime + Search []*data.City } // index handles all incoming page requests @@ -111,8 +111,12 @@ func (app Datetime) search(w http.ResponseWriter, req *http.Request) { } // TODO: do search - search := map[string]string{ - "hi": "hello", + query := req.URL.Query() + search, err := FullSearchCities(app.cities, query.Get("zone")) + if err != nil { + l.Error("search failed", zap.Error(err)) + app.error(HTTPError{http.StatusInternalServerError, err}, w, req) + return } l.Debug("rendering template", zap.Reflect("search", search)) diff --git a/search.go b/search.go new file mode 100644 index 0000000..360ccbc --- /dev/null +++ b/search.go @@ -0,0 +1,11 @@ +package main + +import ( + "github.com/serverwentdown/datetime.link/data" +) + +// FullSearchCities uses a very basic iterative method to search for a city +// with the given string +func FullSearchCities(cities map[string]*data.City, zone string) ([]*data.City, error) { + return nil, nil +} diff --git a/template.go b/template.go index 266a914..093cd61 100644 --- a/template.go +++ b/template.go @@ -19,13 +19,20 @@ func (app Datetime) loadTemplate(name string, w http.ResponseWriter, req *http.R accept := req.Header.Get("Accept") tmpl, contentType, acceptable := app.chooseTemplate(accept, name) if !acceptable { - app.simpleError(HTTPError{http.StatusNotAcceptable, nil}, w, req) + if name == "error" { + app.simpleError(HTTPError{http.StatusNotAcceptable, nil}, w, req) + return nil + } + app.error(HTTPError{http.StatusNotAcceptable, nil}, w, req) return nil } if tmpl == nil { err := fmt.Errorf("%w \"%s\" for \"%s\"", ErrTemplateNotFound, name, accept) l.Warn("unable to find template", zap.Error(err), zap.String("name", name), zap.String("accept", accept)) - app.simpleError(HTTPError{http.StatusNotAcceptable, err}, w, req) + if name == "error" { + app.simpleError(HTTPError{http.StatusNotAcceptable, err}, w, req) + } + app.error(HTTPError{http.StatusNotAcceptable, err}, w, req) //app.simpleError(HTTPError{http.StatusInternalServerError, ErrNoTemplate}, w, req) return nil } diff --git a/templates/search.json b/templates/search.json index 7e4faff..b7cd63d 100644 --- a/templates/search.json +++ b/templates/search.json @@ -1 +1 @@ -{{.Req | jsonMarshal | thisIsSafe }} +{{.Search | jsonMarshal | thisIsSafe }}