Skip to content

Commit

Permalink
fix(person): move seen to stored function instead
Browse files Browse the repository at this point in the history
  • Loading branch information
believer committed Apr 29, 2024
1 parent fad36db commit 2cf58b5
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 49 deletions.
21 changes: 5 additions & 16 deletions db/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ SELECT
p.name,
-- Function get_person_role_json returns a JSON array of movies
-- The function is defined in the database
get_person_role_json (p.id, 'director'::job) AS director,
get_person_role_json (p.id, 'cast') AS cast,
get_person_role_json (p.id, 'writer') AS writer,
get_person_role_json (p.id, 'composer') AS composer,
get_person_role_json (p.id, 'producer') AS producer
get_person_role_with_seen_json (p.id, 'director'::job, $2) AS director,
get_person_role_with_seen_json (p.id, 'cast', $2) AS cast,
get_person_role_with_seen_json (p.id, 'writer', $2) AS writer,
get_person_role_with_seen_json (p.id, 'composer', $2) AS composer,
get_person_role_with_seen_json (p.id, 'producer', $2) AS producer
FROM
person AS p
WHERE
Expand Down Expand Up @@ -297,14 +297,3 @@ GROUP BY
ORDER BY
label DESC;

-- name: person-seen-movie-by-id
SELECT
EXISTS (
SELECT
id
FROM
seen
WHERE
user_id = $1
AND movie_id = $2);

14 changes: 1 addition & 13 deletions handlers/person.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func HandleGetPersonByID(c *fiber.Ctx) error {

id := c.Params("id")

err := db.Dot.Get(db.Client, &person, "person-by-id", id)
err := db.Dot.Get(db.Client, &person, "person-by-id", id, c.Locals("UserId"))

if err != nil {
// TODO: Display 404 page
Expand All @@ -41,15 +41,3 @@ func HandleGetPersonByID(c *fiber.Ctx) error {

return utils.TemplRender(c, views.Person(person, totalCredits, id))
}

func HandleSeenMovieByID(c *fiber.Ctx) error {
var seen bool

err := db.Dot.Get(db.Client, &seen, "person-seen-movie-by-id", c.Locals("UserId"), c.Params("movieId"))

if err != nil {
return err
}

return utils.TemplRender(c, views.Seen(seen))
}
1 change: 0 additions & 1 deletion router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func SetupRoutes(app *fiber.App) {

personGroup.Get("/", redirectToHome)
personGroup.Get("/:id", handlers.HandleGetPersonByID)
personGroup.Get("/:id/:movieId", handlers.HandleSeenMovieByID)

// Search
// --------------------------
Expand Down
1 change: 1 addition & 0 deletions types/movie.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Movie struct {
Title string `db:"title" json:"title"`
UpdatedAt time.Time `db:"updated_at"`
WatchedAt time.Time `db:"watched_at" json:"watchedAt"`
Seen bool `db:"seen"`
}

type Movies []Movie
Expand Down
35 changes: 28 additions & 7 deletions types/person.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,37 @@ package types
import (
"encoding/json"
"fmt"
"time"
)

type PersonMovie struct {
ID int `json:"id" db:"id"`
Title string `json:"title" db:"title"`
ReleaseDate time.Time `json:"release_date" db:"release_date"`
Seen bool `json:"seen" db:"seen"`
}

type PersonMovies []PersonMovie

func (u *PersonMovies) Scan(v interface{}) error {
switch vv := v.(type) {
case []byte:
return json.Unmarshal(vv, u)
case string:
return json.Unmarshal([]byte(vv), u)
default:
return fmt.Errorf("unsupported type: %T", v)
}
}

type Person struct {
ID int `json:"id" db:"id"`
Name string `json:"name" db:"name"`
Cast Movies `json:"cast" db:"cast"`
Director Movies `json:"director" db:"director"`
Writer Movies `json:"writer" db:"writer"`
Composer Movies `json:"composer" db:"composer"`
Producer Movies `json:"producer" db:"producer"`
ID int `json:"id" db:"id"`
Name string `json:"name" db:"name"`
Cast PersonMovies `json:"cast" db:"cast"`
Director PersonMovies `json:"director" db:"director"`
Writer PersonMovies `json:"writer" db:"writer"`
Composer PersonMovies `json:"composer" db:"composer"`
Producer PersonMovies `json:"producer" db:"producer"`
}

type Persons []Person
Expand Down
10 changes: 2 additions & 8 deletions views/person.templ
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strconv"
)

templ Credit(title string, data types.Movies, id string) {
templ Credit(title string, data types.PersonMovies, id string) {
if len(data) > 0 {
@components.Section(title, len(data)) {
<ol class="flex flex-col gap-2">
Expand All @@ -23,13 +23,7 @@ templ Credit(title string, data types.Movies, id string) {
<span class="text-sm tabular-nums text-neutral-500 dark:text-neutral-400">
{ movie.ReleaseDate.Format("2006") }
</span>
<span hx-get={ fmt.Sprintf("/person/%s/%d", id, movie.ID) } hx-trigger="load">
<span class="text-neutral-300 dark:text-neutral-700">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="currentColor" class="w-4 h-4">
<path fill-rule="evenodd" d="M8 15A7 7 0 1 0 8 1a7 7 0 0 0 0 14Zm3.844-8.791a.75.75 0 0 0-1.188-.918l-3.7 4.79-1.649-1.833a.75.75 0 1 0-1.114 1.004l2.25 2.5a.75.75 0 0 0 1.15-.043l4.25-5.5Z" clip-rule="evenodd"></path>
</svg>
</span>
</span>
@Seen(movie.Seen)
</span>
</li>
}
Expand Down
8 changes: 4 additions & 4 deletions views/person_templ.go

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

0 comments on commit 2cf58b5

Please sign in to comment.