-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtodolist.go
65 lines (57 loc) · 1.15 KB
/
todolist.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
package example
import (
"fmt"
)
type Todo struct {
Id int `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
AlertTime string `json:"alert_time"`
}
type TodoList struct {
lastId int
data map[int]*Todo
}
func NewTodoList() *TodoList {
return &TodoList{data: map[int]*Todo{}}
}
func (this *TodoList) Create(title, content, alertTime string) error {
this.lastId += 1
id := this.lastId
if _, exist := this.data[id]; exist {
return fmt.Errorf("duplicate id %d", id)
}
this.data[id] = &Todo{
id,
title,
content,
alertTime,
}
return nil
}
func (this *TodoList) Update(id int, title, content, alertTime string) error {
if _, exist := this.data[id]; !exist {
return fmt.Errorf("id is not exist %d", id)
}
this.data[id] = &Todo{
id,
title,
content,
alertTime,
}
return nil
}
func (this *TodoList) Delete(id int) error {
if _, exist := this.data[id]; !exist {
return fmt.Errorf("id is not exist %d", id)
}
delete(this.data, id)
return nil
}
func (this *TodoList) Retrieve() ([]*Todo, error) {
list := []*Todo{}
for _, item := range this.data {
list = append(list, item)
}
return list, nil
}