-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
167 lines (134 loc) · 3.6 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
package forms
import (
"errors"
"fmt"
"sync"
)
// Service is the web service for the forms handling. It needs transports to communicate with the outside world, and backends to manage the data
//
// A service can have multiple transports and backends. For data consistency, in case of multiple backends, the first one in the list is the reference.
// If you mix backends with data retention (database for example) and backends without (email for example), set a backend with data retention as the first one in the list
type Service struct {
Transports []Transport
Backends []Backend
}
// NewService creates a new service with transports and backends
func NewService(transports []Transport, backends []Backend) *Service {
return &Service{
Backends: backends,
Transports: transports,
}
}
// Run runs the service, launching with backends and transports
func (s *Service) Run() {
var wg sync.WaitGroup
if len(s.Backends) == 0 || len(s.Transports) == 0 {
panic(errors.New("You must define at least one backend and one transport"))
}
for _, t := range s.Transports {
wg.Add(1)
go func(t Transport) {
defer wg.Done()
t.Run(s)
}(t)
}
wg.Wait()
}
// AddForm creates a new form
func (s *Service) AddForm(f Form) error {
for _, b := range s.Backends {
if err := b.AddForm(f); err != nil {
return err
}
}
return nil
}
// DeleteForm deletes a form
func (s *Service) DeleteForm(formId string) error {
for _, b := range s.Backends {
if err := b.DeleteForm(formId); err != nil {
return err
}
}
return nil
}
// GetForms retrieves the forms list
func (s *Service) GetForms() ([]Form, error) {
if len(s.Backends) == 0 {
return []Form{}, errors.New("not enough backends")
}
return s.Backends[0].GetForms()
}
// GetForm retrieves a single form given its ID
func (s *Service) GetForm(formId string) (Form, error) {
if len(s.Backends) == 0 {
return nil, errors.New("not enough backends")
}
form, err := s.Backends[0].GetForm(formId)
if err != nil {
return nil, err
}
return form, nil
}
// SubmitResponse sends a response to a form
func (s *Service) SubmitResponse(formId string, response Response) error {
if len(s.Backends) == 0 {
return errors.New("not enough backends")
}
form, err := s.Backends[0].GetForm(formId)
if err != nil {
return err
}
if !form.Validate(response) {
return errors.New("bad response format")
}
for _, b := range s.Backends {
if err := b.SubmitResponse(form, response); err != nil {
return err
}
}
additionalBackends, err := s.GetFormBackends(formId)
if err != nil {
fmt.Println(err)
return nil
}
// Form backends
for _, b := range additionalBackends {
if err := b.SubmitResponse(form, response); err != nil {
return err
}
}
return nil
}
// GetFormResponses sends a response to a form
func (s *Service) GetFormResponses(formId string) ([]Response, error) {
if len(s.Backends) == 0 {
return nil, errors.New("not enough backends")
}
responses, err := s.Backends[0].GetFormResponses(formId)
if err != nil {
return nil, err
}
return responses, nil
}
// AddFormBackend adds a backend to a form
func (s *Service) AddFormBackend(formid string, b Backend) error {
if len(s.Backends) == 0 {
return errors.New("not enough backends")
}
if err := s.Backends[0].AddFormBackend(formid, b); err != nil {
return err
}
return nil
}
// AddFormBackend adds a backend to a form
func (s *Service) GetFormBackends(formid string) ([]Backend, error) {
if len(s.Backends) == 0 {
return nil, errors.New("not enough backends")
}
backends, err := s.Backends[0].GetFormBackends(formid)
if err != nil {
return nil, err
}
return backends, nil
}