-
Notifications
You must be signed in to change notification settings - Fork 0
/
relation_service.go
239 lines (204 loc) · 8.07 KB
/
relation_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
package entity_type
import (
"context"
"github.com/yaruz/app/pkg/yarus_platform/yaruserror"
"github.com/minipkg/selection_condition"
"github.com/yaruz/app/pkg/yarus_platform/reference/domain/property"
"github.com/pkg/errors"
"github.com/minipkg/log"
)
// RelationService encapsulates usecase logic.
type RelationService interface {
NewEntity() *Relation
Get(ctx context.Context, id uint) (*Relation, error)
First(ctx context.Context, entity *Relation) (*Relation, error)
Query(ctx context.Context, query *selection_condition.SelectionCondition) ([]Relation, error)
Count(ctx context.Context, cond *selection_condition.SelectionCondition) (int64, error)
UpsertBySysname(ctx context.Context, entity *Relation, langID uint) (err error)
Create(ctx context.Context, entity *Relation) error
Update(ctx context.Context, entity *Relation) error
Delete(ctx context.Context, entity *Relation) error
TGet(ctx context.Context, id uint, langID uint) (*Relation, error)
TFirst(ctx context.Context, entity *Relation, langID uint) (*Relation, error)
TQuery(ctx context.Context, cond *selection_condition.SelectionCondition, langID uint) ([]Relation, error)
TCreate(ctx context.Context, entity *Relation, langID uint) (err error)
TUpdate(ctx context.Context, entity *Relation, langID uint) (err error)
PropertyAndRelationQuery(ctx context.Context, cond *selection_condition.SelectionCondition) ([]property.Property, []Relation, error)
TPropertyAndRelationQuery(ctx context.Context, cond *selection_condition.SelectionCondition, langID uint) ([]property.Property, []Relation, error)
GetPropertiesAndRelationsByEntityTypeID(ctx context.Context, entityTypeID uint) ([]property.Property, []Relation, error)
TGetPropertiesAndRelationsByEntityTypeID(ctx context.Context, entityTypeID uint, langID uint) ([]property.Property, []Relation, error)
}
type relationService struct {
logger log.Logger
repository RelationRepository
propertyService property.IService
}
var _ RelationService = (*relationService)(nil)
// NewRelationService creates a new relationService.
func NewRelationService(logger log.Logger, repo RelationRepository, propertyService property.IService) RelationService {
s := &relationService{
logger: logger,
repository: repo,
propertyService: propertyService,
}
repo.SetDefaultConditions(s.defaultConditions())
return s
}
// Defaults returns defaults params
func (s *relationService) defaultConditions() *selection_condition.SelectionCondition {
return &selection_condition.SelectionCondition{}
}
func (s *relationService) NewEntity() *Relation {
return NewRelation()
}
// Get returns the entity with the specified ID.
func (s *relationService) Get(ctx context.Context, id uint) (*Relation, error) {
entity, err := s.repository.Get(ctx, id)
if err != nil {
return nil, err
}
return entity, nil
}
func (s *relationService) First(ctx context.Context, entity *Relation) (*Relation, error) {
entity, err := s.repository.First(ctx, entity)
if err != nil {
return nil, err
}
return entity, nil
}
func (s *relationService) TGet(ctx context.Context, id uint, langID uint) (*Relation, error) {
entity, err := s.repository.TGet(ctx, id, langID)
if err != nil {
return nil, err
}
return entity, nil
}
func (s *relationService) TFirst(ctx context.Context, entity *Relation, langID uint) (*Relation, 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 *relationService) Query(ctx context.Context, cond *selection_condition.SelectionCondition) ([]Relation, 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 *relationService) TQuery(ctx context.Context, cond *selection_condition.SelectionCondition, langID uint) ([]Relation, 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 *relationService) 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 *relationService) UpsertBySysname(ctx context.Context, entity *Relation, langID uint) (err error) {
found, err := s.propertyService.First(ctx, &property.Property{
Sysname: entity.Sysname,
})
if err != nil {
if err != yaruserror.ErrNotFound {
return err
}
err = s.TCreate(ctx, entity, langID)
} else {
entity.ID = found.ID
entity.NameSourceID = found.NameSourceID
entity.DescriptionSourceID = found.DescriptionSourceID
entity.CreatedAt = found.CreatedAt
if found.SortOrder != property.SortOrderDefault {
entity.SortOrder = found.SortOrder
}
err = s.TUpdate(ctx, entity, langID)
}
return err
}
func (s *relationService) Create(ctx context.Context, entity *Relation) 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 *relationService) TCreate(ctx context.Context, entity *Relation, 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 *relationService) Update(ctx context.Context, entity *Relation) 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 *relationService) TUpdate(ctx context.Context, entity *Relation, 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 *relationService) Delete(ctx context.Context, entity *Relation) error {
err := s.repository.Delete(ctx, entity)
if err != nil {
return errors.Wrapf(err, "Can not delete an entity: %v", entity)
}
return nil
}
func (s *relationService) PropertyAndRelationQuery(ctx context.Context, cond *selection_condition.SelectionCondition) ([]property.Property, []Relation, error) {
props, rels, err := s.repository.PropertyAndRelationQuery(ctx, cond)
if err != nil {
return nil, nil, errors.Wrapf(err, "Can not get properties and relations by query: %v", cond)
}
return props, rels, nil
}
func (s *relationService) TPropertyAndRelationQuery(ctx context.Context, cond *selection_condition.SelectionCondition, langID uint) ([]property.Property, []Relation, error) {
props, rels, err := s.repository.TPropertyAndRelationQuery(ctx, cond, langID)
if err != nil {
return nil, nil, errors.Wrapf(err, "Can not get properties and relations by query: %v", cond)
}
return props, rels, nil
}
func (s *relationService) GetPropertiesAndRelationsByEntityTypeID(ctx context.Context, entityTypeID uint) ([]property.Property, []Relation, error) {
props, rels, err := s.repository.GetPropertiesAndRelationsByEntityTypeID(ctx, entityTypeID)
if err != nil {
return nil, nil, errors.Wrapf(err, "Can not get properties and relations by entityTypeID: %v", entityTypeID)
}
return props, rels, nil
}
func (s *relationService) TGetPropertiesAndRelationsByEntityTypeID(ctx context.Context, entityTypeID uint, langID uint) ([]property.Property, []Relation, error) {
props, rels, err := s.repository.TGetPropertiesAndRelationsByEntityTypeID(ctx, entityTypeID, langID)
if err != nil {
return nil, nil, errors.Wrapf(err, "Can not get properties and relations by entityTypeID: %v", entityTypeID)
}
return props, rels, nil
}