-
Notifications
You must be signed in to change notification settings - Fork 9
/
app_storage.go
124 lines (106 loc) · 2.78 KB
/
app_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
package mem
import (
"encoding/json"
"log"
"strings"
"github.com/madappgang/identifo/model"
"github.com/rs/xid"
)
// NewAppStorage creates new in-memory AppStorage implementation.
func NewAppStorage() (model.AppStorage, error) {
return &AppStorage{storage: make(map[string]model.AppData)}, nil
}
// AppStorage is a fully functional app storage.
type AppStorage struct {
storage map[string]model.AppData
}
// AppByID returns app by ID from the in-memory storage.
func (as *AppStorage) AppByID(id string) (model.AppData, error) {
a, ok := as.storage[id]
if !ok {
return model.AppData{}, ErrorNotFound
}
return a, nil
}
// ActiveAppByID returns app by id only if it's active.
func (as *AppStorage) ActiveAppByID(appID string) (model.AppData, error) {
if appID == "" {
return model.AppData{}, ErrorEmptyAppID
}
app, err := as.AppByID(appID)
if err != nil {
return model.AppData{}, err
}
if !app.Active {
return model.AppData{}, ErrorInactiveApp
}
return app, nil
}
// CreateApp creates new app in memory.
func (as *AppStorage) CreateApp(app model.AppData) (model.AppData, error) {
// generate new ID if it's not set
if len(app.ID) == 0 {
app.ID = xid.New().String()
}
as.storage[app.ID] = app
return app, nil
}
// DisableApp deletes app from in-memory storage.
func (as *AppStorage) DisableApp(app model.AppData) error {
delete(as.storage, app.ID)
return nil
}
// UpdateApp updates app in the storage.
func (as *AppStorage) UpdateApp(appID string, newApp model.AppData) (model.AppData, error) {
delete(as.storage, appID)
as.storage[appID] = newApp
return newApp, nil
}
// FetchApps fetches apps which name satisfies provided filterString.
// Supports pagination.
func (as *AppStorage) FetchApps(filterString string, skip, limit int) ([]model.AppData, int, error) {
apps := []model.AppData{}
var total int
for _, app := range as.storage {
total++
skip--
if skip > -1 {
continue
}
if limit != 0 && len(apps) == limit {
break
}
if strings.Contains(strings.ToLower(app.Name), strings.ToLower(filterString)) {
apps = append(apps, app)
}
}
return apps, total, nil
}
// DeleteApp does nothing here.
func (as *AppStorage) DeleteApp(id string) error {
return nil
}
// TestDatabaseConnection is always optimistic about the database connection.
func (as *AppStorage) TestDatabaseConnection() error {
return nil
}
// ImportJSON imports data from JSON.
func (as *AppStorage) ImportJSON(data []byte) error {
apd := []model.AppData{}
if err := json.Unmarshal(data, &apd); err != nil {
log.Println("error while unmarshal app data: ", err)
return err
}
for _, a := range apd {
if _, err := as.CreateApp(a); err != nil {
return err
}
}
return nil
}
// Close clears storage.
func (as *AppStorage) Close() {
for k := range as.storage {
delete(as.storage, k)
}
}