-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_search.go
146 lines (139 loc) · 4.03 KB
/
handle_search.go
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
package main
import (
"context"
"fmt"
"net/http"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
// handleSearch는 URL을 통해 query를 할 수 있게 해주는 함수입니다.
func handleSearch(w http.ResponseWriter, r *http.Request) {
token, err := GetTokenFromHeader(w, r)
if err != nil {
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
q := r.URL.Query()
itemType := q.Get("itemtype")
searchword := q.Get("searchword")
rootCategoryID := q.Get("rootcategoryid")
subCategoryID := q.Get("subcategoryid")
page := PageToString(q.Get("page"))
if page == "" {
page = "1"
}
type recipe struct {
Items []Item
Searchword string
ItemType string
RootCategoryID string
SubCategoryID string
TotalNum int64
CurrentPage int64
TotalPage int64
Pages []int64
Token
User User
Adminsetting Adminsetting
RootCategories []Category
SubCategories []Category
}
rcp := recipe{}
rcp.Searchword = searchword
rcp.ItemType = itemType
rcp.Token = token
//mongoDB client 연결
client, err := mongo.NewClient(options.Client().ApplyURI(*flagMongoDBURI))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(ctx)
err = client.Ping(ctx, readpref.Primary())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.CurrentPage = PageToInt(page)
rcp.RootCategories, err = GetRootCategories(client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if rootCategoryID != "" {
rcp.RootCategoryID = rootCategoryID
c, err := GetCategory(client, rootCategoryID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
searchword += " categories:" + c.Name
rcp.SubCategories, err = GetSubCategories(client, rootCategoryID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
if subCategoryID != "" {
rcp.SubCategoryID = subCategoryID
c, err := GetCategory(client, subCategoryID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
searchword += " categories:" + c.Name
}
totalPage, totalNum, items, err := SearchPage(client, itemType, searchword, rcp.CurrentPage, *flagPagenum)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.Items = items
rcp.User, err = GetUser(client, token.ID) // user 정보 가져옴
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
rcp.TotalNum = totalNum
rcp.TotalPage = totalPage
// 10 페이지씩 보이도록 Pages를 설정한다.
pageCount := (rcp.CurrentPage - 1) / 10
for i := 0; i < 10; i++ {
rcp.Pages = append(rcp.Pages, int64(i)+pageCount*10+1)
if rcp.Pages[i] == rcp.TotalPage {
break
}
}
adminsetting, err := GetAdminSetting(client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.Adminsetting = adminsetting
err = TEMPLATES.ExecuteTemplate(w, "index", rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func handleSearchSubmit(w http.ResponseWriter, r *http.Request) {
_, err := GetTokenFromHeader(w, r)
if err != nil {
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
itemType := r.FormValue("itemtype")
searchword := r.FormValue("searchword")
rootCategoryID := r.FormValue("searchbox-rootcategory-id")
subCategoryID := r.FormValue("searchbox-subcategory-id")
page := PageToString(r.FormValue("page"))
http.Redirect(w, r, fmt.Sprintf("/search?itemtype=%s&searchword=%s&page=%s&rootcategoryid=%s&subcategoryid=%s", itemType, searchword, page, rootCategoryID, subCategoryID), http.StatusSeeOther)
}