Normalize when to fetch data from external service

This commit is contained in:
Lucas BEE 2017-01-31 13:39:58 +00:00
parent e2fa98f7c3
commit 351552d049
4 changed files with 22 additions and 20 deletions

View File

@ -209,16 +209,17 @@ func (m *Movie) GetDetails(env *web.Env, user *users.User, force bool) error {
switch err {
case nil:
log.Debug("movie found in database")
if !force {
log.Debug("returning movie from db")
return nil
}
case sql.ErrNoRows:
log.Debug("movie not found in database")
default:
// Unexpected error
return err
}
// If force is not specified, don't go further
if !force {
// Will return ErrNoRows if the movie wasn't found
return err
}
// GetDetail
err = m.Movie.GetDetails(env.Log)
@ -271,21 +272,20 @@ func (m *Movie) GetTorrents(env *web.Env, force bool) error {
switch err {
case nil:
log.Debug("torrents found in database")
if !force {
log.Debugf("returning %d torrents from db", len(movieTorrents))
// Add the torrents to the movie
for _, t := range movieTorrents {
m.Torrents = append(m.Torrents, t.Torrent)
}
return nil
}
case sql.ErrNoRows:
log.Debug("torrent not found in database")
// We'll need to GetTorrents from torrenters
default:
// Unexpected error
return err
}
if !force {
log.Debugf("returning %d torrents from db", len(movieTorrents))
// Add the torrents to the movie
for _, t := range movieTorrents {
m.Torrents = append(m.Torrents, t.Torrent)
}
return nil
}
err = m.Movie.GetTorrents(env.Log)
if err != nil {

View File

@ -147,18 +147,19 @@ func (e *Episode) GetTorrents(env *web.Env, force bool) error {
log.Debug("torrents found in database")
case sql.ErrNoRows:
log.Debug("torrent not found in database")
// We'll need to GetTorrents from torrenters
default:
// Unexpected error
return err
}
// If force is not specified, don't go further
if !force {
log.Debugf("returning %d torrents from db", len(episodeTorrents))
// Add the torrents to the episode
for _, t := range episodeTorrents {
e.Torrents = append(e.Torrents, t.Torrent)
}
return nil
// Will return ErrNoRows if the torrent wasn't found
return err
}
err = e.ShowEpisode.GetTorrents(env.Log)

View File

@ -31,7 +31,7 @@ func RefreshDetailsHandler(env *web.Env, w http.ResponseWriter, r *http.Request)
return DetailsHandler(env, w, r, true)
}
// DetailsHandler handles details of a show
// DetailsHandler handles details of an episode
func DetailsHandler(env *web.Env, w http.ResponseWriter, r *http.Request, force bool) error {
vars := mux.Vars(r)
id := vars["id"]

View File

@ -190,16 +190,17 @@ func (s *Show) GetDetails(env *web.Env, user *users.User, force bool) error {
switch err {
case nil:
log.Debug("show found in database")
if !force {
log.Debug("returning show from db")
return nil
}
case sql.ErrNoRows:
log.Debug("show not found in database")
default:
// Unexpected error
return err
}
// If force is not specified, don't go further
if !force {
// Will return ErrNoRows if the show wasn't found
return err
}
// GetDetail
err = s.Show.GetDetails(env.Log)