feat: 🎉 Rating Orama!

This commit is contained in:
2023-04-09 06:45:27 +02:00
commit b2f4b573f3
61 changed files with 8130 additions and 0 deletions
+68
View File
@@ -0,0 +1,68 @@
package models
import (
"strconv"
"time"
)
type Popularity struct {
ShowID string `json:"show_id"`
TimesViewed int `json:"times_viewed"`
}
type TvShow struct {
ShowID string `json:"show_id"`
Title string `json:"title"`
Runtime int `json:"runtime"`
Votes int `json:"votes"`
AvgRating float64 `json:"avg_rating"`
MedianRating float64 `json:"median_rating"`
Seasons []Season `json:"seasons"`
}
type Season struct {
Number int `json:"number"`
AvgRating float64 `json:"avg_rating"`
MedianRating float64 `json:"median_rating"`
Votes int `json:"votes"`
Episodes []Episode `json:"episodes"`
}
type Episode struct {
Number int `json:"number"`
EpisodeID string `json:"episode_id"`
Title string `json:"title"`
Aired time.Time `json:"aired"`
AvgRating float64 `json:"avg_rating"`
Votes int `json:"votes"`
}
func (tvShow *TvShow) TvShowBuilder(tvShowDTO TvShowDTO) {
tvShow.ShowID = tvShowDTO.ShowID
tvShow.Title = tvShowDTO.Title
tvShow.Runtime, _ = strconv.Atoi(tvShowDTO.Runtime)
lastSeasonNumber := tvShowDTO.Episodes[len(tvShowDTO.Episodes)-1].SeasonID
if lastSeasonNumber == -1 {
lastSeasonNumber = tvShowDTO.Episodes[len(tvShowDTO.Episodes)-2].SeasonID
}
seasons := make([]Season, lastSeasonNumber)
for currentSeason := 1; currentSeason <= lastSeasonNumber; currentSeason++ {
for _, episode := range tvShowDTO.Episodes {
if episode.SeasonID == currentSeason {
seasons[currentSeason-1].Number = currentSeason
seasons[currentSeason-1].Episodes = append(seasons[currentSeason-1].Episodes, Episode{
Number: episode.Number,
EpisodeID: episode.EpisodeID,
Title: episode.Title,
Aired: episode.Aired.Time,
AvgRating: episode.AvgRating,
Votes: episode.Votes,
})
}
}
}
tvShow.Seasons = seasons
}
+58
View File
@@ -0,0 +1,58 @@
package models
import (
"encoding/json"
"github.com/zepyrshut/rating-orama/utils"
"time"
)
type TvShowDTO struct {
ShowID string `json:"tt_show_id"`
Title string `json:"title"`
Runtime string `json:"runtime"`
Episodes []EpisodeDTO `json:"episodes"`
}
type EpisodeDTO struct {
Number int `json:"number"`
SeasonID int `json:"season_id"`
EpisodeID string `json:"tt_episode_id"`
Title string `json:"title"`
Aired AiredTime `json:"aired"`
AvgRating float64 `json:"avg_rating"`
Votes int `json:"votes"`
}
type AiredTime struct {
time.Time
}
func (tvShow *TvShow) UnmarshalJSON(data []byte) error {
var tvShowDTO TvShowDTO
err := json.Unmarshal(data, &tvShowDTO)
if err != nil {
return err
}
tvShow.TvShowBuilder(tvShowDTO)
return nil
}
func (aired *AiredTime) UnmarshalJSON(data []byte) error {
if string(data) == "null" || string(data) == "" {
return nil
}
var s string
if err := json.Unmarshal(data, &s); err != nil {
return nil
}
t, err := utils.TimeParser(s)
if err != nil {
return err
}
aired.Time = t
return nil
}