From 83b48f3e8833c2f693df04ef58ca57afa76dc2fa Mon Sep 17 00:00:00 2001 From: navanchauhan Date: Sun, 16 Oct 2022 16:46:48 -0600 Subject: new ui --- main.go | 69 ++++++++++++++++++++++++++++++++++++++++ templates/home.html | 38 ++++++++++++++++++++++ templates/search_results.html | 73 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 180 insertions(+) create mode 100644 templates/home.html create mode 100644 templates/search_results.html diff --git a/main.go b/main.go index 6302b6a..a41f774 100644 --- a/main.go +++ b/main.go @@ -57,6 +57,11 @@ func main() { t.Execute(w, nil) }) + http.HandleFunc("/2.0/", func(w http.ResponseWriter, r *http.Request) { + t, _ := template.ParseFiles("templates/home.html") + t.Execute(w, nil) + }) + funcMap := template.FuncMap{ "unescapeHTML": func(s string) template.HTML { return template.HTML(s) @@ -66,11 +71,75 @@ func main() { //searchResTemplate := template.Must(template.ParseFiles("results.gtpl")) searchResTemplate := template.Must(template.New("results.gtpl").Funcs(funcMap).ParseFiles("results.gtpl")) + // v2.0 UI + searchResultsTemplate2 := template.Must(template.New("search_results.html").Funcs(funcMap).ParseFiles("templates/search_results.html")) + if err != nil { fmt.Println("Error parsing template") os.Exit(1) } + http.HandleFunc("/2.0/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, + AttributesToRetrieve: []string{ + "title", + "applicant", + "submission_date", + "predicates", + "id", + }, + AttributesToCrop: []string{"full_text"}, + AttributesToHighlight: []string{"full_text"}, + HighlightPreTag: "", + HighlightPostTag: "", + }) + + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + numPages := pageCount(int(res.EstimatedTotalHits), int(query.MaxResults)) + + searchResultsTemplate2.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") + } + }) + http.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) { r.ParseForm() fmt.Println(r.Form) diff --git a/templates/home.html b/templates/home.html new file mode 100644 index 0000000..35e3ff6 --- /dev/null +++ b/templates/home.html @@ -0,0 +1,38 @@ + + + + + + DogeKnows! + + + + +
+
+

+ DogeKnows +

+

+ Full-text search FDA 510(k) database +

+
+
+
+
+
+
+
+ + + + +
+
+ +
+
+
+
+ + \ No newline at end of file diff --git a/templates/search_results.html b/templates/search_results.html new file mode 100644 index 0000000..d6b65e4 --- /dev/null +++ b/templates/search_results.html @@ -0,0 +1,73 @@ + + + + + + {{.OriginalQuery.Query}} - DogeKnows + + + + +
+
+

+ DogeKnows +

+

+ Full-text search FDA 510(k) database +

+
+
+
+
+
+
+
+
+ + + + +
+
+ +
+
+
+
+

Showing {{.NumResults}} of {{.TotalResults}} (Estimated)

+
+
+
+
+ + + + + + + + + + {{ range .SearchResults }} + + + + + + + + + {{ end }} +
510(k) NumberTitleApplicantHit DetailsSubmission DatePredicates
{{ .id }}{{ .title }}{{ .applicant }}{{unescapeHTML ._formatted.full_text}}{{ .submission_date }}{{ range .predicates}} + {{ . }}, + {{ end }} +
+
+
+ {{ if .MoreResults }} +

Previous Page

+

Next Page

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