From 90c21a1f55bd65846013b013063b2926d364dc2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20P=C3=A9rez?= Date: Wed, 6 Nov 2024 22:35:15 +0100 Subject: [PATCH] some tweaks in getting episodes --- core/cmd/routes.go | 1 + core/database/queries/tv_show.sql | 3 ++- core/internal/handlers/handlers.go | 1 + core/internal/handlers/tvshow.go | 11 ++++++++--- core/internal/sqlc/querier.go | 1 + core/internal/sqlc/tv_show.sql.go | 18 ++++++++++++++++++ 6 files changed, 31 insertions(+), 4 deletions(-) diff --git a/core/cmd/routes.go b/core/cmd/routes.go index c9f917c..2e9e562 100644 --- a/core/cmd/routes.go +++ b/core/cmd/routes.go @@ -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 diff --git a/core/database/queries/tv_show.sql b/core/database/queries/tv_show.sql index 3fb917b..17f1f39 100644 --- a/core/database/queries/tv_show.sql +++ b/core/database/queries/tv_show.sql @@ -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 diff --git a/core/internal/handlers/handlers.go b/core/internal/handlers/handlers.go index 74e1a8b..6f34115 100644 --- a/core/internal/handlers/handlers.go +++ b/core/internal/handlers/handlers.go @@ -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", }) diff --git a/core/internal/handlers/tvshow.go b/core/internal/handlers/tvshow.go index 05ff25c..d12f5f2 100644 --- a/core/internal/handlers/tvshow.go +++ b/core/internal/handlers/tvshow.go @@ -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, }) } diff --git a/core/internal/sqlc/querier.go b/core/internal/sqlc/querier.go index 49febba..d17f295 100644 --- a/core/internal/sqlc/querier.go +++ b/core/internal/sqlc/querier.go @@ -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) } diff --git a/core/internal/sqlc/tv_show.sql.go b/core/internal/sqlc/tv_show.sql.go index a11fc89..5c88ba4 100644 --- a/core/internal/sqlc/tv_show.sql.go +++ b/core/internal/sqlc/tv_show.sql.go @@ -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