Skip to content

Commit

Permalink
change(missing): only remove media items when count > 0
Browse files Browse the repository at this point in the history
  • Loading branch information
l3uddz committed Feb 1, 2020
1 parent abf1a6c commit 57bd36d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
22 changes: 13 additions & 9 deletions cmd/missing.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ var missingCmd = &cobra.Command{
defer database.Close()

// retrieve missing records from pvr and stash in database
if flagRefreshCache {
existingItemsCount := database.GetItemsCount(lowerPvrName, "missing")
if flagRefreshCache || existingItemsCount < 1 {
log.Infof("Retrieving missing media from %s: %q", capitalise.First(pvrConfig.Type), pvrName)

missingRecords, err := pvr.GetWantedMissing()
Expand All @@ -50,22 +51,24 @@ var missingCmd = &cobra.Command{
// stash missing media in database
log.Debug("Stashing media items in database...")

if err := database.SetMediaItems(strings.ToLower(pvrName), "missing", missingRecords); err != nil {
if err := database.SetMediaItems(lowerPvrName, "missing", missingRecords); err != nil {
log.WithError(err).Fatal("Failed stashing media items in database")
}

log.Info("Stashed media items")

// remove media no longer missing
log.Debug("Removing media items from database that are no longer missing...")
if existingItemsCount >= 1 {
log.Debug("Removing media items from database that are no longer missing...")

removedItems, err := database.DeleteMissingItems(strings.ToLower(pvrName), "missing", missingRecords)
if err != nil {
log.WithError(err).Fatal("Failed removing media items from database that are no longer missing...")
}
removedItems, err := database.DeleteMissingItems(lowerPvrName, "missing", missingRecords)
if err != nil {
log.WithError(err).Fatal("Failed removing media items from database that are no longer missing...")
}

log.WithField("removed_items", removedItems).
Info("Removed media items from database that are no longer missing")
log.WithField("removed_items", removedItems).
Info("Removed media items from database that are no longer missing")
}

//if err := database.SetMediaItems(pvrName, "missing", missingRecords); err != nil {
// log.WithError(err).Fatal("Failed stashing media items in database...")
Expand Down Expand Up @@ -149,6 +152,7 @@ func parseValidateInputs(args []string) error {

// validate pvr exists in config
pvrName = args[0]
lowerPvrName = strings.ToLower(pvrName)
pvrConfig, ok = config.Config.Pvr[pvrName]
if !ok {
return fmt.Errorf("no pvr configuration found for: %q", pvrName)
Expand Down
9 changes: 5 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ var (
flagRefreshCache = false

// Global vars
pvrName string
pvrConfig *config.Pvr
pvr pvrObj.Interface
log *logrus.Entry
pvrName string
lowerPvrName string
pvrConfig *config.Pvr
pvr pvrObj.Interface
log *logrus.Entry
)

// rootCmd represents the base command when called without any subcommands
Expand Down
7 changes: 7 additions & 0 deletions database/count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package database

func GetItemsCount(pvrName string, wantedType string) int {
itemCount := 0
db.Model(&MediaItem{}).Where("pvr_name = ? AND wanted_type = ?", pvrName, wantedType).Count(&itemCount)
return itemCount
}

0 comments on commit 57bd36d

Please sign in to comment.