diff options
author | navanchauhan <navanchauhan@gmail.com> | 2022-10-04 02:13:05 -0600 |
---|---|---|
committer | navanchauhan <navanchauhan@gmail.com> | 2022-10-04 02:13:05 -0600 |
commit | 0e2444e6afd9df38b740bd6fdcaff0f95321c325 (patch) | |
tree | 5fa2fc4b1c2d632c74ed76dbf49473dce18f78dd | |
parent | bec4e596415c623d7294ec87680bc029e407fc23 (diff) |
rewrite
-rw-r--r-- | index.html | 9 | ||||
-rw-r--r-- | main.go | 83 | ||||
-rw-r--r-- | results.gtpl | 34 | ||||
-rw-r--r-- | search.gtpl | 10 |
4 files changed, 115 insertions, 21 deletions
@@ -1,11 +1,13 @@ <h1>DogeKnows</h1> <form method="POST"> <label>Query:</label><br /> - <input type="text" name="query"><br /> + <input type="text" name="query" {{if .Success }} value="{{.OriginalQuery.Query}}" {{end}}><br /> + <input name="offset" value="{{.Offset}}" type="hidden"><br /> <input type="submit"> </form> {{if .Success}} <h2>Search Results</h2> +<p>Showing {{.NumResults}} of {{.TotalResults}}</p> <table border="1"> <tr> <th>510(k) Number</th> @@ -27,4 +29,7 @@ </tr> {{ end }} </table> -{{end}}
\ No newline at end of file + {{ if .MoreResults }} + <p>Load More</p> + {{ end }} +{{end}} @@ -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 @@ +<!DOCTYPE html> +<html> +<body> +<h1>Search Results</h1> +<p>Showing results for <b>{{.OriginalQuery.Query}}</b></p> +<p>Go <a href="/">home</a></p> +<p>Showing {{.NumResults}} of {{.TotalResults}}</p> + <table border="1"> + <tr> + <th>510(k) Number</th> + <th>Title</th> + <th>Applicant</th> + <th>Submission Date</th> + <th>Predicates</th> + </tr> + {{ range .SearchResults }} + <tr> + <td><a href="https://www.accessdata.fda.gov/scripts/cdrh/cfdocs/cfPMN/pmn.cfm?ID={{.id}}">{{ .id }}</a></td> + <td>{{ .title }}</td> + <td>{{ .applicant }}</td> + <td>{{ .submission_date }}</td> + <td>{{ range .predicates}} + <a href="https://www.accessdata.fda.gov/scripts/cdrh/cfdocs/cfPMN/pmn.cfm?ID={{.}}">{{ . }}</a>, + {{ end }} + </td> + </tr> + {{ end }} + </table> + {{ if .MoreResults }} + <a href="/search?query={{.OriginalQuery.Query}}&offset={{.LastOffset}}"> <p>Previous Page</p></a> + <a href="/search?query={{.OriginalQuery.Query}}&offset={{.Offset}}"> <p>Next Page</p></a> + {{ end }} +</body> +</html>
\ 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 @@ +<!DOCTYPE html> +<html> +<body> + <h1>DogeKnows</h1> + <form action="/search" method="GET"> + <input type="text" name="query" value="{{.OriginalQuery.Query}}" placeholder="Search Query" spellcheck="false"> + <input type="submit"> + </form> +</body> +</html>
\ No newline at end of file |