Skip to content

Commit

Permalink
feat: Update media repository to use float64 for duration calculations
Browse files Browse the repository at this point in the history
- Changed the data type of `duration` variables in `CountMoviesTotalDuration()` and `CountEpisodesTotalDuration()` functions from `int64` to `float64`
- Updated return statements to round the calculated duration using the `math.Round()` function before converting it back to `int64`

This commit improves accuracy in calculating total durations for movies and episodes by using floating-point numbers instead of integers.
  • Loading branch information
Nouuu committed Jun 28, 2023
1 parent d7d9708 commit 12fddc3
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions internal/repository/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/bingemate/media-go-pkg/tmdb"
"gorm.io/gorm"
"log"
"math"
"time"
)

Expand Down Expand Up @@ -1310,7 +1311,7 @@ func (r *MediaRepository) CountAvailableEpisodes() (int64, error) {
}

func (r *MediaRepository) CountMoviesTotalDuration() (int64, error) {
var duration int64
var duration float64
result := r.db.Model(&repository.Movie{}).
Joins("JOIN media_files ON media_files.id = movies.media_file_id").
Select("SUM(media_files.duration)").
Expand All @@ -1320,11 +1321,11 @@ func (r *MediaRepository) CountMoviesTotalDuration() (int64, error) {
if result.Error != nil {
return 0, result.Error
}
return duration, nil
return int64(math.Round(duration)), nil
}

func (r *MediaRepository) CountEpisodesTotalDuration() (int64, error) {
var duration int64
var duration float64
result := r.db.Model(&repository.Episode{}).
Joins("JOIN media_files ON media_files.id = episodes.media_file_id").
Select("SUM(media_files.duration)").
Expand All @@ -1334,5 +1335,5 @@ func (r *MediaRepository) CountEpisodesTotalDuration() (int64, error) {
if result.Error != nil {
return 0, result.Error
}
return duration, nil
return int64(math.Round(duration)), nil
}

0 comments on commit 12fddc3

Please sign in to comment.