/
operational_solutionStore.go
117 lines (90 loc) · 3.14 KB
/
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
package storage
import (
"github.com/google/uuid"
"go.uber.org/zap"
"github.com/cmsgov/mint-app/pkg/models"
"github.com/cmsgov/mint-app/pkg/shared/utilityUUID"
"github.com/cmsgov/mint-app/pkg/storage/genericmodel"
_ "embed"
)
//go:embed SQL/operational_solution/and_possible_get_by_operational_need_id_LOADER.sql
var operationalSolutionAndPossibleGetByOperationalNeedIDLOADERSQL string
//go:embed SQL/operational_solution/get_by_id.sql
var operationalSolutionGetByIDSQL string
//go:embed SQL/operational_solution/update_by_id.sql
var operationalSolutionUpdateByIDSQL string
//go:embed SQL/operational_solution/insert.sql
var operationalSolutionInsertSQL string
// OperationalSolutionAndPossibleCollectionGetByOperationalNeedIDLOADER returns
// Operational Solutions that match the paramTable JSON
func (s *Store) OperationalSolutionAndPossibleCollectionGetByOperationalNeedIDLOADER(
_ *zap.Logger,
paramTableJSON string,
) ([]*models.OperationalSolution, error) {
var solutions []*models.OperationalSolution
stmt, err := s.db.PrepareNamed(operationalSolutionAndPossibleGetByOperationalNeedIDLOADERSQL)
if err != nil {
return nil, err
}
defer stmt.Close()
arg := map[string]interface{}{
"paramTableJSON": paramTableJSON,
}
err = stmt.Select(&solutions, arg) //this returns more than one
if err != nil {
return nil, err
}
return solutions, nil
}
// OperationalSolutionGetByID returns an operational solution by ID
func (s *Store) OperationalSolutionGetByID(_ *zap.Logger, id uuid.UUID) (*models.OperationalSolution, error) {
solution := models.OperationalSolution{}
stmt, err := s.db.PrepareNamed(operationalSolutionGetByIDSQL)
if err != nil {
return nil, err
}
defer stmt.Close()
arg := map[string]interface{}{
"id": id,
}
err = stmt.Get(&solution, arg)
if err != nil {
return nil, err
}
return &solution, err
}
// OperationalSolutionInsert inserts an operational solution if it already exists
func (s *Store) OperationalSolutionInsert(
logger *zap.Logger,
solution *models.OperationalSolution,
solutionTypeKey *models.OperationalSolutionKey,
) (*models.OperationalSolution, error) {
stmt, err := s.db.PrepareNamed(operationalSolutionInsertSQL)
if err != nil {
return nil, genericmodel.HandleModelUpdateError(logger, err, solution)
}
defer stmt.Close()
solution.ID = utilityUUID.ValueOrNewUUID(solution.ID)
solution.Key = solutionTypeKey
err = stmt.Get(solution, solution)
if err != nil {
return nil, genericmodel.HandleModelCreationError(logger, err, solution) //this could be either update or insert..
}
return solution, err
}
// OperationalSolutionUpdateByID updates an operational solution by its ID
func (s *Store) OperationalSolutionUpdateByID(
logger *zap.Logger,
solution *models.OperationalSolution,
) (*models.OperationalSolution, error) {
stmt, err := s.db.PrepareNamed(operationalSolutionUpdateByIDSQL)
if err != nil {
return nil, genericmodel.HandleModelUpdateError(logger, err, solution)
}
defer stmt.Close()
err = stmt.Get(solution, solution)
if err != nil {
return nil, genericmodel.HandleModelUpdateError(logger, err, solution) //this could be either update or insert..
}
return solution, err
}