-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
260 lines (219 loc) · 7.23 KB
/
service.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package property_view_type
import (
"context"
"github.com/yaruz/app/pkg/yarus_platform/yaruserror"
"github.com/minipkg/selection_condition"
"github.com/pkg/errors"
"github.com/minipkg/log"
)
// IService encapsulates usecase logic.
type IService interface {
NewEntity() *PropertyViewType
Get(ctx context.Context, id uint) (*PropertyViewType, error)
First(ctx context.Context, entity *PropertyViewType) (*PropertyViewType, error)
Query(ctx context.Context, query *selection_condition.SelectionCondition) ([]PropertyViewType, error)
Count(ctx context.Context, cond *selection_condition.SelectionCondition) (int64, error)
Create(ctx context.Context, entity *PropertyViewType) error
Update(ctx context.Context, entity *PropertyViewType) error
Delete(ctx context.Context, entity *PropertyViewType) error
TGet(ctx context.Context, id uint, langID uint) (*PropertyViewType, error)
TFirst(ctx context.Context, entity *PropertyViewType, langID uint) (*PropertyViewType, error)
TQuery(ctx context.Context, cond *selection_condition.SelectionCondition, langID uint) ([]PropertyViewType, error)
TCreate(ctx context.Context, entity *PropertyViewType, langID uint) (err error)
TUpdate(ctx context.Context, entity *PropertyViewType, langID uint) (err error)
GetSysnames(ctx context.Context) ([]string, error)
GetSysnamesEmptyInterfaceSlice(ctx context.Context) ([]interface{}, error)
GetMapSysnameID(ctx context.Context) (map[string]uint, error)
GetMapIDSysname(ctx context.Context) (map[uint]string, error)
GetIDBySysname(ctx context.Context, sysname string) (uint, error)
GetSysnameByID(ctx context.Context, id uint) (string, error)
}
type service struct {
logger log.Logger
repository Repository
}
var _ IService = (*service)(nil)
// NewService creates a new service.
func NewService(logger log.Logger, repo Repository) IService {
s := &service{
logger: logger,
repository: repo,
}
repo.SetDefaultConditions(s.defaultConditions())
return s
}
// Defaults returns defaults params
func (s *service) defaultConditions() *selection_condition.SelectionCondition {
return &selection_condition.SelectionCondition{}
}
func (s *service) NewEntity() *PropertyViewType {
return New()
}
// Get returns the entity with the specified ID.
func (s *service) Get(ctx context.Context, id uint) (*PropertyViewType, error) {
entity, err := s.repository.Get(ctx, id)
if err != nil {
return nil, err
}
return entity, nil
}
func (s *service) First(ctx context.Context, entity *PropertyViewType) (*PropertyViewType, error) {
entity, err := s.repository.First(ctx, entity)
if err != nil {
return nil, err
}
return entity, nil
}
func (s *service) TGet(ctx context.Context, id uint, langID uint) (*PropertyViewType, error) {
entity, err := s.repository.TGet(ctx, id, langID)
if err != nil {
return nil, err
}
return entity, nil
}
func (s *service) TFirst(ctx context.Context, entity *PropertyViewType, langID uint) (*PropertyViewType, error) {
entity, err := s.repository.TFirst(ctx, entity, langID)
if err != nil {
return nil, err
}
return entity, nil
}
// Query returns the items with the specified selection condition.
func (s *service) Query(ctx context.Context, cond *selection_condition.SelectionCondition) ([]PropertyViewType, error) {
items, err := s.repository.Query(ctx, cond)
if err != nil {
return nil, errors.Wrapf(err, "Can not find a list of items by query: %v", cond)
}
return items, nil
}
func (s *service) TQuery(ctx context.Context, cond *selection_condition.SelectionCondition, langID uint) ([]PropertyViewType, error) {
items, err := s.repository.TQuery(ctx, cond, langID)
if err != nil {
return nil, errors.Wrapf(err, "Can not find a list of items by query: %v", cond)
}
return items, nil
}
func (s *service) GetSysnames(ctx context.Context) ([]string, error) {
items, err := s.Query(ctx, &selection_condition.SelectionCondition{})
if err != nil {
return nil, err
}
res := make([]string, len(items))
for i, item := range items {
res[i] = item.Sysname
}
return res, nil
}
func (s *service) GetSysnamesEmptyInterfaceSlice(ctx context.Context) ([]interface{}, error) {
items, err := s.Query(ctx, &selection_condition.SelectionCondition{})
if err != nil {
return nil, err
}
res := make([]interface{}, len(items))
for i, item := range items {
res[i] = item.Sysname
}
return res, nil
}
func (s *service) GetMapSysnameID(ctx context.Context) (map[string]uint, error) {
items, err := s.Query(ctx, &selection_condition.SelectionCondition{})
if err != nil {
return nil, err
}
res := make(map[string]uint, len(items))
for _, item := range items {
res[item.Sysname] = item.ID
}
return res, nil
}
func (s *service) GetMapIDSysname(ctx context.Context) (map[uint]string, error) {
items, err := s.Query(ctx, &selection_condition.SelectionCondition{})
if err != nil {
return nil, err
}
res := make(map[uint]string, len(items))
for _, item := range items {
res[item.ID] = item.Sysname
}
return res, nil
}
func (s *service) GetIDBySysname(ctx context.Context, sysname string) (uint, error) {
mapSysnameID, err := s.GetMapSysnameID(ctx)
if err != nil {
return 0, err
}
id, ok := mapSysnameID[sysname]
if !ok {
return 0, yaruserror.ErrNotFound
}
return id, nil
}
func (s *service) GetSysnameByID(ctx context.Context, id uint) (string, error) {
mapIDSysname, err := s.GetMapIDSysname(ctx)
if err != nil {
return "", err
}
sysname, ok := mapIDSysname[id]
if !ok {
return "", yaruserror.ErrNotFound
}
return sysname, nil
}
func (s *service) Count(ctx context.Context, cond *selection_condition.SelectionCondition) (int64, error) {
count, err := s.repository.Count(ctx, cond)
if err != nil {
return 0, errors.Wrapf(err, "Can not count a list of items by query: %v", cond)
}
return count, nil
}
func (s *service) Create(ctx context.Context, entity *PropertyViewType) error {
err := entity.Validate()
if err != nil {
return errors.Wrapf(err, "Validation error: %v", err)
}
err = s.repository.Create(ctx, entity)
if err != nil {
return errors.Wrapf(err, "Can not create an entity: %v", entity)
}
return nil
}
func (s *service) TCreate(ctx context.Context, entity *PropertyViewType, langID uint) error {
err := entity.Validate()
if err != nil {
return errors.Wrapf(err, "Validation error: %v", err)
}
err = s.repository.TCreate(ctx, entity, langID)
if err != nil {
return errors.Wrapf(err, "Can not create an entity: %v", entity)
}
return nil
}
func (s *service) Update(ctx context.Context, entity *PropertyViewType) error {
err := entity.Validate()
if err != nil {
return errors.Wrapf(err, "Validation error: %v", err)
}
err = s.repository.Update(ctx, entity)
if err != nil {
return errors.Wrapf(err, "Can not update an entity: %v", entity)
}
return nil
}
func (s *service) TUpdate(ctx context.Context, entity *PropertyViewType, langID uint) error {
err := entity.Validate()
if err != nil {
return errors.Wrapf(err, "Validation error: %v", err)
}
err = s.repository.TUpdate(ctx, entity, langID)
if err != nil {
return errors.Wrapf(err, "Can not update an entity: %v", entity)
}
return nil
}
func (s *service) Delete(ctx context.Context, entity *PropertyViewType) error {
err := s.repository.Delete(ctx, entity)
if err != nil {
return errors.Wrapf(err, "Can not delete an entity: %v", entity)
}
return nil
}