-
Notifications
You must be signed in to change notification settings - Fork 1
/
items.go
94 lines (77 loc) · 1.95 KB
/
items.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
package controllers
import (
"encoding/json"
"sort"
"github.com/revel/revel"
)
type Items struct {
*revel.Controller
}
type Item struct {
Id int `json:"id"`
Name string `json:"name"`
}
type ById []Item
func (list ById) Len() int { return len(list) }
func (list ById) Less(i, j int) bool { return list[i].Id < list[j].Id }
func (list ById) Swap(i, j int) { list[i], list[j] = list[j], list[i] }
// Find an item from the list with Id `id` and the index
// -1 for not found
func (list ById) Find(id int) (Item, int) {
i := list.Search(id)
if i < len(list) && list[i].Id == id {
return list[i], i
}
return Item{}, -1
}
// Search for an item by id and return the index
func (list ById) Search(id int) int {
return sort.Search(len(list), func(i int) bool {
return list[i].Id == id
})
}
var ItemList ById = make([]Item, 0, 1)
// List the current set of items
func (c Items) List() revel.Result {
return c.RenderJson(ItemList)
}
// Create an item
func (c Items) Create() revel.Result {
var item Item
err := json.NewDecoder(c.Request.Body).Decode(&item)
if err != nil {
c.RenderError(err)
}
ItemList = append(ItemList, item)
sort.Sort(ById(ItemList))
return c.RenderJson(item)
}
// Read an item
func (c Items) Read(id int) revel.Result {
item, i := ItemList.Find(id)
if i < 0 {
return c.NotFound("Item with id %d not found.", id)
}
return c.RenderJson(item)
}
// Update an item
func (c Items) Update(id int, name string) revel.Result {
item, i := ItemList.Find(id)
if i < 0 {
return c.NotFound("Item with id %d not found.", id)
}
item.Name = name
ItemList[i] = item
sort.Sort(ById(ItemList))
return c.RenderJson(item)
}
// Delete an item
func (c Items) Delete(id int) revel.Result {
// since this is an array, deleting an object doesn't affect sorting
if item, i := ItemList.Find(id); i < 0 {
return c.NotFound("Item with id %d not found.", id)
} else {
ItemList = append(ItemList[:i], ItemList[i+1:]...)
return c.RenderJson(item)
}
}