1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-20 11:27:06 +00:00

start of the vue + mvc example and a simple session binding

Former-commit-id: 994f00952352c93d270ad197cb843f3222fb37c0
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-12-14 05:56:23 +02:00
parent d72c649441
commit 0b2dcc76f5
12 changed files with 438 additions and 3 deletions

View File

@@ -0,0 +1,24 @@
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
}

View File

@@ -0,0 +1,53 @@
package todo
type Service interface {
GetByID(id int64) (Item, bool)
GetByOwner(owner string) []Item
Complete(item Item) bool
Save(newItem Item) error
}
type MemoryService struct {
items map[int64]Item
}
func (s *MemoryService) getLatestID() (id int64) {
for k := range s.items {
if k > id {
id = k
}
}
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 simplicy)
s.items[newItem.ID] = newItem
return nil
}