-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem_1.go
389 lines (343 loc) · 9.21 KB
/
system_1.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package main
import (
"database/sql"
"fmt"
"html/template"
"net/http"
"strconv"
"github.com/gorilla/mux"
_ "github.com/mattn/go-sqlite3"
)
type Schedule struct {
Id int
Month int
Date int
Day string
EventName string
StartHour int
StartMin int
EndHour int
EndMin int
Memo string
Record string
}
func MakeTwoDigit(num int) string {
if num < 10 {
return "0" + strconv.Itoa(num)
}
return strconv.Itoa(num)
}
func MakeId(month int, date int, start_hour int, start_min int) int {
str_month := MakeTwoDigit(month)
str_date := MakeTwoDigit(date)
str_start_hour := MakeTwoDigit(start_hour)
str_start_min := MakeTwoDigit(start_min)
str_id := str_month + str_date + str_start_hour + str_start_min
id, err := strconv.Atoi(str_id)
if err != nil {
fmt.Printf("cannot make id\n")
}
return id
}
func mainHandler(w http.ResponseWriter, r *http.Request) {
// tmpl := template.Must(template.ParseFiles("index.html"))
if r.Method == "POST" {
// get input
// str_month := r.FormValue("month") // 05
// str_date := r.FormValue("date") // 02
// day := r.FormValue("day") // mon, tue, wed, thr, fri
// event_name := r.FormValue("event")
// str_start_hour := r.FormValue("start_hour") // 10
// str_start_min := r.FormValue("start_min") // 00
// str_end_hour := r.FormValue("end_hour") // 20
// str_end_min := r.FormValue("end_min") //00
// memo := r.FormValue("memo")
// record := r.FormValue("record")
action := r.FormValue("action")
switch action {
case "add":
// fmt.Fprintf(w, "Add action selected")
addHandler(w, r)
case "search":
// fmt.Fprintf(w, "Search action selected")
searchHandler(w,r)
default:
fmt.Fprintf(w, "Unknown action")
}
}
}
func addHandler(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("index.html"))
var errorMsgs []string
var scheduleList []Schedule
str_month := r.FormValue("month") // 05
str_date := r.FormValue("date") // 02
day := r.FormValue("day") // mon, tue, wed, thr, fri
event_name := r.FormValue("event")
str_start_hour := r.FormValue("start_hour") // 10
str_start_min := r.FormValue("start_min") // 00
str_end_hour := r.FormValue("end_hour") // 20
str_end_min := r.FormValue("end_min") //00
memo := r.FormValue("memo")
record := r.FormValue("record")
// check input type
month, err := strconv.Atoi(str_month)
if err != nil {
errorMsgs = append(errorMsgs, "month must be number")
}
date, err := strconv.Atoi(str_date)
if err != nil {
errorMsgs = append(errorMsgs, "date must be number")
}
start_hour := 0
if str_start_hour != "" {
start_hour, err = strconv.Atoi(str_start_hour)
if err != nil {
errorMsgs = append(errorMsgs, "start hour must be number")
}
}
start_min := 0
if str_start_min != "" {
start_min, err = strconv.Atoi(str_start_min)
if err != nil {
errorMsgs = append(errorMsgs, "start minute must be number")
}
}
end_hour := 0
if str_end_hour != "" {
end_hour, err = strconv.Atoi(str_end_hour)
if err != nil {
errorMsgs = append(errorMsgs, "end hour must be number")
}
}
end_min := 0
if str_end_min != "" {
end_min, err = strconv.Atoi(str_end_min)
if err != nil {
errorMsgs = append(errorMsgs, "end minute must be number")
}
}
// Check if there are any error messages
if len(errorMsgs) > 0 {
// If there are errors, render the template with the error messages
tmpl.Execute(w, struct {
ScheduleList []Schedule
ErrorMsgs []string
}{
ScheduleList: scheduleList,
ErrorMsgs: errorMsgs,
})
return
}
db, err := sql.Open("sqlite3", "database.db")
if err != nil {
fmt.Printf("cannot open database: %s\n", err)
}
defer db.Close()
_, err = db.Exec(`INSERT INTO schedule (id, month, date, day, eventName, startHour, startMin, endHour, endMin, memo, record) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`,
MakeId(month, date, start_hour, start_min),
month,
date,
day,
event_name,
start_hour,
start_min,
end_hour,
end_min,
memo,
record)
if err != nil {
fmt.Printf("cannot insert entry into database: %s\n", err)
}
rows, err := db.Query(`SELECT id, month, date, day, eventName, startHour, startMin, endHour, endMin, memo, record FROM schedule ORDER BY id;`)
if err != nil {
fmt.Printf("cannot query database: %s\n", err)
}
defer rows.Close()
scheduleList = []Schedule{}
for rows.Next() {
var s Schedule
err := rows.Scan(&s.Id, &s.Month, &s.Date, &s.Day, &s.EventName, &s.StartHour, &s.StartMin, &s.EndHour, &s.EndMin, &s.Memo, &s.Record)
if err != nil {
fmt.Printf("cannot scan row: %s\n", err)
}
scheduleList = append(scheduleList, s)
}
for i, v := range scheduleList {
fmt.Println(i, v)
}
// Render the template with the schedule list and error messages
tmpl.Execute(w, struct {
ScheduleList []Schedule
ErrorMsgs []string
}{
ScheduleList: scheduleList,
ErrorMsgs: nil,
})
}
func searchHandler(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("index.html"))
var errorMsgs []string
var scheduleList []Schedule
if r.Method == "POST" {
// get input
str_month := r.FormValue("month") // 05
str_date := r.FormValue("date") // 02
day := r.FormValue("day") // mon, tue, wed, thr, fri
event_name := r.FormValue("event")
str_start_hour := r.FormValue("start_hour") // 10
str_start_min := r.FormValue("start_min") // 00
str_end_hour := r.FormValue("end_hour") // 20
str_end_min := r.FormValue("end_min") //00
memo := r.FormValue("memo")
record := r.FormValue("record")
query := "SELECT * FROM schedule WHERE 1=1"
var args []interface{}
if str_month != "" {
month, err := strconv.Atoi(str_month)
if err != nil {
errorMsgs = append(errorMsgs, "month must be number")
}
query += " AND month = ?"
args = append(args, month)
}
if str_date != "" {
date, err := strconv.Atoi(str_date)
if err != nil {
errorMsgs = append(errorMsgs, "date must be number")
}
query += " AND date = ?"
args = append(args, date)
}
if day != "" {
query += " AND day = ?"
args = append(args, day)
}
if event_name != "" {
query += " AND event_name = ?"
args = append(args, event_name)
}
if str_start_hour != "" {
start_hour, err := strconv.Atoi(str_start_hour)
if err != nil {
errorMsgs = append(errorMsgs, "start_hour must be number")
}
query += " AND start_hour = ?"
args = append(args, start_hour)
}
if str_start_min != "" {
start_min, err := strconv.Atoi(str_start_min)
if err != nil {
errorMsgs = append(errorMsgs, "start_min must be number")
}
query += " AND start_min = ?"
args = append(args, start_min)
}
if str_end_hour != "" {
end_hour, err := strconv.Atoi(str_end_hour)
if err != nil {
errorMsgs = append(errorMsgs, "end_hour must be number")
}
query += " AND end_hour = ?"
args = append(args, end_hour)
}
if str_end_min != "" {
end_min, err := strconv.Atoi(str_end_min)
if err != nil {
errorMsgs = append(errorMsgs, "end_min must be number")
}
query += " AND end_min = ?"
args = append(args, end_min)
}
if memo != "" {
query += " AND memo = ?"
args = append(args, memo)
}
if record != "" {
query += " AND record = ?"
args = append(args, record)
}
// Check if there are any error messages
if len(errorMsgs) > 0 {
// If there are errors, render the template with the error messages
tmpl.Execute(w, struct {
ScheduleList []Schedule
ErrorMsgs []string
}{
ScheduleList: scheduleList,
ErrorMsgs: errorMsgs,
})
return
}
db, err := sql.Open("sqlite3", "database.db")
if err != nil {
fmt.Printf("cannot open database: %s\n", err)
}
defer db.Close()
// TODO: order elements
rows, err := db.Query(query, args...)
if err != nil {
fmt.Printf("cannot query database: %s\n", err)
}
defer rows.Close()
scheduleList = []Schedule{}
for rows.Next() {
var s Schedule
err := rows.Scan(&s.Id, &s.Month, &s.Date, &s.Day, &s.EventName, &s.StartHour, &s.StartMin, &s.EndHour, &s.EndMin, &s.Memo, &s.Record)
if err != nil {
fmt.Printf("cannot scan row: %s\n", err)
}
scheduleList = append(scheduleList, s)
}
}
for i, v := range scheduleList {
fmt.Println(i, v)
}
// Render the template with the schedule list and error messages
tmpl.Execute(w, struct {
ScheduleList []Schedule
ErrorMsgs []string
}{
ScheduleList: scheduleList,
ErrorMsgs: nil,
})
}
func initializeDatabase() error {
db, err := sql.Open("sqlite3", "database.db")
if err != nil {
return err
}
defer db.Close()
// query that make schedule table
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS schedule (
id INTEGER PRIMARY KEY,
month INTEGER,
date INTEGER,
day TEXT,
eventName TEXT,
startHour INTEGER,
startMin INTEGER,
endHour INTEGER,
endMin INTEGER,
memo TEXT,
record TEXT
);
`)
if err != nil {
return err
}
return nil
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/", mainHandler)
// initialize database
if err := initializeDatabase(); err != nil {
fmt.Println("initializeDatabase: ", err)
return
}
http.Handle("/", r)
fmt.Println("boot server")
http.ListenAndServe(":8080", nil)
}