Skip to content

Commit

Permalink
feat(stats): add year filtering for persons
Browse files Browse the repository at this point in the history
  • Loading branch information
believer committed Sep 15, 2024
1 parent 0b3ef2f commit edff58a
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 139 deletions.
14 changes: 7 additions & 7 deletions components/dropdown.templ
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package components

import "strconv"

type DropdownProps struct {
Route string
Options []int
Options []string
Value string
}

templ Dropdown(props DropdownProps) {
<select class="appearance-none border border-neutral-300 dark:border-neutral-700 rounded text-xs px-2 py-1 bg-transparent focus:outline-dashed focus:outline-offset-2 focus:outline-neutral-300 dark:focus:outline-neutral-700" hx-target="closest section" hx-get={ props.Route } name="year" value={ props.Value } hx-swap="outerHTML">
<select class="appearance-none border border-neutral-300 dark:border-neutral-700 rounded text-xs px-2 py-1 bg-transparent focus:outline-dashed focus:outline-offset-2 focus:outline-neutral-300 dark:focus:outline-neutral-700 text-center" hx-target="closest section" hx-get={ props.Route } name="year" value={ props.Value } hx-swap="outerHTML">
for _, option := range props.Options {
<option
value={ strconv.Itoa(option) }
if props.Value == strconv.Itoa(option) {
value={ option }
if props.Value == option {
selected
}
>{ strconv.Itoa(option) }</option>
>
{ option }
</option>
}
</select>
}
20 changes: 9 additions & 11 deletions components/dropdown_templ.go

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

2 changes: 1 addition & 1 deletion components/graphs.templ
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type GraphWithYearProps struct {
Route string
SelectedYear string
Title string
Years []int
Years []string
}

templ GraphWithYear(props GraphWithYearProps) {
Expand Down
2 changes: 1 addition & 1 deletion components/graphs_templ.go

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

19 changes: 16 additions & 3 deletions components/mostWatchedPerson.templ
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
package components

templ MostWatchedPerson(data []ListItem, title string) {
@Section(title, 0) {
@OrderedList(data, "person")
import "fmt"

type MostWatchedPersonProps struct {
Data []ListItem
Job string
Title string
Year string
Years []string
}

templ MostWatchedPerson(props MostWatchedPersonProps) {
@Section("", 0) {
@SectionTitleWithDropdown(props.Title) {
@Dropdown(DropdownProps{Route: fmt.Sprintf("/stats/most-watched-person/%s", props.Job), Options: props.Years, Value: props.Year})
}
@OrderedList(props.Data, "person")
}
}

Expand Down
52 changes: 44 additions & 8 deletions components/mostWatchedPerson_templ.go

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

4 changes: 3 additions & 1 deletion db/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ FROM ( SELECT DISTINCT ON (movie_id)
FROM
seen
WHERE
user_id = $2) AS s
user_id = $2
AND ($3 = 'All'
OR EXTRACT(YEAR FROM date) = $3::int)) AS s
INNER JOIN movie_person AS mp ON mp.movie_id = s.movie_id
INNER JOIN person AS p ON p.id = mp.person_id
WHERE
Expand Down
31 changes: 22 additions & 9 deletions handlers/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
func getPersonsByJob(job string, userId string) ([]components.ListItem, error) {
var persons []components.ListItem

err := db.Dot.Select(db.Client, &persons, "stats-most-watched-by-job", job, userId)
err := db.Dot.Select(db.Client, &persons, "stats-most-watched-by-job", job, userId, "All")

if err != nil {
return nil, err
Expand Down Expand Up @@ -151,15 +151,27 @@ func HandleGetStats(c *fiber.Ctx) error {
}

func HandleGetMostWatchedByJob(c *fiber.Ctx) error {
var persons []components.ListItem

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

err := db.Dot.Select(db.Client, &persons, "stats-most-watched-by-job", job, userId, year)

if err != nil {
return err
}

return utils.TemplRender(c, components.MostWatchedPerson(persons,
cases.Title(language.English).String(job),
return utils.TemplRender(c, components.MostWatchedPerson(components.MostWatchedPersonProps{
Data: persons,
Job: job,
Title: cases.Title(language.English).String(job),
Year: year,
Years: years,
},
))
}

Expand Down Expand Up @@ -328,16 +340,17 @@ func pgSelectedYear(year string) (string, error) {
return time.Date(parsedYear, time.September, 10, 0, 0, 0, 0, time.UTC).Format("2006-01-02 15:04:05"), nil
}

func availableYears() []int {
func availableYears() []string {
// First year with "real" data
// 2011 is used as a catch all for anything before I had the database
startYear := 2012
endYear := 2012
currentYear := time.Now().Year()

years := make([]int, 0)
years := make([]string, 0)

for year := startYear; year <= currentYear; year++ {
years = append(years, year)
for year := currentYear; year >= endYear; year-- {
y := strconv.Itoa(year)
years = append(years, y)
}

return years
Expand Down
12 changes: 8 additions & 4 deletions views/stats.templ
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type StatsProps struct {
WilhelmScreams int
Year string
YearRatings []types.Bar
Years []int
Years []string
}

templ Stats(props StatsProps) {
Expand Down Expand Up @@ -170,9 +170,13 @@ templ Stats(props StatsProps) {
@components.Section("Most watched movies", 0) {
@components.OrderedList(props.MostWatchedMovies, "movie")
}
@components.Section("Cast", 0) {
@components.OrderedList(props.MostWatchedCast, "person")
}
@components.MostWatchedPerson(components.MostWatchedPersonProps{
Job: "cast",
Title: "Cast",
Data: props.MostWatchedCast,
Year: "All",
Years: append([]string{"All"}, props.Years...),
})
@statsSection("director")
@statsSection("writer")
@statsSection("composer")
Expand Down
Loading

0 comments on commit edff58a

Please sign in to comment.