This repository has been archived by the owner on Jul 19, 2020. It is now read-only.
/
rule_service.go
134 lines (124 loc) · 3.63 KB
/
rule_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
package service
import (
"context"
"github.com/hotstone-seo/hotstone-seo/pkg/dbtxn"
"github.com/hotstone-seo/hotstone-seo/server/repository"
"go.uber.org/dig"
)
// RuleService contain logic for Rule Controller [mock]
type RuleService interface {
FindOne(ctx context.Context, id int64) (*repository.Rule, error)
Find(ctx context.Context, paginationParam repository.PaginationParam) ([]*repository.Rule, error)
Insert(ctx context.Context, rule repository.Rule) (lastInsertID int64, err error)
Delete(ctx context.Context, id int64) error
Update(ctx context.Context, rule repository.Rule) error
}
// RuleServiceImpl is implementation of RuleService
type RuleServiceImpl struct {
dig.In
RuleRepo repository.RuleRepo
URLSyncRepo repository.URLSyncRepo
AuditTrailService
HistoryService
dbtxn.Transactional
}
// NewRuleService return new instance of RuleService [constructor]
func NewRuleService(impl RuleServiceImpl) RuleService {
return &impl
}
// FindOne rule
func (r *RuleServiceImpl) FindOne(ctx context.Context, id int64) (rule *repository.Rule, err error) {
return r.RuleRepo.FindOne(ctx, id)
}
// Find rule
func (r *RuleServiceImpl) Find(ctx context.Context, paginationParam repository.PaginationParam) (list []*repository.Rule, err error) {
return r.RuleRepo.Find(ctx, paginationParam)
}
// Insert rule
func (r *RuleServiceImpl) Insert(ctx context.Context, rule repository.Rule) (newRuleID int64, err error) {
defer r.CommitMe(&ctx)()
if newRuleID, err = r.RuleRepo.Insert(ctx, rule); err != nil {
r.CancelMe(ctx, err)
return
}
if _, err = r.URLSyncRepo.Insert(ctx, repository.URLSync{
Operation: "INSERT",
RuleID: newRuleID,
LatestURLPattern: &rule.URLPattern,
}); err != nil {
r.CancelMe(ctx, err)
return newRuleID, err
}
newRule, err := r.RuleRepo.FindOne(ctx, newRuleID)
if err != nil {
r.CancelMe(ctx, err)
return
}
if _, err = r.AuditTrailService.RecordChanges(ctx, "rules", newRuleID, repository.Insert, nil, newRule); err != nil {
r.CancelMe(ctx, err)
return
}
return newRuleID, nil
}
// Delete rule
func (r *RuleServiceImpl) Delete(ctx context.Context, id int64) (err error) {
defer r.CommitMe(&ctx)()
oldRule, err := r.RuleRepo.FindOne(ctx, id)
if err != nil {
r.CancelMe(ctx, err)
return
}
if _, err = r.HistoryService.RecordHistory(ctx, "rules", id, oldRule); err != nil {
r.CancelMe(ctx, err)
return
}
if err = r.RuleRepo.Delete(ctx, id); err != nil {
r.CancelMe(ctx, err)
return
}
if _, err = r.URLSyncRepo.Insert(ctx, repository.URLSync{
Operation: "DELETE",
RuleID: id,
LatestURLPattern: nil,
}); err != nil {
r.CancelMe(ctx, err)
return
}
// TODO: why oldRule still exists though it's already deleted ?
if _, err = r.AuditTrailService.RecordChanges(ctx, "rules", id, repository.Delete, oldRule, nil); err != nil {
r.CancelMe(ctx, err)
return
}
return nil
}
// Update rule
func (r *RuleServiceImpl) Update(ctx context.Context, rule repository.Rule) (err error) {
defer r.CommitMe(&ctx)()
oldRule, err := r.RuleRepo.FindOne(ctx, rule.ID)
if err != nil {
r.CancelMe(ctx, err)
return
}
if err = r.RuleRepo.Update(ctx, rule); err != nil {
r.CancelMe(ctx, err)
return
}
if _, err = r.URLSyncRepo.Insert(ctx, repository.URLSync{
Operation: "UPDATE",
RuleID: rule.ID,
LatestURLPattern: &rule.URLPattern,
}); err != nil {
r.CancelMe(ctx, err)
return
}
newRule, err := r.RuleRepo.FindOne(ctx, rule.ID)
if err != nil {
r.CancelMe(ctx, err)
return
}
if _, err = r.AuditTrailService.RecordChanges(ctx, "rules", rule.ID, repository.Update, oldRule, newRule); err != nil {
r.CancelMe(ctx, err)
return
}
return nil
}