-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
162 lines (147 loc) · 3.49 KB
/
store.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"fmt"
)
type item struct {
id string
createdAt string
lastUpdated string
name string
status Status
description string
}
func (i *item) ProgressStatus() {
switch i.status {
case Backlog:
i.status = Planned
case Planned:
i.status = InProgress
case InProgress:
i.status = InReview
case InReview:
i.status = Done
case Done:
i.status = Archived
case Archived:
i.status = Archived
}
}
type store struct {
items []item
}
func (s *store) CreateTestStore() {
DeleteStore()
for i := 0; i < 10; i++ {
s.CreateItem(fmt.Sprintf("item-%d", i), fmt.Sprintf("description-%d", i), "")
}
}
func (s *store) WriteStore() {
WriteToFile(s.convertItemsToRecords())
}
func (s *store) ListItems(showArchived bool) {
fmt.Println("Id | Created At | Last Updated | Name | Status | Description")
for _, item := range s.items {
if item.status != Archived || showArchived {
fmt.Println(item)
}
}
}
func (s *store) GetItemByName(name string) (item, error) {
for _, item := range s.items {
if item.name == name {
return item, nil
}
}
return item{}, fmt.Errorf("Item not found")
}
func (s *store) GetItemById(id string) (item, error) {
for _, item := range s.items {
if item.id == id {
return item, nil
}
}
return item{}, fmt.Errorf("Item not found")
}
func (s *store) CreateItem(name string, description string, status string) {
// Currently not forcing uniqueness on names... This is going to be a problem
// but I'll punt it for now
now := GetCurrentTimeString()
if status == "" {
status = "backlog"
}
typedStatus, err := StringToStatus(status)
if err != nil {
fmt.Println(err)
return
}
item := &item{
id: Guid(),
createdAt: now,
lastUpdated: now,
name: name,
status: typedStatus,
description: description,
}
s.items = append(s.items, *item)
fmt.Println(s.items)
s.WriteStore()
}
func (s *store) ProgressItem(id string, name string) {
for i, item := range s.items {
if item.name == name && name != "" || item.id == id && id != "" {
s.items[i].ProgressStatus()
s.items[i].lastUpdated = GetCurrentTimeString()
s.WriteStore()
return
}
}
fmt.Println("Item not found")
}
func (s *store) convertItemsToRecords() [][]string {
records := [][]string{}
for _, item := range s.items {
record := []string{item.id, item.createdAt, item.lastUpdated, item.name, string(item.status), item.description}
records = append(records, record)
}
return records
}
func (s *store) DeleteItem(name string, id string) {
for i, item := range s.items {
if item.name == name && name != "" || item.id == id && id != "" {
s.items = append(s.items[:i], s.items[i+1:]...)
s.WriteStore()
return
}
}
fmt.Println("Item not found")
}
func (s *store) UpdateItem(id string, name string, description string, status string, allowEmpty bool) {
var itemToUpdate item
if id == "" {
itemToUpdate, _ = s.GetItemByName(name)
} else {
itemToUpdate, _ = s.GetItemById(id)
}
for i, item := range s.items {
if item.id == itemToUpdate.id {
if name != "" || allowEmpty {
s.items[i].name = name
}
if description != "" || allowEmpty {
s.items[i].description = description
}
if status != "" {
status, err := StringToStatus(status)
if err != nil {
fmt.Println(err)
return
}
s.items[i].status = status
}
s.items[i].lastUpdated = GetCurrentTimeString()
s.WriteStore()
return
}
}
fmt.Println("Item not found")
}