diff --git a/backend/models/movies.go b/backend/models/movies.go index ee71748..e96122e 100644 --- a/backend/models/movies.go +++ b/backend/models/movies.go @@ -1,6 +1,7 @@ package models import ( + "database/sql" "fmt" "time" @@ -34,7 +35,6 @@ type movieDB struct { ID string `db:"id"` ImdbID string `db:"imdb_id"` TmdbID int `db:"tmdb_id"` - UserID *string `db:"user_id"` Title string `db:"title"` OriginalTitle string `db:"original_title"` SortTitle string `db:"sort_title"` @@ -49,27 +49,34 @@ type movieDB struct { Updated time.Time `db:"updated_at"` } +func getMovieDB(db *sqlx.DB, movie *polochon.Movie) (*movieDB, error) { + var mDB movieDB + err := db.QueryRowx(getMovieQueryByImdbID, movie.ImdbID).StructScan(&mDB) + if err != nil { + return &mDB, err + } + return &mDB, nil +} + // GetMovie fills show details of a polochon.Movie func GetMovie(db *sqlx.DB, pMovie *polochon.Movie) error { - var mDB movieDB - var err error - if pMovie.ImdbID != "" { - // Get the data from the DB - err = db.QueryRowx(getMovieQueryByImdbID, pMovie.ImdbID).StructScan(&mDB) - } else { - err = fmt.Errorf("Can't get movie details, you have to specify an ImdbID") + if pMovie.ImdbID == "" { + return fmt.Errorf("Can't get movie details, you have to specify an ImdbID") } + + // Get the movie from the DB + movie, err := getMovieDB(db, pMovie) if err != nil { return err } // Fills the polochon.Movie from the movieDB - FillFromDB(&mDB, pMovie) + FillMovieFromDB(movie, pMovie) return nil } -// FillFromDB fills a Movie from a movieDB extracted from the DB -func FillFromDB(mDB *movieDB, pMovie *polochon.Movie) { +// FillMovieFromDB fills a Movie from a movieDB extracted from the DB +func FillMovieFromDB(mDB *movieDB, pMovie *polochon.Movie) { pMovie.ImdbID = mDB.ImdbID pMovie.Title = mDB.Title pMovie.Rating = mDB.Rating @@ -84,10 +91,61 @@ func FillFromDB(mDB *movieDB, pMovie *polochon.Movie) { pMovie.Tagline = mDB.Tagline } +// updateFromMovie will update the movieDB from a Movie +// We just make sure to never update the movieDB with empty value +func (m *movieDB) updateFromMovie(movie *polochon.Movie) { + if movie.ImdbID != "" { + m.ImdbID = movie.ImdbID + } + if movie.TmdbID != 0 { + m.TmdbID = movie.TmdbID + } + if movie.Title != "" { + m.Title = movie.Title + } + if movie.OriginalTitle != "" { + m.OriginalTitle = movie.OriginalTitle + } + if movie.SortTitle != "" { + m.SortTitle = movie.SortTitle + } + if movie.Rating != 0 { + m.Rating = movie.Rating + } + if movie.Votes != 0 { + m.Votes = movie.Votes + } + if movie.Plot != "" { + m.Plot = movie.Plot + } + if movie.Year != 0 { + m.Year = movie.Year + } + if movie.Runtime != 0 { + m.Runtime = movie.Runtime + } + if movie.Tagline != "" { + m.Tagline = movie.Tagline + } + if movie.Genres != nil { + m.Genres = movie.Genres + } +} + // UpsertMovie upsert a polochon Movie in the database func UpsertMovie(db *sqlx.DB, pMovie *polochon.Movie) error { - mDB := NewMovieDB(pMovie) - r, err := db.NamedQuery(upsertMovieQuery, mDB) + // Try to get the movie + movieDB, err := getMovieDB(db, pMovie) + // Return only if the error is != sql.ErrNoRows + if err != nil { + if err != sql.ErrNoRows { + return err + } + } + + // Update the movieDB from the movie we have + movieDB.updateFromMovie(pMovie) + r, err := db.NamedQuery(upsertMovieQuery, movieDB) if err != nil { return err } @@ -96,9 +154,9 @@ func UpsertMovie(db *sqlx.DB, pMovie *polochon.Movie) error { return nil } -// NewMovieDB returns a Movie ready to be put in DB from a +// newMovieDB returns a Movie ready to be put in DB from a // polochon Movie -func NewMovieDB(m *polochon.Movie) movieDB { +func newMovieDB(m *polochon.Movie) movieDB { genres := []string{} if m.Genres != nil { genres = m.Genres