Skip to content

Commit

Permalink
feat(stats): add genre by year
Browse files Browse the repository at this point in the history
  • Loading branch information
believer committed Oct 20, 2024
1 parent 76149c1 commit c143d04
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 14 deletions.
17 changes: 13 additions & 4 deletions components/mostWatchedPerson.templ
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,19 @@ templ MostWatchedPerson(props MostWatchedPersonProps) {
}
}

templ MostWatchedGenres(data []ListItem) {
@Section("Most watched genre", 0) {
if len(data) > 0 {
@OrderedList(data, "genre")
type MostWatchedGenresProps struct {
Data []ListItem
Year string
Years []string
}

templ MostWatchedGenres(props MostWatchedGenresProps) {
@Section("", 0) {
@SectionTitleWithDropdown("Genre") {
@Dropdown(DropdownProps{Route: "/stats/genres", Options: props.Years, Value: props.Year})
}
if len(props.Data) > 0 {
@OrderedList(props.Data, "genre")
} else {
@EmptyState("No movies seen")
}
Expand Down
40 changes: 36 additions & 4 deletions components/mostWatchedPerson_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions db/statsQueries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,16 @@ SELECT
g.id,
g.name,
COUNT(DISTINCT s.movie_id) AS count
FROM
seen s
FROM ( SELECT DISTINCT ON (movie_id)
movie_id
FROM
seen
WHERE
user_id = $1
AND ($2 = 'All'
OR EXTRACT(YEAR FROM date) = $2::int)) AS s
INNER JOIN movie_genre mg ON mg.movie_id = s.movie_id
INNER JOIN genre g ON mg.genre_id = g.id
WHERE
s.user_id = $1
GROUP BY
g.id
ORDER BY
Expand Down
10 changes: 8 additions & 2 deletions handlers/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,12 +436,18 @@ func HandleGetGenreStats(c *fiber.Ctx) error {
var genres []components.ListItem

userId := c.Locals("UserId").(string)
year := c.Query("year", "All")
years := append([]string{"All"}, availableYears()...)

err := db.Dot.Select(db.Client, &genres, "stats-genres", userId)
err := db.Dot.Select(db.Client, &genres, "stats-genres", userId, year)

if err != nil {
return err
}

return utils.TemplRender(c, components.MostWatchedGenres(genres))
return utils.TemplRender(c, components.MostWatchedGenres(components.MostWatchedGenresProps{
Data: genres,
Year: year,
Years: years,
}))
}

0 comments on commit c143d04

Please sign in to comment.