-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.go
More file actions
178 lines (137 loc) · 4.72 KB
/
Copy pathservice.go
File metadata and controls
178 lines (137 loc) · 4.72 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package configuration
import (
"context"
"database/sql"
"dictionaries-service/tenant"
"dictionaries-service/util"
"fmt"
slog "github.com/go-eden/slf4go"
"time"
)
func NewConfigurationService(configurationRepository Repository) Service {
return &service{repository: configurationRepository}
}
type service struct {
repository Repository
}
type Service interface {
LoadForDay(key, tenant string, day time.Time) (*Configuration, error)
Load(key, tenant string, from, to time.Time) ([]Configuration, error)
Save(conf *Configuration) error
NewConfigValue(key, tenant, configType, name, value string) error
LoadMany(tenant string, day time.Time, keys ...string) []Configuration
LoadAllShort(ctx context.Context) ([]Short, error)
LoadValues(ctx context.Context, key string) ([]Configuration, error)
Update(configuration *Configuration) error
DeleteValue(ctx context.Context, key string, dateFrom time.Time) error
AddNewValueInTime(ctx context.Context, key string, value *string, dateFrom, dateTo time.Time) error
UpdateValueInTime(ctx context.Context, key string, dateFrom time.Time, newValue *string) error
LoadEntry(ctx context.Context, key string) (*Configuration, error)
}
func (c *service) LoadForDay(key, tenant string, day time.Time) (*Configuration, error) {
return c.repository.LoadForDay(key, tenant, day)
}
func (c *service) Load(key, tenant string, from, to time.Time) ([]Configuration, error) {
return c.repository.Load(key, tenant, from, to)
}
func (c *service) LoadMany(tenant string, day time.Time, keys ...string) []Configuration {
var res []Configuration
for _, key := range keys {
conf, err := c.LoadForDay(key, tenant, day)
if err != nil {
slog.Warnf("can't load key = %s tenant = %s cause: %v", key, tenant, err)
}
res = append(res, *conf)
}
return res
}
func (c *service) LoadAllShort(ctx context.Context) ([]Short, error) {
t, ok := tenant.FromContext(ctx)
if !ok {
return nil, fmt.Errorf("can't read tenant from context")
}
return c.repository.LoadAllShort(t.Name)
}
func (c *service) LoadValues(ctx context.Context, key string) ([]Configuration, error) {
t, ok := tenant.FromContext(ctx)
if !ok {
return nil, fmt.Errorf("can't read tenant from context")
}
return c.repository.LoadValues(t.Name, key)
}
func (c *service) Save(conf *Configuration) error {
return c.repository.Save(conf)
}
func (c *service) Update(configuration *Configuration) error {
return c.repository.Update(configuration)
}
func (c *service) AddNewValueInTime(ctx context.Context, key string, value *string, dateFrom, dateTo time.Time) error {
t, ok := tenant.FromContext(ctx)
if !ok {
return fmt.Errorf("can't read tenant from context")
}
origin, err := c.repository.LoadFirst(key, t.Name)
if err != nil {
slog.Warnf("can't read config entry for key = %s, %v", key, err)
return fmt.Errorf("there is no config entry for given key = %s", key)
}
err = c.repository.Save(&Configuration{
Key: key,
Tenant: t.Name,
Type: origin.Type,
Name: origin.Name,
Value: util.PointerToSqlNullString(value),
DateFrom: dateFrom,
DateTo: dateTo,
})
if err != nil {
return err
}
return nil
}
func (c *service) LoadEntry(ctx context.Context, key string) (*Configuration, error) {
t, ok := tenant.FromContext(ctx)
if !ok {
return nil, fmt.Errorf("can't read tenant from context")
}
return c.repository.LoadFirst(key, t.Name)
}
func (c *service) UpdateValueInTime(ctx context.Context, key string, dateFrom time.Time, newValue *string) error {
t, ok := tenant.FromContext(ctx)
if !ok {
return fmt.Errorf("can't read tenant from context")
}
origin, err := c.repository.LoadById(key, t.Name, dateFrom)
if err != nil {
slog.Warnf("can't read config entry for key = %s, and date_from = %s, %v", key, dateFrom, err)
return fmt.Errorf("there is no config entry for given key = %s and date_from = %s", key, dateFrom)
}
origin.Value = util.PointerToSqlNullString(newValue)
err = c.repository.Update(&origin)
if err != nil {
slog.Warnf("can't update config value for key = %s, and date_from = %s, %v", key, dateFrom, err)
return err
}
return nil
}
func (c *service) UpdateDateTo(key string, dateFrom, newDateTo time.Time) {
}
func (c *service) NewConfigValue(key, tenant, configType, name, value string) error {
conf := &Configuration{
Key: key,
Tenant: tenant,
Type: configType,
Name: name,
Value: sql.NullString{String: value},
DateFrom: BeginOfTheWorld,
DateTo: EndOfTheWorld,
}
return c.repository.Save(conf)
}
func (c *service) DeleteValue(ctx context.Context, key string, dateFrom time.Time) error {
t, ok := tenant.FromContext(ctx)
if !ok {
return fmt.Errorf("can't read tenant from context")
}
return c.repository.DeleteValue(t.Name, key, dateFrom)
}