-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
156 lines (150 loc) · 4.4 KB
/
main.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
package rest
import (
"encoding/json"
"fmt"
"github.com/go-martini/martini"
"github.com/jinzhu/gorm"
"github.com/martini-contrib/render"
"github.com/shlomimatichin/go-martini-pagination"
"io/ioutil"
"net/http"
"reflect"
)
func CreateRecordView(jsonRecord interface{}, construct interface{}) func(r render.Render, c martini.Context) {
return func(r render.Render, c martini.Context) {
returned, err := c.Invoke(construct)
if err != nil {
panic(fmt.Sprint("Unable to invoke Construct:", err))
}
id := returned[0].Int()
if id == 0 {
RenderErrorNotAllowed(r)
return
}
r.JSON(200, map[string]interface{}{"id": id})
}
}
func DeleteRecordView(viewParams Params) func(r render.Render, db *gorm.DB, c martini.Context) {
viewParams.sanity()
viewParams.sanityMustNotHaveToJSON()
viewParams.sanityMustNotHaveFilter()
viewParams.sanityMustNotHaveValid()
viewParams.sanityMustNotHaveField()
return func(r render.Render, db *gorm.DB, c martini.Context) {
zero := viewParams.zero()
if !FindRecordFromIDParameter(c, zero.Interface()) {
return
}
c.Map(zero.Interface())
if !viewParams.allowed(c) {
return
}
db.Delete(zero.Interface())
RenderResultOK(r)
}
}
func ListRecordsView(viewParams Params) func(p *pagination.Pagination, db *gorm.DB, c martini.Context) {
viewParams.sanity()
viewParams.sanityMustHaveToJSON()
viewParams.sanityMustNotHaveValid()
viewParams.sanityMustNotHaveField()
return func(p *pagination.Pagination, db *gorm.DB, c martini.Context) {
records := viewParams.slice()
viewParams.filter(c).Offset(int(p.Offset)).Limit(int(p.PerPage)).Find(records.Interface())
total := records.Elem().Len()
p.SetTotal(uint(total))
for i := 0; i < total; i++ {
record := records.Elem().Index(i)
c.Map(record.Addr().Interface())
p.Append(viewParams.toJSON(c))
}
}
}
func GetRecordView(viewParams Params) func(r render.Render, db *gorm.DB, c martini.Context) {
viewParams.sanity()
viewParams.sanityMustHaveToJSON()
viewParams.sanityMustNotHaveFilter()
viewParams.sanityMustNotHaveValid()
viewParams.sanityMustNotHaveField()
return func(r render.Render, db *gorm.DB, c martini.Context) {
record := viewParams.zero()
if !FindRecordFromIDParameter(c, record.Interface()) {
return
}
c.Map(record.Interface())
if !viewParams.allowed(c) {
return
}
r.JSON(200, viewParams.toJSON(c))
}
}
func ModifyRecordFieldView(viewParams Params) func(r render.Render, db *gorm.DB, req *http.Request, c martini.Context) {
viewParams.sanity()
viewParams.sanityMustNotHaveToJSON()
viewParams.sanityMustNotHaveFilter()
viewParams.sanityMustHaveField()
return func(r render.Render, db *gorm.DB, req *http.Request, c martini.Context) {
zero := viewParams.zero()
if !FindRecordFromIDParameter(c, zero.Interface()) {
return
}
c.Map(zero.Interface())
if !viewParams.allowed(c) {
return
}
body, err := ioutil.ReadAll(req.Body)
if err != nil {
panic("Unable to read body")
}
var value map[string]interface{}
err2 := json.Unmarshal(body, &value)
if err2 != nil {
RenderError(r, 422, fmt.Sprint("Unable to parse JSON:", err2))
return
}
if value["value"] == nil {
RenderError(r, 422, fmt.Sprint("JSON must contain field 'value'"))
return
}
field := zero.Elem().FieldByName(viewParams.Field)
switch field.Interface().(type) {
default:
panic(fmt.Sprint("Unknown field type:", field.Type()))
case float64:
if reflect.TypeOf(value["value"]).Kind() != reflect.Float64 {
RenderError(r, 422, fmt.Sprint("Value can not be parsed as float:", reflect.TypeOf(value["value"])))
return
}
final := value["value"].(float64)
c.Map(final)
if !viewParams.valid(c) {
return
}
field.SetFloat(final)
case int64:
if reflect.TypeOf(value["value"]).Kind() != reflect.Float64 {
RenderError(r, 422, fmt.Sprint("Value can not be parsed as int:", reflect.TypeOf(value["value"])))
return
}
final := int64(value["value"].(float64))
c.Map(final)
if !viewParams.valid(c) {
return
}
field.SetInt(final)
case string:
if reflect.TypeOf(value["value"]).Kind() != reflect.String {
RenderError(r, 422, fmt.Sprint("Value can not be parsed as string:", reflect.TypeOf(value["value"])))
return
}
final := value["value"].(string)
c.Map(final)
if !viewParams.valid(c) {
return
}
field.SetString(final)
}
db.Save(zero.Interface())
r.JSON(200, map[string]interface{}{"value": value["value"]})
}
}