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:
24
_examples/tutorial/vuejs-todo-mvc/src/todo/item.go
Normal file
24
_examples/tutorial/vuejs-todo-mvc/src/todo/item.go
Normal 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
|
||||
}
|
||||
53
_examples/tutorial/vuejs-todo-mvc/src/todo/service.go
Normal file
53
_examples/tutorial/vuejs-todo-mvc/src/todo/service.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user