/
movie.go
438 lines (387 loc) · 12.3 KB
/
movie.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package service
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v4"
"github.com/gilcrest/diygoapi"
"github.com/gilcrest/diygoapi/errs"
"github.com/gilcrest/diygoapi/secure"
"github.com/gilcrest/diygoapi/sqldb/datastore"
)
// movieAudit is the combination of a domain Movie and its audit data
type movieAudit struct {
Movie diygoapi.Movie
SimpleAudit diygoapi.SimpleAudit
}
// newMovieResponse initializes MovieResponse
func newMovieResponse(ma movieAudit) *diygoapi.MovieResponse {
return &diygoapi.MovieResponse{
ExternalID: ma.Movie.ExternalID.String(),
Title: ma.Movie.Title,
Rated: ma.Movie.Rated,
Released: ma.Movie.Released.Format(time.RFC3339),
RunTime: ma.Movie.RunTime,
Director: ma.Movie.Director,
Writer: ma.Movie.Writer,
CreateAppExtlID: ma.SimpleAudit.Create.App.ExternalID.String(),
CreateUserFirstName: ma.SimpleAudit.Create.User.FirstName,
CreateUserLastName: ma.SimpleAudit.Create.User.LastName,
CreateDateTime: ma.SimpleAudit.Create.Moment.Format(time.RFC3339),
UpdateAppExtlID: ma.SimpleAudit.Update.App.ExternalID.String(),
UpdateUserFirstName: ma.SimpleAudit.Update.User.FirstName,
UpdateUserLastName: ma.SimpleAudit.Update.User.LastName,
UpdateDateTime: ma.SimpleAudit.Update.Moment.Format(time.RFC3339),
}
}
// MovieService is a service for creating a Movie
type MovieService struct {
Datastorer diygoapi.Datastorer
}
// Create is used to create a Movie
func (s *MovieService) Create(ctx context.Context, r *diygoapi.CreateMovieRequest, adt diygoapi.Audit) (mr *diygoapi.MovieResponse, err error) {
const op errs.Op = "service/MovieService.Create"
if r == nil {
return nil, errs.E(op, errs.Validation, "CreateMovieRequest must have a value when creating a Movie")
}
var released time.Time
released, err = time.Parse(time.RFC3339, r.Released)
if err != nil {
return nil, errs.E(op, errs.Validation,
errs.Code("invalid_date_format"),
errs.Parameter("release_date"),
err)
}
// initialize Movie and inject dependent fields
m := diygoapi.Movie{
ID: uuid.New(),
ExternalID: secure.NewID(),
Title: r.Title,
Rated: r.Rated,
Released: released,
RunTime: r.RunTime,
Director: r.Director,
Writer: r.Writer,
}
sa := diygoapi.SimpleAudit{
Create: adt,
Update: adt,
}
err = m.IsValid()
if err != nil {
return nil, errs.E(op, err)
}
createMovieParams := datastore.CreateMovieParams{
MovieID: m.ID,
ExtlID: m.ExternalID.String(),
Title: m.Title,
Rated: diygoapi.NewNullString(m.Rated),
Released: diygoapi.NewNullTime(released),
RunTime: diygoapi.NewNullInt32(int32(m.RunTime)),
Director: diygoapi.NewNullString(m.Director),
Writer: diygoapi.NewNullString(m.Writer),
CreateAppID: sa.Create.App.ID,
CreateUserID: sa.Create.User.NullUUID(),
CreateTimestamp: sa.Create.Moment,
UpdateAppID: sa.Update.App.ID,
UpdateUserID: sa.Update.User.NullUUID(),
UpdateTimestamp: sa.Update.Moment,
}
// start db txn using pgxpool
var tx pgx.Tx
tx, err = s.Datastorer.BeginTx(ctx)
if err != nil {
return nil, errs.E(op, err)
}
// defer transaction rollback and handle error, if any
defer func() {
err = s.Datastorer.RollbackTx(ctx, tx, err)
}()
_, err = datastore.New(tx).CreateMovie(ctx, createMovieParams)
if err != nil {
return nil, errs.E(op, errs.Database, err)
}
// commit db txn using pgxpool
err = s.Datastorer.CommitTx(ctx, tx)
if err != nil {
return nil, errs.E(op, err)
}
mr = newMovieResponse(movieAudit{m, sa})
return mr, nil
}
// Update is used to update a movie
func (s *MovieService) Update(ctx context.Context, r *diygoapi.UpdateMovieRequest, adt diygoapi.Audit) (mr *diygoapi.MovieResponse, err error) {
const op errs.Op = "service/MovieService.Update"
var released time.Time
released, err = time.Parse(time.RFC3339, r.Released)
if err != nil {
return nil, errs.E(op, errs.Validation,
errs.Code("invalid_date_format"),
errs.Parameter("release_date"),
err)
}
// start db txn using pgxpool
var tx pgx.Tx
tx, err = s.Datastorer.BeginTx(ctx)
if err != nil {
return nil, errs.E(op, err)
}
// defer transaction rollback and handle error, if any
defer func() {
err = s.Datastorer.RollbackTx(ctx, tx, err)
}()
// retrieve existing Movie
var row datastore.FindMovieByExternalIDWithAuditRow
row, err = datastore.New(tx).FindMovieByExternalIDWithAudit(ctx, r.ExternalID)
if err != nil {
if err == pgx.ErrNoRows {
return nil, errs.E(op, errs.Validation, "No movie exists for the given external ID")
}
return nil, errs.E(op, errs.Database, err)
}
m := diygoapi.Movie{
ID: row.MovieID,
ExternalID: secure.MustParseIdentifier(row.ExtlID),
Title: row.Title,
Rated: row.Rated.String,
Released: row.Released.Time,
RunTime: int(row.RunTime.Int32),
Director: row.Director.String,
Writer: row.Writer.String,
}
// update fields from request
m.Title = r.Title
m.Rated = r.Rated
m.Released = released
m.RunTime = r.RunTime
m.Director = r.Director
m.Writer = r.Writer
err = m.IsValid()
if err != nil {
return nil, errs.E(op, err)
}
sa := diygoapi.SimpleAudit{
Create: diygoapi.Audit{
App: &diygoapi.App{
ID: row.CreateAppID,
ExternalID: secure.MustParseIdentifier(row.CreateAppExtlID),
Org: &diygoapi.Org{ID: row.CreateAppOrgID},
Name: row.CreateAppName,
Description: row.CreateAppDescription,
APIKeys: nil,
},
User: &diygoapi.User{
ID: row.CreateUserID.UUID,
FirstName: row.CreateUserFirstName.String,
LastName: row.CreateUserLastName.String,
},
Moment: row.CreateTimestamp,
},
}
// update audit with latest
sa.Update = adt
updateMovieParams := datastore.UpdateMovieParams{
Title: m.Title,
Rated: diygoapi.NewNullString(m.Rated),
Released: diygoapi.NewNullTime(released),
RunTime: diygoapi.NewNullInt32(int32(m.RunTime)),
Director: diygoapi.NewNullString(m.Director),
Writer: diygoapi.NewNullString(m.Writer),
UpdateAppID: adt.App.ID,
UpdateUserID: adt.User.NullUUID(),
UpdateTimestamp: adt.Moment,
MovieID: m.ID,
}
err = datastore.New(tx).UpdateMovie(ctx, updateMovieParams)
if err != nil {
return nil, errs.E(op, errs.Database, err)
}
// commit db txn using pgxpool
err = s.Datastorer.CommitTx(ctx, tx)
if err != nil {
return nil, errs.E(op, err)
}
mr = newMovieResponse(movieAudit{m, sa})
return mr, nil
}
// Delete is used to delete a movie
func (s *MovieService) Delete(ctx context.Context, extlID string) (dr diygoapi.DeleteResponse, err error) {
const op errs.Op = "service/MovieService.Delete"
// start db txn using pgxpool
var tx pgx.Tx
tx, err = s.Datastorer.BeginTx(ctx)
if err != nil {
return diygoapi.DeleteResponse{}, errs.E(op, err)
}
// defer transaction rollback and handle error, if any
defer func() {
err = s.Datastorer.RollbackTx(ctx, tx, err)
}()
// retrieve existing Movie
var dbm datastore.Movie
dbm, err = datastore.New(tx).FindMovieByExternalID(ctx, extlID)
if err != nil {
if err == pgx.ErrNoRows {
return diygoapi.DeleteResponse{}, errs.E(op, errs.Validation, "No movie exists for the given external ID")
}
return diygoapi.DeleteResponse{}, errs.E(op, errs.Database, err)
}
var rowsAffected int64
rowsAffected, err = datastore.New(tx).DeleteMovie(ctx, dbm.MovieID)
if err != nil {
return diygoapi.DeleteResponse{}, errs.E(op, errs.Database, err)
}
if rowsAffected != 1 {
return diygoapi.DeleteResponse{}, errs.E(op, errs.Database, fmt.Sprintf("rows affected should be 1, actual: %d", rowsAffected))
}
// commit db txn using pgxpool
err = s.Datastorer.CommitTx(ctx, tx)
if err != nil {
return diygoapi.DeleteResponse{}, errs.E(op, err)
}
response := diygoapi.DeleteResponse{
ExternalID: dbm.ExtlID,
Deleted: true,
}
return response, nil
}
// FindMovieByExternalID is used to find an individual movie
func (s *MovieService) FindMovieByExternalID(ctx context.Context, extlID string) (mr *diygoapi.MovieResponse, err error) {
const op errs.Op = "service/MovieService.FindMovieByExternalID"
// start db txn using pgxpool
var tx pgx.Tx
tx, err = s.Datastorer.BeginTx(ctx)
if err != nil {
return nil, errs.E(op, err)
}
// defer transaction rollback and handle error, if any
defer func() {
err = s.Datastorer.RollbackTx(ctx, tx, err)
}()
var row datastore.FindMovieByExternalIDWithAuditRow
row, err = datastore.New(tx).FindMovieByExternalIDWithAudit(ctx, extlID)
if err != nil {
if err == pgx.ErrNoRows {
return nil, errs.E(op, errs.Validation, "no movie exists for the given external ID")
}
return nil, errs.E(op, errs.Database, err)
}
m := diygoapi.Movie{
ID: row.MovieID,
ExternalID: secure.MustParseIdentifier(row.ExtlID),
Title: row.Title,
Rated: row.Rated.String,
Released: row.Released.Time,
RunTime: int(row.RunTime.Int32),
Director: row.Director.String,
Writer: row.Writer.String,
}
sa := diygoapi.SimpleAudit{
Create: diygoapi.Audit{
App: &diygoapi.App{
ID: row.CreateAppID,
ExternalID: secure.MustParseIdentifier(row.CreateAppExtlID),
Org: &diygoapi.Org{ID: row.CreateAppOrgID},
Name: row.CreateAppName,
Description: row.CreateAppDescription,
APIKeys: nil,
},
User: &diygoapi.User{
ID: row.CreateUserID.UUID,
FirstName: row.CreateUserFirstName.String,
LastName: row.CreateUserLastName.String,
},
Moment: row.CreateTimestamp,
},
Update: diygoapi.Audit{
App: &diygoapi.App{
ID: row.UpdateAppID,
ExternalID: secure.MustParseIdentifier(row.UpdateAppExtlID),
Org: &diygoapi.Org{ID: row.UpdateAppOrgID},
Name: row.UpdateAppName,
Description: row.UpdateAppDescription,
APIKeys: nil,
},
User: &diygoapi.User{
ID: row.UpdateUserID.UUID,
FirstName: row.UpdateUserFirstName.String,
LastName: row.UpdateUserLastName.String,
},
Moment: row.UpdateTimestamp,
},
}
mr = newMovieResponse(movieAudit{m, sa})
return mr, nil
}
// FindAllMovies is used to list all movies in the db
func (s *MovieService) FindAllMovies(ctx context.Context) (smr []*diygoapi.MovieResponse, err error) {
const op errs.Op = "service/MovieService.FindAllMovies"
// start db txn using pgxpool
var tx pgx.Tx
tx, err = s.Datastorer.BeginTx(ctx)
if err != nil {
return nil, errs.E(op, err)
}
// defer transaction rollback and handle error, if any
defer func() {
err = s.Datastorer.RollbackTx(ctx, tx, err)
}()
var rows []datastore.FindMoviesRow
rows, err = datastore.New(tx).FindMovies(ctx)
if err != nil {
if err == pgx.ErrNoRows {
return nil, errs.E(op, errs.Validation, "no movies exists")
}
return nil, errs.E(op, errs.Database, err)
}
for _, row := range rows {
m := diygoapi.Movie{
ID: row.MovieID,
ExternalID: secure.MustParseIdentifier(row.ExtlID),
Title: row.Title,
Rated: row.Rated.String,
Released: row.Released.Time,
RunTime: int(row.RunTime.Int32),
Director: row.Director.String,
Writer: row.Writer.String,
}
sa := diygoapi.SimpleAudit{
Create: diygoapi.Audit{
App: &diygoapi.App{
ID: row.CreateAppID,
ExternalID: secure.MustParseIdentifier(row.CreateAppExtlID),
Org: &diygoapi.Org{ID: row.CreateAppOrgID},
Name: row.CreateAppName,
Description: row.CreateAppDescription,
APIKeys: nil,
},
User: &diygoapi.User{
ID: row.CreateUserID.UUID,
FirstName: row.CreateUserFirstName.String,
LastName: row.CreateUserLastName.String,
},
Moment: row.CreateTimestamp,
},
Update: diygoapi.Audit{
App: &diygoapi.App{
ID: row.UpdateAppID,
ExternalID: secure.MustParseIdentifier(row.UpdateAppExtlID),
Org: &diygoapi.Org{ID: row.UpdateAppOrgID},
Name: row.UpdateAppName,
Description: row.UpdateAppDescription,
APIKeys: nil,
},
User: &diygoapi.User{
ID: row.UpdateUserID.UUID,
FirstName: row.UpdateUserLastName.String,
LastName: row.UpdateUserLastName.String,
},
Moment: row.UpdateTimestamp,
},
}
mr := newMovieResponse(movieAudit{m, sa})
smr = append(smr, mr)
}
return smr, nil
}