/
storage.go
134 lines (111 loc) · 2.65 KB
/
storage.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
package main
import (
"errors"
"fmt"
"log"
"time"
)
var (
errNotFound = errors.New("Item not found, the operation failed")
)
//Memory save tha data in memory
type Memory struct {
Items []Item
}
func (a *api) listsGoods() ([]Item, error) {
a.mutex.Lock()
defer a.mutex.Unlock()
var listItems []Item
for _, item := range a.db.Items {
valid := checkValidity(item)
item.IsValid = valid
listItems = append(listItems, item)
}
return listItems, nil
}
func (a *api) addGood(items ...Item) (string, error) {
a.mutex.Lock()
defer a.mutex.Unlock()
for _, item := range items {
for _, i := range a.db.Items {
if item.ID == i.ID {
return "item already exists", nil
}
}
addtime := time.Now().Format(layoutRO)
addtime1, err := time.Parse(layoutRO, addtime)
if err != nil {
log.Printf("Can't parse the date, %v", err)
return "", fmt.Errorf("can't parse the date: %v", err)
}
if len(a.db.Items) == 0 {
item.ID = len(a.db.Items) + 1
} else {
lastItem := a.db.Items[len(a.db.Items)-1]
item.ID = lastItem.ID + 1
}
item.Created = timestamp{addtime1}
a.db.Items = append(a.db.Items, item)
}
return "Items has been added to database", nil
}
func (a *api) openState(id int, status bool) (string, error) {
a.mutex.Lock()
defer a.mutex.Unlock()
var foundIndex int
var found bool
for i, item := range a.db.Items {
if id == item.ID {
found = true
foundIndex = i
break
}
}
if !found {
return "", errNotFound
}
opentimeS := time.Now().Format(layoutRO)
opentimeT, err := time.Parse(layoutRO, opentimeS)
if err != nil {
log.Printf("Can't parse the date, %v", err)
return "", fmt.Errorf("can't parse the date: %v", err)
}
a.db.Items[foundIndex].IsOpen = status
a.db.Items[foundIndex].Opened = timestamp{opentimeT}
return fmt.Sprintf("Item with id %d has been opened", a.db.Items[foundIndex].ID), nil
}
func (a *api) delGood(id int) (string, error) {
a.mutex.Lock()
defer a.mutex.Unlock()
var foundIndex int
var found bool
for i, item := range a.db.Items {
if id == item.ID {
found = true
foundIndex = i
break
}
}
if !found {
return "", errNotFound
}
a.db.Items = removeIndex(a.db.Items, foundIndex)
return fmt.Sprintf("Item id %d has been deleted", id), nil
}
func removeIndex(s []Item, index int) []Item {
return append(s[:index], s[index+1:]...)
}
func checkValidity(i Item) bool {
t := time.Now()
i.IsValid = true
if t.Sub(i.ExpDate.Time) > 0 {
i.IsValid = false
}
if i.IsOpen {
//if i.Opened.Time.Add(time.Duration(int64(time.Duration(i.ExpOpen * 24).Hours()))).Before(t) {
if t.Sub(i.Opened.Time.AddDate(0, 0, i.ExpOpen)) > 0 {
i.IsValid = false
}
}
return i.IsValid
}