diff --git a/internal/features/anime/handler.go b/internal/features/anime/handler.go index e056018..55035d5 100644 --- a/internal/features/anime/handler.go +++ b/internal/features/anime/handler.go @@ -7,10 +7,23 @@ import ( "strconv" "mal/internal/database" + "mal/internal/jikan" "mal/internal/shared/middleware" "mal/internal/templates" ) +func deduplicateAnimes(animes []jikan.Anime) []jikan.Anime { + seen := make(map[int]bool) + var result []jikan.Anime + for _, a := range animes { + if !seen[a.MalID] { + seen[a.MalID] = true + result = append(result, a) + } + } + return result +} + type Handler struct { svc *Service } @@ -65,6 +78,8 @@ func (h *Handler) HandleAPISearch(w http.ResponseWriter, r *http.Request) { return } + res.Animes = deduplicateAnimes(res.Animes) + templates.SearchItems(query, res.Animes, page+1, res.HasNextPage).Render(r.Context(), w) } @@ -82,6 +97,8 @@ func (h *Handler) HandleAPICatalog(w http.ResponseWriter, r *http.Request) { return } + res.Animes = deduplicateAnimes(res.Animes) + templates.CatalogItems(res.Animes, page+1, res.HasNextPage).Render(r.Context(), w) } @@ -191,6 +208,8 @@ func (h *Handler) HandleAPIDiscoverAiring(w http.ResponseWriter, r *http.Request return } + res.Animes = deduplicateAnimes(res.Animes) + templates.DiscoverItems(res.Animes, "airing", page+1, res.HasNextPage).Render(r.Context(), w) } @@ -208,5 +227,7 @@ func (h *Handler) HandleAPIDiscoverUpcoming(w http.ResponseWriter, r *http.Reque return } + res.Animes = deduplicateAnimes(res.Animes) + templates.DiscoverItems(res.Animes, "upcoming", page+1, res.HasNextPage).Render(r.Context(), w) } diff --git a/internal/templates/catalog.templ b/internal/templates/catalog.templ index 4e748f7..8e3599d 100644 --- a/internal/templates/catalog.templ +++ b/internal/templates/catalog.templ @@ -19,17 +19,30 @@ templ Catalog() { } templ CatalogItems(animes []jikan.Anime, nextPage int, hasNext bool) { - for i, anime := range animes { - if i == len(animes)-1 && hasNext { -
- @CatalogItem(anime) -
- } else { -
- @CatalogItem(anime) -
- } + for _, anime := range animes { +
+ @CatalogItem(anime) +
} + if hasNext { +
+ } + } templ CatalogItem(anime jikan.Anime) { diff --git a/internal/templates/catalog_templ.go b/internal/templates/catalog_templ.go index f1743e6..bbc426d 100644 --- a/internal/templates/catalog_templ.go +++ b/internal/templates/catalog_templ.go @@ -79,47 +79,55 @@ func CatalogItems(animes []jikan.Anime, nextPage int, hasNext bool) templ.Compon templ_7745c5c3_Var3 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - for i, anime := range animes { - if i == len(animes)-1 && hasNext { - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = CatalogItem(anime).Render(ctx, templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - } else { - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = CatalogItem(anime).Render(ctx, templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } + for _, anime := range animes { + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = CatalogItem(anime).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + if hasNext { + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err } return nil }) @@ -141,79 +149,79 @@ func CatalogItem(anime jikan.Anime) templ.Component { }() } ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var5 := templ.GetChildren(ctx) - if templ_7745c5c3_Var5 == nil { - templ_7745c5c3_Var5 = templ.NopComponent + templ_7745c5c3_Var6 := templ.GetChildren(ctx) + if templ_7745c5c3_Var6 == nil { + templ_7745c5c3_Var6 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "\">") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } if anime.ImageURL() != "" { - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "\"")") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 11, "\" alt=\"") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var9 string + templ_7745c5c3_Var9, templ_7745c5c3_Err = templ.JoinStringErrs(anime.DisplayTitle()) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templates/catalog.templ`, Line: 51, Col: 59} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var9)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 12, "\" class=\"catalog-thumb\" loading=\"lazy\">") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } } else { - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 12, "
no image
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 13, "
no image
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 13, "
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 14, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var9 string - templ_7745c5c3_Var9, templ_7745c5c3_Err = templ.JoinStringErrs(anime.DisplayTitle()) + var templ_7745c5c3_Var10 string + templ_7745c5c3_Var10, templ_7745c5c3_Err = templ.JoinStringErrs(anime.DisplayTitle()) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templates/catalog.templ`, Line: 44, Col: 24} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/templates/catalog.templ`, Line: 57, Col: 24} } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var9)) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var10)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 14, "
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 15, "
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/internal/templates/discovery.templ b/internal/templates/discovery.templ index aabba87..cec4691 100644 --- a/internal/templates/discovery.templ +++ b/internal/templates/discovery.templ @@ -39,15 +39,28 @@ templ Discover() { } templ DiscoverItems(animes []jikan.Anime, listType string, nextPage int, hasNext bool) { - for i, anime := range animes { - if i == len(animes)-1 && hasNext { -
- @CatalogItem(anime) -
- } else { -
- @CatalogItem(anime) -
- } + for _, anime := range animes { +
+ @CatalogItem(anime) +
} + if hasNext { +
+ } + } diff --git a/internal/templates/discovery_templ.go b/internal/templates/discovery_templ.go index d198a19..7170bfc 100644 --- a/internal/templates/discovery_templ.go +++ b/internal/templates/discovery_templ.go @@ -79,47 +79,55 @@ func DiscoverItems(animes []jikan.Anime, listType string, nextPage int, hasNext templ_7745c5c3_Var3 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - for i, anime := range animes { - if i == len(animes)-1 && hasNext { - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = CatalogItem(anime).Render(ctx, templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - } else { - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = CatalogItem(anime).Render(ctx, templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } + for _, anime := range animes { + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = CatalogItem(anime).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + if hasNext { + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err } return nil }) diff --git a/internal/templates/index.templ b/internal/templates/index.templ index ef4c05f..a30dab6 100644 --- a/internal/templates/index.templ +++ b/internal/templates/index.templ @@ -39,15 +39,28 @@ templ SearchResultsWrapper(query string, animes []jikan.Anime, nextPage int, has } templ SearchItems(query string, animes []jikan.Anime, nextPage int, hasNext bool) { - for i, anime := range animes { - if i == len(animes)-1 && hasNext { -
- @CatalogItem(anime) -
- } else { -
- @CatalogItem(anime) -
- } + for _, anime := range animes { +
+ @CatalogItem(anime) +
} + if hasNext { +
+ } + } diff --git a/internal/templates/index_templ.go b/internal/templates/index_templ.go index 8608ecd..240256e 100644 --- a/internal/templates/index_templ.go +++ b/internal/templates/index_templ.go @@ -146,47 +146,55 @@ func SearchItems(query string, animes []jikan.Anime, nextPage int, hasNext bool) templ_7745c5c3_Var5 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - for i, anime := range animes { - if i == len(animes)-1 && hasNext { - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = CatalogItem(anime).Render(ctx, templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - } else { - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = CatalogItem(anime).Render(ctx, templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 11, "
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } + for _, anime := range animes { + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = CatalogItem(anime).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + if hasNext { + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 12, "") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err } return nil })