From 0e2444e6afd9df38b740bd6fdcaff0f95321c325 Mon Sep 17 00:00:00 2001 From: navanchauhan Date: Tue, 4 Oct 2022 02:13:05 -0600 Subject: rewrite --- index.html | 9 +++++-- main.go | 83 ++++++++++++++++++++++++++++++++++++++++++++++-------------- results.gtpl | 34 +++++++++++++++++++++++++ search.gtpl | 10 ++++++++ 4 files changed, 115 insertions(+), 21 deletions(-) create mode 100644 results.gtpl create mode 100644 search.gtpl diff --git a/index.html b/index.html index 0759bd3..2c2c279 100644 --- a/index.html +++ b/index.html @@ -1,11 +1,13 @@

DogeKnows


-
+
+
{{if .Success}}

Search Results

+

Showing {{.NumResults}} of {{.TotalResults}}

@@ -27,4 +29,7 @@ {{ end }}
510(k) Number
-{{end}} \ No newline at end of file + {{ if .MoreResults }} +

Load More

+ {{ end }} +{{end}} diff --git a/main.go b/main.go index f0282e3..44f5bdc 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,10 @@ package main import ( "fmt" "html/template" + "math" "net/http" "os" + "strconv" "github.com/joho/godotenv" "github.com/meilisearch/meilisearch-go" @@ -13,11 +15,23 @@ import ( type SearchQuery struct { Query string MaxResults int64 + Offset int64 } type SearchResponse struct { Success bool SearchResults []interface{} + NumResults int + TotalResults int64 + MoreResults bool + OriginalQuery SearchQuery + Offset int64 + LastOffset int64 + NumPages int +} + +func pageCount(total int, perPage int) int { + return int(math.Ceil(float64(total) / float64(perPage))) } func main() { @@ -38,30 +52,61 @@ func main() { index := client.Index("fda510k") - tmpl := template.Must(template.ParseFiles("index.html")) - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - if r.Method != http.MethodPost { - tmpl.Execute(w, nil) - return - } - query := SearchQuery{ - Query: r.FormValue("query"), - MaxResults: 100, - } + t, _ := template.ParseFiles("search.gtpl") + t.Execute(w, nil) + }) - res, err := index.Search(query.Query, &meilisearch.SearchRequest{ - Limit: query.MaxResults, - }) + searchResTemplate := template.Must(template.ParseFiles("results.gtpl")) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + http.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) { + r.ParseForm() + fmt.Println(r.Form) + if r.Form["query"] != nil || r.FormValue("query") != "" { + fmt.Println("query:", r.Form["query"]) + var myOffset int64 + if r.Form["offset"] != nil { + offset, _ := strconv.ParseInt(r.FormValue("offset"), 10, 64) + myOffset = offset + if offset < 0 { + myOffset = 0 + } + } else { + offset := int64(0) + myOffset = offset + } + query := SearchQuery{ + Query: r.FormValue("query"), + MaxResults: 100, + Offset: myOffset, + } + + res, err := index.Search(query.Query, &meilisearch.SearchRequest{ + Limit: query.MaxResults, + Offset: query.Offset, + }) - fmt.Println(res.Hits) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } - tmpl.Execute(w, SearchResponse{Success: true, SearchResults: res.Hits}) + numPages := pageCount(int(res.EstimatedTotalHits), int(query.MaxResults)) + + searchResTemplate.Execute(w, SearchResponse{ + Success: true, + SearchResults: res.Hits, + NumResults: len(res.Hits) + int(query.Offset), + TotalResults: res.EstimatedTotalHits, + MoreResults: res.EstimatedTotalHits > query.MaxResults, + OriginalQuery: query, + Offset: query.Offset + query.MaxResults, + LastOffset: query.Offset - query.MaxResults, + NumPages: numPages, + }) + } else { + fmt.Println("query is empty") + } }) fmt.Println("Listening on port 8080") diff --git a/results.gtpl b/results.gtpl new file mode 100644 index 0000000..39e2a32 --- /dev/null +++ b/results.gtpl @@ -0,0 +1,34 @@ + + + +

Search Results

+

Showing results for {{.OriginalQuery.Query}}

+

Go home

+

Showing {{.NumResults}} of {{.TotalResults}}

+ + + + + + + + + {{ range .SearchResults }} + + + + + + + + {{ end }} +
510(k) NumberTitleApplicantSubmission DatePredicates
{{ .id }}{{ .title }}{{ .applicant }}{{ .submission_date }}{{ range .predicates}} + {{ . }}, + {{ end }} +
+ {{ if .MoreResults }} +

Previous Page

+

Next Page

+ {{ end }} + + \ No newline at end of file diff --git a/search.gtpl b/search.gtpl new file mode 100644 index 0000000..0b83191 --- /dev/null +++ b/search.gtpl @@ -0,0 +1,10 @@ + + + +

DogeKnows

+
+ + +
+ + \ No newline at end of file -- cgit v1.2.3