some tweaks in getting episodes

This commit is contained in:
Pedro Pérez 2024-11-06 22:35:15 +01:00
parent 3d71a16633
commit 90c21a1f55
6 changed files with 31 additions and 4 deletions

View File

@ -11,6 +11,7 @@ func Router(h *handlers.Handlers, app *config.App) *gin.Engine {
gin.SetMode(app.AppInfo.GinMode)
r := gin.New()
r.GET("/ping", h.Ping)
r.GET("/tvshow", h.GetTVShow)
return r

View File

@ -14,7 +14,8 @@ where tt_imdb = $1;
-- name: GetEpisodes :many
select * from "episodes"
where tv_show_id = $1;
where tv_show_id = $1
order by season, episode asc;
-- name: IncreasePopularity :exec
update "tv_show" set popularity = popularity + 1

View File

@ -66,6 +66,7 @@ func (hq *Handlers) ToBeImplemented(c *gin.Context) {
}
func (hq *Handlers) Ping(c *gin.Context) {
slog.Info("ping", RequestID, c.Request.Context().Value(RequestID))
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})

View File

@ -50,9 +50,14 @@ func (hq *Handlers) GetTVShow(c *gin.Context) {
slog.Info("tv show exists", "ttid", ttShowID, "title", tvShow.Name)
}
tvShowMedian, _ := hq.Queries.TvShowMedianRating(c, sqlcEpisodes[0].TvShowID)
tvShowAverage, _ := hq.Queries.TvShowAverageRating(c, sqlcEpisodes[0].TvShowID)
c.JSON(http.StatusOK, gin.H{
"popularity": tvShow.Popularity,
"title": title,
"seasons": sqlcEpisodes,
"popularity": tvShow.Popularity,
"title": title,
"seasons": sqlcEpisodes,
"tvShowMedian": tvShowMedian,
"tvShowAverage": tvShowAverage,
})
}

View File

@ -15,6 +15,7 @@ type Querier interface {
GetEpisodes(ctx context.Context, tvShowID int32) ([]Episode, error)
IncreasePopularity(ctx context.Context, ttImdb string) error
SeasonAverageRating(ctx context.Context, arg SeasonAverageRatingParams) (float64, error)
SeasonMedianRating(ctx context.Context, arg SeasonMedianRatingParams) (float64, error)
TvShowAverageRating(ctx context.Context, tvShowID int32) (float64, error)
TvShowMedianRating(ctx context.Context, tvShowID int32) (float64, error)
}

View File

@ -101,6 +101,7 @@ func (q *Queries) CreateTVShow(ctx context.Context, arg CreateTVShowParams) (TvS
const getEpisodes = `-- name: GetEpisodes :many
select id, tv_show_id, season, episode, released, name, plot, avg_rating, vote_count from "episodes"
where tv_show_id = $1
order by season, episode asc
`
func (q *Queries) GetEpisodes(ctx context.Context, tvShowID int32) ([]Episode, error) {
@ -160,6 +161,23 @@ func (q *Queries) SeasonAverageRating(ctx context.Context, arg SeasonAverageRati
return avg, err
}
const seasonMedianRating = `-- name: SeasonMedianRating :one
select percentile_cont(0.5) within group (order by avg_rating) from "episodes"
where tv_show_id = $1 and season = $2
`
type SeasonMedianRatingParams struct {
TvShowID int32 `json:"tv_show_id"`
Season int32 `json:"season"`
}
func (q *Queries) SeasonMedianRating(ctx context.Context, arg SeasonMedianRatingParams) (float64, error) {
row := q.db.QueryRow(ctx, seasonMedianRating, arg.TvShowID, arg.Season)
var percentile_cont float64
err := row.Scan(&percentile_cont)
return percentile_cont, err
}
const tvShowAverageRating = `-- name: TvShowAverageRating :one
select avg(avg_rating) from "episodes"
where tv_show_id = $1