/
category.go
195 lines (128 loc) · 4.16 KB
/
category.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
package controllers
import "github.com/revel/revel"
import "github.com/muizsyed/go-quotesy/app/models"
import "github.com/muizsyed/go-quotesy/app/routes"
import "fmt"
import "strconv"
import "time"
type Category struct {
GorpController
}
func (c Category) Index(page int) revel.Result {
if page < 1 {
page = 1
}
var err error
count := c.GetCount("Category")
offset := 10 * (page - 1)
categories, err := c.Txn.Select(models.Category{}, "select * from Category order by category_id limit 10 offset ?", offset)
if err != nil {
panic(err)
}
return c.Render(categories, count, page)
}
func (c Category) Filter(name string, omit int) revel.Result {
categories, err := c.Txn.Select(models.Json{}, "select category_id value, name data from Category where category_id <> ? AND name like ?", omit, "%"+name+"%")
if err != nil {
panic(err)
}
return c.RenderJson(categories)
}
func (c Category) GetCategories(exclusion *int) []interface{} {
var categories []interface{}
var err error
if exclusion == nil {
categories, err = c.Txn.Select(models.Category{}, "select * from Category order by name")
} else {
categories, err = c.Txn.Select(models.Category{}, "select * from Category where category_id <> ? order by name", *exclusion)
}
if err != nil {
panic(err)
}
return categories
}
func (c Category) New() revel.Result {
c.RenderArgs["action"] = "New"
c.RenderArgs["categories"] = c.GetCategories(nil)
return c.RenderTemplate(c.Name + "/Edit." + c.Request.Format)
}
func (c Category) Add(category models.Category) revel.Result {
fmt.Println("Entered AddCategory")
category.Validate(c.Validation)
if c.Validation.HasErrors() {
c.RenderArgs["categories"] = c.GetCategories(nil)
c.RenderArgs["action"] = "New"
c.FlashParams()
return c.RenderTemplate(c.Name + "/Edit." + c.Request.Format)
}
category.Created = time.Now().UnixNano()
category.Updated = category.Created
var err error
err = c.Txn.Insert(&category)
if err != nil {
panic(err)
}
c.Flash.Success("Added Category, " + category.Name)
return c.Redirect(routes.Category.Index(1))
}
func (c Category) Edit(id int) revel.Result {
var category models.Category
err := c.Txn.SelectOne(&category, "select * from Category where category_id=?", id)
if err != nil {
panic(err)
}
// Dont like the following - but need to set the values initially
category.SetFlashValues(&c.Flash)
if category.ParentId.Valid {
var parentcategory models.Category
err := c.Txn.SelectOne(&parentcategory, "select * from Category where category_id=?", category.ParentId.Int64)
if err != nil {
panic(err)
}
c.RenderArgs["parentcategory"] = parentcategory.Name
}
//c.RenderArgs["categories"] = c.GetCategories(&id)
// Need to remember the version
c.Session["category-version"] = strconv.Itoa(category.Version)
return c.Render(category)
}
func (c Category) Update(category models.Category) revel.Result {
// Run validation rules
category.Validate(c.Validation)
// On request to edit we stored version in session - optimistic locking
var oldSessionVersion = c.Session["category-version"]
oldVersion, _ := strconv.Atoi(oldSessionVersion)
// Update non-form related fields that need updating
category.Version = oldVersion
category.Updated = time.Now().UnixNano()
if c.Validation.HasErrors() {
// Need to set the flash values again
category.SetFlashValues(&c.Flash)
// Store all the categories
c.RenderArgs["categories"] = c.GetCategories(&category.Id)
// As we are rendering a template - need to set the render args
c.RenderArgs["category"] = category
c.FlashParams()
return c.RenderTemplate(c.Name + "/Edit." + c.Request.Format)
}
var err error
_, err = c.Txn.Update(&category)
if err != nil {
panic(err)
}
c.Flash.Success("Updated Category, " + category.Name)
return c.Redirect(routes.Category.Index(1))
}
func (c Category) Delete(id int) revel.Result {
var category models.Category
err := c.Txn.SelectOne(&category, "select * from Category where category_id=?", id)
if err != nil {
panic(err)
}
_, err = c.Txn.Delete(&category)
if err != nil {
panic(err)
}
c.Flash.Success(fmt.Sprintf("Deleted Category: %s", category.Name))
return nil
}