-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_cleanup.go
165 lines (160 loc) · 4.43 KB
/
handle_cleanup.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"context"
"fmt"
"net/http"
"strconv"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
func handleCleanUpDB(w http.ResponseWriter, r *http.Request) {
token, err := GetTokenFromHeader(w, r)
if err != nil {
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
w.Header().Set("Content-Type", "text/html")
type recipe struct {
Items []Item
Token
Adminsetting Adminsetting
User User
}
//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)
rcp := recipe{}
// 데이터가 모두 업로드되지 않은 아이템을 가져온다
rcp.Items, err = GetIncompleteItems(client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.Token = token
adminsetting, err := GetAdminSetting(client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.Adminsetting = adminsetting
user, err := GetUser(client, token.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.User = user
err = TEMPLATES.ExecuteTemplate(w, "cleanup-db", rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func handleCleanUpDBSubmit(w http.ResponseWriter, r *http.Request) {
_, err := GetTokenFromHeader(w, r)
if err != nil {
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
//checkbox의 개수를 받아온다.
itemNum, err := strconv.Atoi(r.FormValue("itemnum"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
itemsToDelete := make(map[string]string)
//checkbox의 개수만큼 포문을 돈다
for i := 0; i < itemNum; i++ {
//check되어 있는지 확인한다.
id := r.FormValue(fmt.Sprintf("checkbox%d", i+1))
if id == "" {
continue // check 안되어 있으면 넘어간다
} //check 되어 있으면 itemytpe을 가져와서 map을 채운다.
itemtype := r.FormValue(fmt.Sprintf("itemtype%d", i+1))
itemsToDelete[id] = itemtype
}
//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)
//삭제할 아이템의 리스트를 돌면서 삭제한다.
for id := range itemsToDelete {
err = RmData(client, id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = RmItem(client, id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
http.Redirect(w, r, "/cleanup-db", http.StatusSeeOther)
}
func handleCleanUpDBSuccess(w http.ResponseWriter, r *http.Request) {
token, err := GetTokenFromHeader(w, r)
if err != nil {
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
type recipe struct {
Token
Adminsetting Adminsetting
}
rcp := recipe{}
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
}
adminsetting, err := GetAdminSetting(client)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rcp.Adminsetting = adminsetting
w.Header().Set("Content-Type", "text/html")
err = TEMPLATES.ExecuteTemplate(w, "cleanup-db-success", rcp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}