Skip to content

Commit

Permalink
chore: validate movie and generate uuid
Browse files Browse the repository at this point in the history
Create method validation for movie, and update the datasource to
generate an uuid in case of empty id.
  • Loading branch information
faabiosr committed Jul 13, 2023
1 parent 0e1b195 commit 5cecef6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module github.com/faabiosr/go-movies-demo

go 1.20

require go.etcd.io/bbolt v1.3.7
require (
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
github.com/gofrs/uuid/v5 v5.0.0
go.etcd.io/bbolt v1.3.7
)

require golang.org/x/sys v0.4.0 // indirect
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 h1:zV3ejI06GQ59hwDQAvmK1qxOQGB3WuVTRoY0okPTAv0=
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 h1:byhDUpfEwjsVQb1vBunvIjh2BHQ9ead57VkAEY4V+Es=
github.com/go-ozzo/ozzo-validation/v4 v4.3.0/go.mod h1:2NKgrcHl3z6cJs+3Oo940FPRiTzuqKbvfrL2RxCj6Ew=
github.com/gofrs/uuid/v5 v5.0.0 h1:p544++a97kEL+svbcFbCQVM9KFu0Yo25UoISXGNNH9M=
github.com/gofrs/uuid/v5 v5.0.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
go.etcd.io/bbolt v1.3.7 h1:j+zJOnnEjF/kyHlDDgGnVL/AIqIJPq8UoB2GSNfkUfQ=
go.etcd.io/bbolt v1.3.7/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
5 changes: 5 additions & 0 deletions internal/movies/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"path/filepath"

"github.com/gofrs/uuid/v5"
bolt "go.etcd.io/bbolt"
)

Expand Down Expand Up @@ -33,6 +34,10 @@ func NewDatasource(path string) (*Datasource, error) {
// Store stores the movie content into the database, if some error occurs during
// the storing, the error will be returned.
func (ds *Datasource) Store(m Movie) error {
if m.ID == "" {
m.ID = uuid.Must(uuid.NewV4()).String()
}

return ds.db.Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists(bucketName)
if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions internal/movies/movie.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package movies

import (
"encoding/json"
"errors"
"time"

v "github.com/go-ozzo/ozzo-validation/v4"
)

// Date it is a custom struct for masharlling and unmarshalling dates.
Expand Down Expand Up @@ -46,6 +49,15 @@ func (dt *Date) UnmarshalJSON(value []byte) error {
return nil
}

// Validate validates if the date is zero.
func (dt Date) Validate() error {
if dt.IsZero() {
return errors.New("cannot be blank")
}

return nil
}

// Movie represents a movie in the system.
type Movie struct {
ID string `json:"id"`
Expand All @@ -55,3 +67,11 @@ type Movie struct {

// Movies represents a collection of movie.
type Movies []Movie

// Validate validates the movie structure.
func (m Movie) Validate() error {
return v.ValidateStruct(&m,
v.Field(&m.Title, v.Required, v.Length(3, 0)), // nolint:gomnd
v.Field(&m.Released, v.Required),
)
}
15 changes: 15 additions & 0 deletions internal/movies/movie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,18 @@ func TestDateUnmarshalJSON(t *testing.T) {
})
}
}

func TestMovieValidation(t *testing.T) {
m := Movie{}

if err := m.Validate(); err == nil {
t.Error("expected an error, got nil")
}

m.Title = "Spider-Man: Into the Spider-Verse"
m.Released, _ = NewDate("2018-12-21")

if err := m.Validate(); err != nil {
t.Errorf("expected nil, got %v", err)
}
}

0 comments on commit 5cecef6

Please sign in to comment.