forked from orloc/goking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschedule.go
192 lines (148 loc) · 4.61 KB
/
schedule.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
package main
import (
"github.com/asaskevich/govalidator"
"net/http"
"time"
"encoding/json"
)
type ScheduleIdentity struct {
ID int64 `json:"id" valid:"required"`
}
type Schedule struct {
ScheduleIdentity
Name string `json:"name"`
Mon bool `json:"mon"`
Tue bool `json:"tue"`
Wed bool `json:"wed"`
Thu bool `json:"thu"`
Fri bool `json:"fri"`
Sat bool `json:"sat"`
Sun bool `json:"sun"`
StartTime time.Time `json:"start_time" valid:"required"`
EndTime time.Time `json:"end_time" valid:"required"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt time.Time `json:"deleted_at"`
}
func (schedule *Schedule) MatchesNow() bool {
nowTime := time.Now()
normalizedTime := time.Date(1970, 1, 1, nowTime.Hour(), nowTime.Minute(), nowTime.Second(), nowTime.Nanosecond(), nowTime.Location())
if schedule.StartTime.Before(normalizedTime) && schedule.EndTime.After(normalizedTime) {
switch nowTime.Weekday() {
case time.Monday:
return schedule.Mon == true
case time.Tuesday:
return schedule.Tue == true
case time.Wednesday:
return schedule.Wed == true
case time.Thursday:
return schedule.Thu == true
case time.Friday:
return schedule.Fri == true
case time.Saturday:
return schedule.Sat == true
case time.Sunday:
return schedule.Sun == true
}
}
return false
}
type ScheduleRequest struct {
Name string `json:"name"`
Mon bool `json:"mon"`
Tue bool `json:"tue"`
Wed bool `json:"wed"`
Thu bool `json:"thu"`
Fri bool `json:"fri"`
Sat bool `json:"sat"`
Sun bool `json:"sun"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
}
func (h *DBHandler) schedulesIndexHandler(rw http.ResponseWriter, req *http.Request) {
page := getPage(req) - 1
perPage := getPerPage(req)
offset := perPage * page
var schedules []Schedule
h.db.Limit(perPage).Offset(offset).Find(&schedules)
if schedules == nil {
h.r.JSON(rw, http.StatusOK, make([]int64, 0))
} else {
var count int
h.db.Table("schedules").Count(&count)
vals := make([]interface{}, len(schedules))
for i, v := range schedules {
vals[i] = v
}
resp := getResponse(vals, count)
h.r.JSON(rw, http.StatusOK, &resp)
}
}
func (h *DBHandler) scheduleShowHandler(rw http.ResponseWriter, req *http.Request) {
id := getId(req)
schedule := Schedule{}
h.db.First(&schedule, id)
h.r.JSON(rw, http.StatusOK, &schedule)
}
func (h *DBHandler) scheduleCreateHandler(rw http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(req.Body)
createSchedule := ScheduleRequest{}
err := decoder.Decode(&createSchedule)
if err != nil {
h.r.JSON(rw, http.StatusBadRequest, map[string]string{"error":err.Error()})
return
}
schedule := Schedule{}
schedule.Name = createSchedule.Name
schedule.Mon = createSchedule.Mon
schedule.Tue = createSchedule.Tue
schedule.Wed = createSchedule.Wed
schedule.Thu = createSchedule.Thu
schedule.Fri = createSchedule.Fri
schedule.Sat = createSchedule.Sat
schedule.Sun = createSchedule.Sun
schedule.StartTime = createSchedule.StartTime
schedule.EndTime = createSchedule.EndTime
_, err = govalidator.ValidateStruct(&schedule)
if err != nil {
h.r.JSON(rw, http.StatusBadRequest, map[string]string{"error":err.Error()})
return
}
h.db.Save(&schedule)
h.r.JSON(rw, http.StatusOK, &schedule)
}
func (h *DBHandler) scheduleUpdateHandler(rw http.ResponseWriter, req *http.Request) {
id := getId(req)
decoder := json.NewDecoder(req.Body)
updateSchedule := ScheduleRequest{}
err := decoder.Decode(&updateSchedule)
if err != nil {
h.r.JSON(rw, http.StatusBadRequest, map[string]string{"error":err.Error()})
return
}
schedule := Schedule{}
h.db.First(&schedule, id)
schedule.Name = updateSchedule.Name
schedule.Mon = updateSchedule.Mon
schedule.Tue = updateSchedule.Tue
schedule.Wed = updateSchedule.Wed
schedule.Thu = updateSchedule.Thu
schedule.Fri = updateSchedule.Fri
schedule.Sat = updateSchedule.Sat
schedule.Sun = updateSchedule.Sun
schedule.StartTime = updateSchedule.StartTime
schedule.EndTime = updateSchedule.EndTime
_, err = govalidator.ValidateStruct(&schedule)
if err != nil {
h.r.JSON(rw, http.StatusBadRequest, map[string]string{"error":err.Error()})
return
}
h.db.Save(&schedule)
h.r.JSON(rw, http.StatusOK, &schedule)
}
func (h *DBHandler) scheduleDeleteHandler(rw http.ResponseWriter, req *http.Request) {
id := getId(req)
schedule := Schedule{}
h.db.Delete(&schedule, id)
h.r.JSON(rw, http.StatusOK, &schedule)
}