-
Notifications
You must be signed in to change notification settings - Fork 11
/
trb_lead_option.go
104 lines (90 loc) · 2.56 KB
/
trb_lead_option.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
package storage
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/google/uuid"
"go.uber.org/zap"
"github.com/cmsgov/easi-app/pkg/appcontext"
"github.com/cmsgov/easi-app/pkg/apperrors"
"github.com/cmsgov/easi-app/pkg/models"
)
// CreateTRBLeadOption creates a new TRB lead option record in the database
func (s *Store) CreateTRBLeadOption(ctx context.Context, leadOption *models.TRBLeadOption) (*models.TRBLeadOption, error) {
leadOption.ID = uuid.New()
stmt, err := s.db.PrepareNamed(`
INSERT INTO trb_lead_options (
id,
eua_user_id,
created_by,
modified_by
)
VALUES (
:id,
:eua_user_id,
:created_by,
:modified_by
)
RETURNING *;`)
if err != nil {
appcontext.ZLogger(ctx).Error(
fmt.Sprintf("Failed to create TRB lead option with error %s", err),
zap.String("id", leadOption.ID.String()),
)
return nil, err
}
defer stmt.Close()
created := models.TRBLeadOption{}
err = stmt.Get(&created, leadOption)
if err != nil {
appcontext.ZLogger(ctx).Error("Failed to create TRB lead option with error %s", zap.Error(err))
return nil, err
}
return &created, nil
}
// DeleteTRBLeadOption deletes an existing TRB lead option record in the database
func (s *Store) DeleteTRBLeadOption(ctx context.Context, euaID string) (*models.TRBLeadOption, error) {
stmt, err := s.db.PrepareNamed(`
DELETE FROM trb_lead_options
WHERE eua_user_id = :eua_user_id
RETURNING *;`)
if err != nil {
appcontext.ZLogger(ctx).Error(
fmt.Sprintf("Failed to update TRB lead option %s", err),
zap.String("id", euaID),
)
return nil, err
}
defer stmt.Close()
toDelete := models.TRBLeadOption{}
toDelete.EUAUserID = euaID
deleted := models.TRBLeadOption{}
err = stmt.Get(&deleted, &toDelete)
if err != nil {
appcontext.ZLogger(ctx).Error(
fmt.Sprintf("Failed to delete TRB lead option %s", err),
zap.String("id", euaID),
)
return nil, &apperrors.QueryError{
Err: err,
Model: toDelete,
Operation: apperrors.QueryUpdate,
}
}
return &deleted, err
}
// GetTRBLeadOptions retrieves all TRB lead option records from the database
func (s *Store) GetTRBLeadOptions(ctx context.Context) ([]*models.TRBLeadOption, error) {
results := []*models.TRBLeadOption{}
err := s.db.Select(&results, `SELECT * FROM trb_lead_options`)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
appcontext.ZLogger(ctx).Error("Failed to fetch TRB lead options", zap.Error(err))
return nil, &apperrors.QueryError{
Err: err,
Model: models.TRBLeadOption{},
Operation: apperrors.QueryFetch,
}
}
return results, nil
}