fix: limit continue watching carousel
This commit is contained in:
@@ -25,3 +25,10 @@ func (r *animeRepository) GetWatchListEntry(ctx context.Context, params db.GetWa
|
||||
func (r *animeRepository) GetContinueWatchingEntries(ctx context.Context, userID string) ([]db.GetContinueWatchingEntriesRow, error) {
|
||||
return r.queries.GetContinueWatchingEntries(ctx, userID)
|
||||
}
|
||||
|
||||
func (r *animeRepository) GetContinueWatchingCarouselEntries(ctx context.Context, userID string, limit int64) ([]db.GetContinueWatchingEntriesRow, error) {
|
||||
return r.queries.GetContinueWatchingCarouselEntries(ctx, db.GetContinueWatchingCarouselEntriesParams{
|
||||
UserID: userID,
|
||||
Limit: limit,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -19,6 +19,8 @@ type animeService struct {
|
||||
repo domain.AnimeRepository
|
||||
}
|
||||
|
||||
const continueWatchingCarouselLimit int64 = 24
|
||||
|
||||
func wrapAnimes(in []jikan.Anime) []domain.Anime {
|
||||
out := make([]domain.Anime, 0, len(in))
|
||||
for _, a := range in {
|
||||
@@ -56,7 +58,7 @@ func (s *animeService) GetCatalogSection(ctx context.Context, userID string, sec
|
||||
if userID != "" && section == "Continue" {
|
||||
g.Go(func() error {
|
||||
var err error
|
||||
cw, err = s.repo.GetContinueWatchingEntries(gCtx, userID)
|
||||
cw, err = s.repo.GetContinueWatchingCarouselEntries(gCtx, userID, continueWatchingCarouselLimit)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get continue watching entries for %q: %w", userID, err)
|
||||
}
|
||||
|
||||
55
internal/anime/service_test.go
Normal file
55
internal/anime/service_test.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package anime
|
||||
|
||||
import (
|
||||
"context"
|
||||
"mal/internal/db"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type catalogRepoStub struct {
|
||||
allContinueRows []db.GetContinueWatchingEntriesRow
|
||||
carouselContinueRows []db.GetContinueWatchingEntriesRow
|
||||
carouselLimit int64
|
||||
}
|
||||
|
||||
func (r *catalogRepoStub) GetUserWatchList(ctx context.Context, userID string) ([]db.GetUserWatchListRow, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (r *catalogRepoStub) GetWatchListEntry(ctx context.Context, params db.GetWatchListEntryParams) (db.WatchListEntry, error) {
|
||||
return db.WatchListEntry{}, nil
|
||||
}
|
||||
|
||||
func (r *catalogRepoStub) GetContinueWatchingEntries(ctx context.Context, userID string) ([]db.GetContinueWatchingEntriesRow, error) {
|
||||
return r.allContinueRows, nil
|
||||
}
|
||||
|
||||
func (r *catalogRepoStub) GetContinueWatchingCarouselEntries(ctx context.Context, userID string, limit int64) ([]db.GetContinueWatchingEntriesRow, error) {
|
||||
r.carouselLimit = limit
|
||||
return r.carouselContinueRows, nil
|
||||
}
|
||||
|
||||
func TestGetCatalogSectionLimitsContinueWatchingCarousel(t *testing.T) {
|
||||
repo := &catalogRepoStub{
|
||||
allContinueRows: make([]db.GetContinueWatchingEntriesRow, 30),
|
||||
carouselContinueRows: []db.GetContinueWatchingEntriesRow{
|
||||
{AnimeID: 30},
|
||||
{AnimeID: 29},
|
||||
},
|
||||
}
|
||||
svc := NewAnimeService(nil, repo)
|
||||
|
||||
got, err := svc.GetCatalogSection(context.Background(), "user-1", "Continue")
|
||||
if err != nil {
|
||||
t.Fatalf("GetCatalogSection: %v", err)
|
||||
}
|
||||
if repo.carouselLimit != continueWatchingCarouselLimit {
|
||||
t.Fatalf("carousel limit = %d, want %d", repo.carouselLimit, continueWatchingCarouselLimit)
|
||||
}
|
||||
if len(got.ContinueWatching) != len(repo.carouselContinueRows) {
|
||||
t.Fatalf("len(ContinueWatching) = %d, want %d", len(got.ContinueWatching), len(repo.carouselContinueRows))
|
||||
}
|
||||
if got.ContinueWatching[0].AnimeID != 30 || got.ContinueWatching[1].AnimeID != 29 {
|
||||
t.Fatalf("ContinueWatching = %+v, want anime IDs [30 29]", got.ContinueWatching)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user