/
belt.go
169 lines (143 loc) · 3.9 KB
/
belt.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
package belt
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"time"
"github.com/jinzhu/gorm"
"goji.io/pat"
"vallon.me/shortening"
)
type BeltCtx struct {
*Belt
db *gorm.DB
}
func (ctx *BeltCtx) Save() error {
if res := ctx.db.Save(&ctx.Belt); res.Error != nil {
return res.Error
}
return nil
}
func (ctx *BeltCtx) SaveWithOptions(opts []Option) error {
for _, o := range opts {
if o.ID != 0 {
for _, beltopt := range ctx.Belt.Options {
if beltopt.ID == o.ID {
if res := ctx.db.Model(&beltopt).Update(&o); res.Error != nil {
return res.Error
}
log.Println(beltopt)
break
}
}
} else {
if res := ctx.db.Model(&ctx.Belt).Association("Options").Append(o); res.Error != nil {
return res.Error
}
}
}
return ctx.db.Save(&ctx.Belt).Error
}
func Context(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
beltID, err := shortening.Decode([]byte(pat.Param(r, "beltid")))
if err != nil {
log.Println(err)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
db, ok := r.Context().Value("database").(*gorm.DB)
if !ok {
log.Println("could not find database in context")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
var b Belt
if res := db.Set("gorm:auto_preload", true).First(&b, beltID); res.Error != nil {
log.Println(err)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
ctx := context.WithValue(r.Context(), "beltctx", &BeltCtx{&b, db})
h.ServeHTTP(w, r.WithContext(ctx))
}
return http.HandlerFunc(fn)
}
func Edit(w http.ResponseWriter, r *http.Request) {
belt, ok := r.Context().Value("beltctx").(*BeltCtx)
if !ok {
log.Println("could not find belt in context")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if err := editTmpl.ExecuteTemplate(w, "edit.html", belt); err != nil {
log.Println(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
func SaveEdit(w http.ResponseWriter, r *http.Request) {
belt, ok := r.Context().Value("beltctx").(*BeltCtx)
if !ok {
log.Println("could not find belt in context")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if err := r.ParseMultipartForm(0); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
endTime, err := time.Parse(time.RFC3339Nano, r.FormValue("endTime"))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
belt.Title = r.FormValue("title")
belt.Message = strings.Replace(r.FormValue("description"), "\r\n", "\n", -1)
belt.EndTime = endTime
opts, err := getOptions(r.MultipartForm.Value)
if err != nil {
log.Println(err)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
if err := belt.SaveWithOptions(opts); err != nil {
log.Println(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, `{"ok": true}`)
}
func getOptions(form map[string][]string) ([]Option, error) {
ids, ok1 := form["option[id][]"]
names, ok2 := form["option[name][]"]
imgs, ok3 := form["option[img][]"]
if (!ok1 || !ok2 || !ok3) || len(ids) != len(names) || len(ids) != len(imgs) {
return nil, errors.New("bad option data")
}
opts := make([]Option, len(ids))
for i, id := range ids {
n, err := strconv.Atoi(id)
if err != nil {
return nil, err
}
if 0 < n {
opts[i].ID = uint(n)
}
}
for i, name := range names {
opts[i].Name = name
}
for i, img := range imgs {
opts[i].Image = img
}
return opts, nil
}