-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
todos_impl.go
97 lines (87 loc) · 2.58 KB
/
todos_impl.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
package implementation
import (
"fmt"
"net/http"
"sync"
"github.com/go-openapi/runtime/middleware"
"github.com/go-openapi/swag"
"github.com/go-swagger/go-swagger/examples/auto-configure/models"
"github.com/go-swagger/go-swagger/examples/auto-configure/restapi/operations/todos"
)
type TodosHandlerImpl struct {
// locks the entire struct
lock sync.Mutex
items map[int64]*models.Item
// counter for newest item
idx int64
}
func (i *TodosHandlerImpl) AddOne(params todos.AddOneParams, principal interface{}) middleware.Responder {
i.lock.Lock()
defer i.lock.Unlock()
newItem := params.Body
if newItem == nil {
return todos.NewAddOneDefault(http.StatusBadRequest).
WithPayload(&models.Error{
Code: http.StatusBadRequest,
Message: &[]string{"Item Body is nil"}[0],
})
}
// assign new id
newItem.ID = i.idx
i.idx++
i.items[newItem.ID] = newItem
return todos.NewAddOneCreated().WithPayload(newItem)
}
func (i *TodosHandlerImpl) DestroyOne(params todos.DestroyOneParams, principal interface{}) middleware.Responder {
i.lock.Lock()
defer i.lock.Unlock()
if _, ok := i.items[params.ID]; !ok {
return todos.NewDestroyOneDefault(http.StatusNotFound).
WithPayload(&models.Error{
Code: http.StatusNotFound,
Message: &[]string{fmt.Sprintf("item with id %d is not found.", params.ID)}[0],
})
}
delete(i.items, params.ID)
return todos.NewDestroyOneNoContent()
}
func (i *TodosHandlerImpl) FindTodos(params todos.FindTodosParams, principal interface{}) middleware.Responder {
i.lock.Lock()
defer i.lock.Unlock()
mergedParams := todos.NewFindTodosParams()
mergedParams.Since = swag.Int64(0)
if params.Since != nil {
mergedParams.Since = params.Since
}
if params.Limit != nil {
mergedParams.Limit = params.Limit
}
limit := *mergedParams.Limit
since := *mergedParams.Since
// copy all items and return
result := make([]*models.Item, 0)
for id, item := range i.items {
if len(result) >= int(limit) {
break
}
if since == 0 || id > since {
result = append(result, item)
}
}
return todos.NewFindTodosOK().WithPayload(result)
}
func (i *TodosHandlerImpl) UpdateOne(params todos.UpdateOneParams, principal interface{}) middleware.Responder {
i.lock.Lock()
defer i.lock.Unlock()
if _, ok := i.items[params.ID]; !ok {
errStr := fmt.Sprintf("Item with id %v is not found", params.ID)
return todos.NewUpdateOneDefault(http.StatusNotFound).
WithPayload(&models.Error{
Code: http.StatusNotFound,
Message: &errStr,
})
}
params.Body.ID = params.ID
i.items[params.ID] = params.Body
return todos.NewUpdateOneOK().WithPayload(params.Body)
}