Skip to content

Commit

Permalink
feat(genre): add pagination on scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
believer committed Sep 28, 2024
1 parent 266b992 commit d88b2f9
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 84 deletions.
7 changes: 7 additions & 0 deletions db/db_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,15 @@ func InitializeConnection() error {
return err
}

genreQueries, err := dotsql.LoadFromFile("./db/genreQueries.sql")

if err != nil {
return err
}

dot := dotsql.Merge(
generalQueries,
genreQueries,
seriesQueries,
statsQueries,
watchlistQueries,
Expand Down
42 changes: 42 additions & 0 deletions db/genreQueries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
-- name: genres-by-id
SELECT DISTINCT
(m.id),
m.title,
m.release_date,
m.imdb_id,
(s.id IS NOT NULL) AS "seen"
FROM
movie_genre mg
INNER JOIN movie m ON m.id = mg.movie_id
LEFT JOIN ( SELECT DISTINCT ON (movie_id)
movie_id,
id
FROM
public.seen
WHERE
user_id = $2
ORDER BY
movie_id,
id) AS s ON m.id = s.movie_id
WHERE
mg.genre_id = $1
ORDER BY
m.release_date DESC OFFSET $3
LIMIT 50;

-- name: genre-by-id
SELECT
name
FROM
genre
WHERE
id = $1;

-- name: movies-in-genre
SELECT
count(*)
FROM
movie_genre
WHERE
id = $1;

33 changes: 0 additions & 33 deletions db/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -195,39 +195,6 @@ WHERE
ORDER BY
release_date;

-- name: genres-by-id
SELECT DISTINCT
(m.id),
m.title,
m.release_date,
m.imdb_id,
(s.id IS NOT NULL) AS "seen"
FROM
movie_genre mg
INNER JOIN movie m ON m.id = mg.movie_id
LEFT JOIN ( SELECT DISTINCT ON (movie_id)
movie_id,
id
FROM
public.seen
WHERE
user_id = $2
ORDER BY
movie_id,
id) AS s ON m.id = s.movie_id
WHERE
mg.genre_id = $1
ORDER BY
m.release_date DESC;

-- name: genre-by-id
SELECT
name
FROM
genre
WHERE
id = $1;

-- name: wilhelm-screams
SELECT
count(*)
Expand Down
18 changes: 6 additions & 12 deletions handlers/genre.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func HandleGetGenre(c *fiber.Ctx) error {
var movies types.Movies
var genre types.MovieGenre

page := c.QueryInt("page", 1)
userId := c.Locals("UserId").(string)
genreId := c.Params("id")

Expand All @@ -22,23 +23,16 @@ func HandleGetGenre(c *fiber.Ctx) error {
return err
}

err = db.Dot.Select(db.Client, &movies, "genres-by-id", genreId, userId)
err = db.Dot.Select(db.Client, &movies, "genres-by-id", genreId, userId, (page-1)*50)

if err != nil {
return err
}

seen := 0

for _, movie := range movies {
if movie.Seen {
seen += 1
}
}

return utils.TemplRender(c, views.Genre(views.GenreProps{
Name: genre.Name,
Movies: movies,
Seen: seen,
ID: genreId,
Name: genre.Name,
NextPage: page + 1,
Movies: movies,
}))
}
17 changes: 11 additions & 6 deletions views/genre.templ
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package views
import (
"believer/movies/components"
"believer/movies/types"
"fmt"
)

type GenreProps struct {
Name string
Movies types.Movies
Seen int
ID string
Name string
NextPage int
Movies types.Movies
}

templ Genre(props GenreProps) {
Expand All @@ -17,9 +19,6 @@ templ Genre(props GenreProps) {
class="mx-auto flex max-w-xl flex-col gap-8 px-5 py-8"
>
@components.H1(props.Name, false)
<div>
{ props.Movies.NumberOfSeenMovies(props.Seen) }
</div>
if len(props.Movies) > 0 {
<ol class="flex flex-col gap-2 md:text-sm text-neutral-500 dark:text-neutral-400">
for _, movie := range props.Movies {
Expand All @@ -41,6 +40,12 @@ templ Genre(props GenreProps) {
</span>
</li>
}
<li
hx-get={ fmt.Sprintf("/genre/%s?page=%d", props.ID, props.NextPage) }
hx-trigger="revealed"
hx-swap="outerHTML"
hx-select="li"
></li>
</ol>
} else {
@components.EmptyState("No movies this year")
Expand Down
64 changes: 31 additions & 33 deletions views/genre_templ.go

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

0 comments on commit d88b2f9

Please sign in to comment.