-
Notifications
You must be signed in to change notification settings - Fork 1
/
possible_operational_solutionStore.go
184 lines (139 loc) · 4.96 KB
/
possible_operational_solutionStore.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
package storage
import (
"database/sql"
"errors"
"fmt"
"go.uber.org/zap"
"github.com/google/uuid"
"github.com/cmsgov/mint-app/pkg/apperrors"
"github.com/cmsgov/mint-app/pkg/models"
_ "embed"
)
//go:embed SQL/possible_operational_solution/collection_get_by_need_type.sql
var possibleOperationalSolutionCollectionByNeedTypeSQL string
//go:embed SQL/possible_operational_solution/collection_get_all.sql
var possibleOperationalSolutionCollectionGelAllSQL string
//go:embed SQL/possible_operational_solution/collection_get_by_operational_need_id.sql
var possibleOperationalSolutionCollectionByOperationalNeedIDSQL string
//go:embed SQL/possible_operational_solution/get_by_id.sql
var possibleOperationalSolutionGetByIDSQL string
//go:embed SQL/possible_operational_solution/get_by_key.sql
var possibleOperationalSolutionGetByKeySQL string
// PossibleOperationalSolutionCollectionGetByNeedType returns possible
// operational solutions for a given operational need
func (s *Store) PossibleOperationalSolutionCollectionGetByNeedType(
_ *zap.Logger,
needKey models.OperationalNeedKey,
) ([]*models.PossibleOperationalSolution, error) {
var posSols []*models.PossibleOperationalSolution
stmt, err := s.db.PrepareNamed(possibleOperationalSolutionCollectionByNeedTypeSQL)
if err != nil {
return nil, err
}
defer stmt.Close()
arg := map[string]interface{}{
"need_key": needKey,
}
err = stmt.Select(&posSols, arg) // This returns more than one
if err != nil {
return nil, err
}
return posSols, nil
}
// PossibleOperationalSolutionCollectionGetAll returns all possible operational solutions
func (s *Store) PossibleOperationalSolutionCollectionGetAll(_ *zap.Logger) (
[]*models.PossibleOperationalSolution,
error,
) {
var posSols []*models.PossibleOperationalSolution
stmt, err := s.db.PrepareNamed(possibleOperationalSolutionCollectionGelAllSQL)
if err != nil {
return nil, err
}
arg := map[string]interface{}{}
err = stmt.Select(&posSols, arg) // This returns more than one
if err != nil {
return nil, err
}
return posSols, nil
}
// PossibleOperationalSolutionCollectionGetByOperationalNeedID returns possible
// operational solutions for a given operational need
func (s *Store) PossibleOperationalSolutionCollectionGetByOperationalNeedID(
_ *zap.Logger,
operationalNeedID uuid.UUID,
) ([]*models.PossibleOperationalSolution, error) {
var posSols []*models.PossibleOperationalSolution
stmt, err := s.db.PrepareNamed(possibleOperationalSolutionCollectionByOperationalNeedIDSQL)
if err != nil {
return nil, err
}
defer stmt.Close()
arg := map[string]interface{}{
"operational_need_id": operationalNeedID,
}
err = stmt.Select(&posSols, arg) // This returns more than one
if err != nil {
return nil, err
}
return posSols, nil
}
// PossibleOperationalSolutionGetByID returns a possible solution associated to a specific id
func (s *Store) PossibleOperationalSolutionGetByID(logger *zap.Logger, id int) (*models.PossibleOperationalSolution, error) {
opSol := models.PossibleOperationalSolution{}
stmt, err := s.db.PrepareNamed(possibleOperationalSolutionGetByIDSQL)
if err != nil {
return nil, fmt.Errorf("failed to prepare SQL statement: %w", err)
}
defer stmt.Close()
arg := map[string]interface{}{"id": id}
err = stmt.Get(&opSol, arg)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
logger.Warn("no possible solution was found for the given ID : %s",
zap.Int("id", id))
return nil, fmt.Errorf("no possible solution plan found for the given id: %w", err)
}
logger.Error(
"failed to fetch possible operational solution",
zap.Error(err),
zap.Int("id", id),
)
return nil, &apperrors.QueryError{
Err: fmt.Errorf("failed to fetch the possible operational solution: %w", err),
Model: opSol,
Operation: apperrors.QueryFetch,
}
}
return &opSol, nil
}
// PossibleOperationalSolutionGetByKey returns a possible solution associated to a specific id
func (s *Store) PossibleOperationalSolutionGetByKey(logger *zap.Logger, solKey models.OperationalSolutionKey) (*models.PossibleOperationalSolution, error) {
//TODO: restructure as data-loaders
opSol := models.PossibleOperationalSolution{}
stmt, err := s.db.PrepareNamed(possibleOperationalSolutionGetByKeySQL)
if err != nil {
return nil, fmt.Errorf("failed to prepare SQL statement: %w", err)
}
defer stmt.Close()
arg := map[string]interface{}{"sol_key": solKey}
err = stmt.Get(&opSol, arg)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
logger.Warn("no possible solution was found for the given key : %s",
zap.Any("sol_key", solKey))
return nil, fmt.Errorf("no possible solution plan found for the given id: %w", err)
}
logger.Error(
"failed to fetch possible operational solution",
zap.Error(err),
zap.Any("sol_key", solKey),
)
return nil, &apperrors.QueryError{
Err: fmt.Errorf("failed to fetch the possible operational solution: %w", err),
Model: opSol,
Operation: apperrors.QueryFetch,
}
}
return &opSol, nil
}