From 403ca081cc0d31200460fdeddd54081f6a7d5f98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Delattre?= Date: Mon, 7 Nov 2016 16:51:02 +0100 Subject: [PATCH] Allow movies.GetDetails to be forced To check from the internet even if the movie is already in the database --- src/internal/movies/handlers.go | 4 +-- src/internal/movies/movies.go | 48 +++++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/internal/movies/handlers.go b/src/internal/movies/handlers.go index a5af68d..4e4b177 100644 --- a/src/internal/movies/handlers.go +++ b/src/internal/movies/handlers.go @@ -79,7 +79,7 @@ func FromPolochon(env *web.Env, w http.ResponseWriter, r *http.Request) error { } for _, m := range movies { - err := m.GetDetails(env) + err := m.GetDetails(env, false) if err != nil { env.Log.Error(err) } @@ -119,7 +119,7 @@ func ExplorePopular(env *web.Env, w http.ResponseWriter, r *http.Request) error for _, m := range tmovies { movie := New(m.IDs.ImDB) movie.Detailers = []polochon.Detailer{detailer} - err := movie.GetDetails(env) + err := movie.GetDetails(env, false) if err != nil { env.Log.Error(err) continue diff --git a/src/internal/movies/movies.go b/src/internal/movies/movies.go index b0ec930..54eebd2 100644 --- a/src/internal/movies/movies.go +++ b/src/internal/movies/movies.go @@ -17,6 +17,11 @@ const ( VALUES (:imdbid, :title, :rating, :votes, :plot, :tmdbid, :year, :originaltitle, :runtime, :sorttitle, :tagline) RETURNING id;` + updateMovieQuery = ` + UPDATE movies + SET imdb_id=:imdbid, title=:title, rating=:rating, votes=:votes, plot=:plot, tmdb_id=:tmdbid, year=:year, original_title=:originaltitle, runtime=:runtime, sort_title=:sorttitle, tagline=:tagline + WHERE ID = :id;` + getMovieQueryByImdbID = ` SELECT id, imdb_id AS imdbid, title, rating, votes, plot, @@ -74,25 +79,40 @@ func (m *Movie) Get(db *sqlx.DB) error { return nil } -// GetDetails retrieves details for the movie, first try to -// get info from db, if not exists, use polochon.Detailer -// and save informations in the database for future use -func (m *Movie) GetDetails(env *web.Env) error { - m.Detailers = env.Config.MovieDetailers +// GetDetails retrieves details for the movie, first try to get info from db, +// if not exists, use polochon.Detailer and save informations in the database +// for future use +// +// If force is used, the detailer will be used even if the movie is found in +// database +func (m *Movie) GetDetails(env *web.Env, force bool) error { + if len(m.Detailers) == 0 { + m.Detailers = env.Config.MovieDetailers + } log := env.Log.WithFields(logrus.Fields{ "imdb_id": m.ImdbID, "function": "movies.GetDetails", }) + + // If the movie is not in db, we should add it, otherwise we should update + // it + var dbFunc func(db *sqlx.DB) error + var err error err = m.Get(env.Database) - if err == nil { + switch err { + case nil: log.Debug("movie found in database") - return nil - } - if err != ErrNotFound { - // Unexpected error + dbFunc = m.Update + if !force { + return nil + } + case ErrNotFound: + dbFunc = m.Add log.Debug("movie not found in database") + default: + // Unexpected error return err } @@ -104,7 +124,7 @@ func (m *Movie) GetDetails(env *web.Env) error { log.Debug("got details from detailers") - err = m.Add(env.Database) + err = dbFunc(env.Database) if err != nil { return err } @@ -138,6 +158,12 @@ func (m *Movie) Add(db *sqlx.DB) error { return nil } +// Update a movie in the database +func (m *Movie) Update(db *sqlx.DB) error { + _, err := db.NamedQuery(updateMovieQuery, m) + return err +} + // Delete movie from database func (m *Movie) Delete(db *sqlx.DB) error { r, err := db.Exec(deleteMovieQuery, m.ID)