diff --git a/core/index.go b/core/index.go index 4abd4b6..88ffe3c 100644 --- a/core/index.go +++ b/core/index.go @@ -170,22 +170,33 @@ func parsePages(resp string) (*struct { return &pages, nil } -// populateFollowing populates the Following field in LandingArtworks with artworks -// from artists that the user is following. -func populateFollowing(followIDs []int, artworks map[string]ArtworkBrief) []ArtworkBrief { - following := make([]ArtworkBrief, 0, len(followIDs)) - - for _, id := range followIDs { - // Convert integer ID to string to match the keys in the artworks map - if artwork, exists := artworks[fmt.Sprint(id)]; exists { - following = append(following, artwork) +// populateArtworks is a generic helper function that maps a slice of string IDs +// to their corresponding ArtworkBrief objects. +func populateArtworks(ids []string, artworks map[string]ArtworkBrief) []ArtworkBrief { + populated := make([]ArtworkBrief, 0, len(ids)) + for _, id := range ids { + if artwork, exists := artworks[id]; exists { + populated = append(populated, artwork) } } - return following + return populated } -// populateRecommendedByTags populates the RecommendByTags field in LandingArtworks -// with artworks grouped under each recommended tag. +// populateFollowing converts int IDs to strings and uses the generic helper. +func populateFollowing(followIDs []int, artworks map[string]ArtworkBrief) []ArtworkBrief { + stringIDs := make([]string, 0, len(followIDs)) + for _, id := range followIDs { + stringIDs = append(stringIDs, fmt.Sprint(id)) + } + return populateArtworks(stringIDs, artworks) +} + +// populateRecommended uses the generic helper. +func populateRecommended(recommendedIDs []string, artworks map[string]ArtworkBrief) []ArtworkBrief { + return populateArtworks(recommendedIDs, artworks) +} + +// populateRecommendedByTags uses the generic helper for each tag's IDs. func populateRecommendedByTags(recommends []struct { Name string `json:"tag"` IDs []string `json:"ids"` @@ -194,12 +205,7 @@ func populateRecommendedByTags(recommends []struct { recommendByTags := make([]RecommendedTags, 0, len(recommends)) for _, r := range recommends { - artworksList := make([]ArtworkBrief, 0, len(r.IDs)) - for _, id := range r.IDs { - if artwork, exists := artworks[id]; exists { - artworksList = append(artworksList, artwork) - } - } + artworksList := populateArtworks(r.IDs, artworks) recommendByTags = append(recommendByTags, RecommendedTags{ Name: r.Name, Artworks: artworksList, @@ -208,20 +214,6 @@ func populateRecommendedByTags(recommends []struct { return recommendByTags } -// populateRecommended populates the Recommended field in LandingArtworks with general recommended artworks. -// -// It filters and maps recommended artwork IDs to their corresponding ArtworkBrief structs. -func populateRecommended(recommendedIDs []string, artworks map[string]ArtworkBrief) []ArtworkBrief { - recommended := make([]ArtworkBrief, 0, len(recommendedIDs)) - - for _, id := range recommendedIDs { - if artwork, exists := artworks[id]; exists { - recommended = append(recommended, artwork) - } - } - return recommended -} - // fetchRankings retrieves the current rankings based on the selected mode. // // It maps the landing page mode to the appropriate ranking mode and fetches the ranking data.