summaryrefslogtreecommitdiff
path: root/handlerFuncs.go
blob: dae3bfbc230281616b7e7c1690e509e3259e0f71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main

import (
	"fmt"
	"html/template"
	"net/http"
	"strconv"

	"github.com/meilisearch/meilisearch-go"
)

func searchHandler(w http.ResponseWriter, r *http.Request, index *meilisearch.Index, searchTemplate *template.Template) {
	r.ParseForm()
	fmt.Println(r.Form)
	if r.Form["query"] != nil || r.FormValue("query") != "" {
		//fmt.Println("query:", r.Form["query"])
		var pageNo int64
		var maxHits int64
		var sortBy string
		if r.Form["page"] != nil {
			convertedInt, _ := strconv.ParseInt(r.FormValue("page"), 10, 64)
			if convertedInt > 0 {
				pageNo = convertedInt
			} else {
				pageNo = 1
			}
		} else {
			pageNo = 1
		}
		if r.Form["maxHits"] != nil {
			convertedInt, _ := strconv.ParseInt(r.FormValue("maxHits"), 10, 64)
			if convertedInt > 0 {
				maxHits = convertedInt
			} else {
				maxHits = 20
			}
		} else {
			maxHits = 20
		}

		if r.Form["sort"] != nil {
			if r.FormValue("sort") == "subdesc" {
				sortBy = "submission_date_timestamp:desc"
			} else if r.FormValue("sort") == "subasc" {
				sortBy = "submission_date_timestamp:asc"
			} else if r.FormValue("sort") == "decasc" {
				sortBy = "decision_date_timestamp:asc"
			} else if r.FormValue("sort") == "decdesc" {
				sortBy = "decision_date_timestamp:desc"
			} else {
				sortBy = ""
			}

		} else {
			sortBy = ""
		}

		if maxHits > 100 {
			maxHits = 100
		}
		query := SearchQuery{
			Query:      r.FormValue("query"),
			MaxResults: maxHits,
			Page:       pageNo,
		}

		var res *meilisearch.SearchResponse

		fmt.Println("sort:", sortBy)

		if sortBy != "" {
			res, _ = index.Search(query.Query, &meilisearch.SearchRequest{
				HitsPerPage: query.MaxResults,
				Page:        query.Page,
				AttributesToRetrieve: []string{
					"title",
					"applicant",
					"submission_date",
					"predicates",
					"id",
				},
				AttributesToCrop:      []string{"full_text"},
				AttributesToHighlight: []string{"full_text"},
				HighlightPreTag:       "<mark>",
				HighlightPostTag:      "</mark>",
				Sort: []string{
					sortBy},
			})
		} else {
			res, _ = index.Search(query.Query, &meilisearch.SearchRequest{
				HitsPerPage: query.MaxResults,
				Page:        query.Page,
				AttributesToRetrieve: []string{
					"title",
					"applicant",
					"submission_date",
					"predicates",
					"id",
				},
				AttributesToCrop:      []string{"full_text"},
				AttributesToHighlight: []string{"full_text"},
				HighlightPreTag:       "<mark>",
				HighlightPostTag:      "</mark>",
			})
		}

		searchTemplate.Execute(w, SearchResponse{
			GlobalVars:    globalVariables,
			Success:       true,
			SearchResults: res.Hits,
			NumResults:    int(res.TotalHits) - (int(res.TotalPages)-int(res.Page))*int(res.HitsPerPage),
			TotalResults:  res.TotalHits,
			MoreResults:   res.Page < res.TotalPages,
			OriginalQuery: query,
			NumPages:      int(res.TotalPages),
			MaxResults:    maxHits,
			CurPage:       res.Page,
			ShowPrev:      res.Page > 1,
			PrevPage:      res.Page - 1,
			NextPage:      res.Page + 1,
			Sort:          r.FormValue("sort"),
		})

	} else {
		// Return empty search results
		searchTemplate.Execute(w, SearchResponse{
			GlobalVars:    globalVariables,
			Success:       true,
			SearchResults: []interface{}{},
			NumResults:    0,
			TotalResults:  0,
			MoreResults:   false,
			OriginalQuery: SearchQuery{},
			NumPages:      0,
			MaxResults:    0,
			CurPage:       0,
			ShowPrev:      false,
			PrevPage:      0,
			NextPage:      0,
		})
	}
}

func documentHandler510k(w http.ResponseWriter, r *http.Request, index *meilisearch.Index, template *template.Template) {
	r.ParseForm()
	fmt.Println(r.Form)
	var res interface{}
	var documentID string = r.FormValue("id")
	if r.Form["id"] != nil {
		index.GetDocument(documentID, &meilisearch.DocumentQuery{
			Fields: []string{
				"title",
				"applicant",
				"decision",
				"decision_date",
				"full_text",
				"id",
				"predicates",
				"submission_date",
				"contact",
				"STREET1",
				"STREET2",
				"CITY",
				"STATE",
				"ZIP",
				"COUNTRY_CODE",
				"postal_code",
				"REVIEWADVISECOMM",
				"PRODUCTCODE",
				"STATEORSUMM",
				"CLASSADVISECOMM",
				"SSPINDICATOR",
				"TYPE",
				"THIRDPARTY",
				"EXPEDITEDREVIEW",
			}}, &res)
		//fmt.Println(res)
		var year = documentID[1:3]

		template.Execute(w, DocumentResponse{
			GlobalVars:    globalVariables,
			SearchResults: res,
			SummaryPDF:    create_pdf_url(year, documentID),
		})
	} else {
		fmt.Println("No ID provided")
	}

}