1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-06 03:27:27 +00:00

finish the first state of the vuejs todo mvc example, a simple rest api - todo: websocket and live updates between browser tabs with the same session id

Former-commit-id: 0bd859420cff87014479c44a471ec273c621c1a2
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-12-23 17:07:39 +02:00
parent a2f217be17
commit e1c65d23fb
11 changed files with 232 additions and 190 deletions

View File

@@ -1,24 +1,8 @@
package todo
type State uint32
const (
StateActive State = iota
StateCompleted
)
func ParseState(s string) State {
switch s {
case "completed":
return StateCompleted
default:
return StateActive
}
}
type Item struct {
OwnerID string
ID int64
Body string
CurrentState State
SessionID string `json:"-"`
ID int64 `json:"id,omitempty"`
Title string `json:"title"`
Completed bool `json:"completed"`
}

View File

@@ -1,53 +1,31 @@
package todo
type Service interface {
GetByID(id int64) (Item, bool)
GetByOwner(owner string) []Item
Complete(item Item) bool
Save(newItem Item) error
Get(owner string) []Item
Save(owner string, newItems []Item) error
}
type MemoryService struct {
items map[int64]Item
items map[string][]Item
}
func (s *MemoryService) getLatestID() (id int64) {
for k := range s.items {
if k > id {
id = k
func NewMemoryService() *MemoryService {
return &MemoryService{make(map[string][]Item, 0)}
}
func (s *MemoryService) Get(sessionOwner string) (items []Item) {
return s.items[sessionOwner]
}
func (s *MemoryService) Save(sessionOwner string, newItems []Item) error {
var prevID int64
for i := range newItems {
if newItems[i].ID == 0 {
newItems[i].ID = prevID
prevID++
}
}
return
}
func (s *MemoryService) GetByID(id int64) (Item, bool) {
item, found := s.items[id]
return item, found
}
func (s *MemoryService) GetByOwner(owner string) (items []Item) {
for _, item := range s.items {
if item.OwnerID != owner {
continue
}
items = append(items, item)
}
return
}
func (s *MemoryService) Complete(item Item) bool {
item.CurrentState = StateCompleted
return s.Save(item) == nil
}
func (s *MemoryService) Save(newItem Item) error {
if newItem.ID == 0 {
// create
newItem.ID = s.getLatestID() + 1
}
// full replace here for the shake of simplicity)
s.items[newItem.ID] = newItem
s.items[sessionOwner] = newItems
return nil
}