/
repository.go
194 lines (158 loc) · 5.25 KB
/
repository.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
185
186
187
188
189
190
191
192
193
194
package postgres
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/CMSgov/bcda-app/bcda/models"
"github.com/CMSgov/bcda-app/bcdaworker/repository"
"github.com/huandu/go-sqlbuilder"
"github.com/pborman/uuid"
)
type queryable interface {
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}
type executable interface {
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
}
const (
sqlFlavor = sqlbuilder.PostgreSQL
)
// Ensure Repository satisfies the interface
var _ repository.Repository = &Repository{}
type Repository struct {
queryable
executable
}
func NewRepository(db *sql.DB) *Repository {
return &Repository{db, db}
}
func NewRepositoryTx(tx *sql.Tx) *Repository {
return &Repository{tx, tx}
}
func (r *Repository) GetACOByUUID(ctx context.Context, uuid uuid.UUID) (*models.ACO, error) {
sb := sqlFlavor.NewSelectBuilder().Select("id", "uuid", "cms_id", "name").From("acos")
sb.Where(sb.Equal("uuid", uuid))
query, args := sb.Build()
row := r.QueryRowContext(ctx, query, args...)
var (
aco models.ACO
name, cmsID sql.NullString
)
err := row.Scan(&aco.ID, &aco.UUID, &cmsID, &name)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, fmt.Errorf("no ACO record found for uuid %s", uuid)
}
return nil, err
}
aco.Name, aco.CMSID = name.String, &cmsID.String
return &aco, nil
}
func (r *Repository) GetCCLFBeneficiaryByID(ctx context.Context, id uint) (*models.CCLFBeneficiary, error) {
sb := sqlFlavor.NewSelectBuilder()
sb.Select("id", "file_id", "mbi", "blue_button_id")
sb.From("cclf_beneficiaries").Where(sb.Equal("id", id))
query, args := sb.Build()
row := r.QueryRowContext(ctx, query, args...)
var (
bene models.CCLFBeneficiary
bbID sql.NullString
)
if err := row.Scan(&bene.ID, &bene.FileID, &bene.MBI, &bbID); err != nil {
return nil, err
}
bene.BlueButtonID = bbID.String
return &bene, nil
}
func (r *Repository) GetJobByID(ctx context.Context, jobID uint) (*models.Job, error) {
sb := sqlFlavor.NewSelectBuilder()
sb.Select("id", "aco_id", "request_url", "status", "transaction_time", "job_count", "completed_job_count", "created_at", "updated_at")
sb.From("jobs").Where(sb.Equal("id", jobID))
query, args := sb.Build()
var (
j models.Job
transactionTime, createdAt, updatedAt sql.NullTime
)
err := r.QueryRowContext(ctx, query, args...).Scan(&j.ID, &j.ACOID, &j.RequestURL, &j.Status, &transactionTime,
&j.JobCount, &j.CompletedJobCount, &createdAt, &updatedAt)
j.TransactionTime, j.CreatedAt, j.UpdatedAt = transactionTime.Time, createdAt.Time, updatedAt.Time
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, repository.ErrJobNotFound
}
return nil, err
}
return &j, nil
}
func (r *Repository) UpdateJobStatus(ctx context.Context, jobID uint, new models.JobStatus) error {
return r.updateJob(ctx,
map[string]interface{}{"id": jobID},
map[string]interface{}{"status": new})
}
func (r *Repository) UpdateJobStatusCheckStatus(ctx context.Context, jobID uint, current, new models.JobStatus) error {
return r.updateJob(ctx,
map[string]interface{}{"id": jobID, "status": current},
map[string]interface{}{"status": new})
}
func (r *Repository) IncrementCompletedJobCount(ctx context.Context, jobID uint) error {
ub := sqlFlavor.NewUpdateBuilder().Update("jobs")
ub.Set(ub.Incr("completed_job_count"), ub.Assign("updated_at", sqlbuilder.Raw("NOW()"))).
Where(ub.Equal("id", jobID))
query, args := ub.Build()
res, err := r.ExecContext(ctx, query, args...)
if err != nil {
return err
}
count, err := res.RowsAffected()
if err != nil {
return err
}
if count == 0 {
return fmt.Errorf("job %d not updated, no job found", jobID)
}
return nil
}
func (r *Repository) CreateJobKey(ctx context.Context, jobKey models.JobKey) error {
ib := sqlFlavor.NewInsertBuilder().InsertInto("job_keys")
ib.Cols("job_id", "file_name", "resource_type").
Values(jobKey.JobID, jobKey.FileName, jobKey.ResourceType)
query, args := ib.Build()
_, err := r.ExecContext(ctx, query, args...)
return err
}
func (r *Repository) GetJobKeyCount(ctx context.Context, jobID uint) (int, error) {
sb := sqlFlavor.NewSelectBuilder().Select("COUNT(1)").From("job_keys")
sb.Where(sb.Equal("job_id", jobID))
sb.Where(sb.NotLike("file_name", "%-error.ndjson%")) //Ignore error files from completed count.
query, args := sb.Build()
var count int
if err := r.QueryRowContext(ctx, query, args...).Scan(&count); err != nil {
return -1, err
}
return count, nil
}
func (r *Repository) updateJob(ctx context.Context, clauses map[string]interface{}, fieldAndValues map[string]interface{}) error {
ub := sqlFlavor.NewUpdateBuilder().Update("jobs")
ub.Set(ub.Assign("updated_at", sqlbuilder.Raw("NOW()")))
for field, value := range fieldAndValues {
ub.SetMore(ub.Assign(field, value))
}
for field, value := range clauses {
ub.Where(ub.Equal(field, value))
}
query, args := ub.Build()
result, err := r.ExecContext(ctx, query, args...)
if err != nil {
return err
}
affected, err := result.RowsAffected()
if err != nil {
return err
}
if affected == 0 {
return repository.ErrJobNotUpdated
}
return nil
}