-
Notifications
You must be signed in to change notification settings - Fork 1
/
backends.go
135 lines (112 loc) · 3.06 KB
/
backends.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
package forms
import (
"errors"
"fmt"
)
// Backend is the interface for backends (storage, data handling)
type Backend interface {
Type() string
Configuration() map[string]interface{}
AddForm(Form) error
DeleteForm(string) error
GetForms() ([]Form, error)
GetForm(string) (Form, error)
SubmitResponse(Form, Response) error
GetFormResponses(string) ([]Response, error)
AddFormBackend(string, Backend) error
GetFormBackends(string) ([]Backend, error)
}
// NoBackend is a fake backend implementation
type NoBackend struct {
}
// TempBackend is an in-memory temporary backend primarily used for testing and development purposes
type TempBackend struct {
Forms map[string]Form
Responses map[string][]Response
}
func (b TempBackend) Type() string {
return "Temp"
}
func (b TempBackend) Configuration() map[string]interface{} {
return map[string]interface{}{
"type": b.Type(),
}
}
// NewTempBackend initializes a new empty TempBackend
func NewTempBackend() TempBackend {
return TempBackend{
Forms: map[string]Form{},
Responses: map[string][]Response{},
}
}
// AddForm creates a new form in the backend
func (b TempBackend) AddForm(f Form) error {
if b.Forms[f.ID()] != nil {
return errors.New("a form already exists with this id")
}
b.Forms[f.ID()] = f
fmt.Println(b)
return nil
}
// DeleteForm removes the form from the backend using formIs
func (b TempBackend) DeleteForm(formId string) error {
if b.Forms[formId] == nil {
return errors.New("form does not exist")
}
delete(b.Forms, formId)
return nil
}
// GetForms retrieves all forms from this backend
func (b TempBackend) GetForms() ([]Form, error) {
result := []Form{}
for _, f := range b.Forms {
result = append(result, f)
}
return result, nil
}
// GetForm retrieves the form from the backend given a formId
func (b TempBackend) GetForm(formId string) (Form, error) {
for _, f := range b.Forms {
if f.ID() == formId {
return f, nil
}
}
return nil, errors.New("form not found")
}
func (b TempBackend) SubmitResponse(form Form, response Response) error {
if b.Forms[form.ID()] == nil {
return errors.New("form not found")
}
if b.Responses[form.ID()] == nil {
b.Responses[form.ID()] = []Response{response}
return nil
}
b.Responses[form.ID()] = append(b.Responses[form.ID()], response)
return nil
}
func (b TempBackend) GetFormResponses(formId string) ([]Response, error) {
if b.Forms[formId] == nil {
return []Response{}, errors.New("form not found")
}
if b.Responses[formId] == nil {
return []Response{}, nil
}
return b.Responses[formId], nil
}
// AddFormBackend adds a backend to a form
func (b TempBackend) AddFormBackend(formid string, backend Backend) error {
if b.Forms[formid] == nil {
return errors.New("form not found")
}
if err := b.Forms[formid].AddBackend(backend); err != nil {
return err
}
return nil
}
// GetFormBackends returns all the backends for one form
func (b TempBackend) GetFormBackends(formid string) ([]Backend, error) {
if b.Forms[formid] == nil {
return nil, errors.New("form not found")
}
return b.Forms[formid].GetAdditionalBackends(), nil
}