diff --git a/internal/app/service/recipe_service.go b/internal/app/service/recipe_service.go index ae732c4..acb5baa 100644 --- a/internal/app/service/recipe_service.go +++ b/internal/app/service/recipe_service.go @@ -203,14 +203,24 @@ func (s *RecipeService) SearchRecipes(filters domain.SearchFilters, userId *int, // GetUserRecipes returns a list of the recipes that the user has created. The user's // ID should be provided. Any errors will be bubbled to the caller. -func (s *RecipeService) GetUserRecipes(id int) ([]domain.Recipe, error) { - return s.recipeRepository.GetUserRecipes(id) +func (s *RecipeService) GetUserRecipes(userId int) ([]domain.Recipe, error) { + ids, err := s.recipeRepository.GetUserRecipesIds(userId) + if err != nil { + return []domain.Recipe{}, err + } + + return s.recipeRepository.GetRecipes(ids, &userId) } // GetUserFavoriteRecipes returns a list of the recipes that the user has marked as a // favorite. The user's ID should be provided. Any errors will be bubbled to the caller. -func (s *RecipeService) GetUserFavoriteRecipes(id int) ([]domain.Recipe, error) { - return s.recipeRepository.GetUserFavoriteRecipes(id) +func (s *RecipeService) GetUserFavoriteRecipes(userId int) ([]domain.Recipe, error) { + ids, err := s.recipeRepository.GetUserFavoriteRecipesIds(userId) + if err != nil { + return []domain.Recipe{}, err + } + + return s.recipeRepository.GetRecipes(ids, &userId) } // GetUserViewedRecipes returns a list of the most recent x (limit) recipes viewed by a user, from diff --git a/internal/domain/recipe/repository.go b/internal/domain/recipe/repository.go index c33681d..93de1f4 100644 --- a/internal/domain/recipe/repository.go +++ b/internal/domain/recipe/repository.go @@ -6,8 +6,8 @@ type RecipeRepository interface { GetRecipes(ids []int, userId *int) ([]Recipe, error) SearchRecipes(filters SearchFilters, userId *int, favorites bool) ([]int, error) CreateRecipeTags(recipe Recipe, tags []string) error - GetUserRecipes(id int) ([]Recipe, error) - GetUserFavoriteRecipes(id int) ([]Recipe, error) + GetUserRecipesIds(userId int) ([]int, error) + GetUserFavoriteRecipesIds(userId int) ([]int, error) GetRecipeTags(recipe *Recipe) error GetRecipeFavorite(recipe *Recipe, userId int) error GetRecipeOfTheWeekId(userId *int) (*int, error) diff --git a/internal/domain/recipe/service.go b/internal/domain/recipe/service.go index 91bf12f..9c6a75f 100644 --- a/internal/domain/recipe/service.go +++ b/internal/domain/recipe/service.go @@ -8,8 +8,8 @@ type RecipeService interface { CreateRecipe(ctx *gin.Context) (*Recipe, error) GetRecipe(id int, userId *int) (*Recipe, error) SearchRecipes(filters SearchFilters, userId *int, favorites bool) ([]Recipe, error) - GetUserRecipes(id int) ([]Recipe, error) - GetUserFavoriteRecipes(id int) ([]Recipe, error) + GetUserRecipes(userId int) ([]Recipe, error) + GetUserFavoriteRecipes(userId int) ([]Recipe, error) GetUserViewedRecipes(userId, limit int) ([]Recipe, error) GetUserMadeRecipes(userId, limit int) ([]Recipe, error) GetRecipeOfTheWeek(userId *int) (*Recipe, error) diff --git a/internal/infrastructure/database/repository/recipe_repository.go b/internal/infrastructure/database/repository/recipe_repository.go index c055aad..ea8376a 100644 --- a/internal/infrastructure/database/repository/recipe_repository.go +++ b/internal/infrastructure/database/repository/recipe_repository.go @@ -464,8 +464,8 @@ func (r *RecipeRepository) CreateRecipeTags(recipe domain.Recipe, tags []string) // authenticated or exists. If nothing is found, a blank slice will be returned. The resulting list // is sorted by the created dates, newest first. Any errors will be bubbled to the caller. // -// TODO: This should just return the IDs -func (r *RecipeRepository) GetUserRecipes(id int) ([]domain.Recipe, error) { +// 12/28/25: This now returns just the IDs, the service can handle fetching them. +func (r *RecipeRepository) GetUserRecipesIds(user_id int) ([]int, error) { query := ` SELECT id FROM recipes @@ -473,38 +473,31 @@ func (r *RecipeRepository) GetUserRecipes(id int) ([]domain.Recipe, error) { ORDER BY created DESC; ` - rows, err := r.db.Query(query, id) + rows, err := r.db.Query(query, user_id) if err != nil { return nil, fmt.Errorf("Failed to query DB for user recipes. %s\n", err.Error()) } defer rows.Close() - var recipes []domain.Recipe + var ids []int for rows.Next() { var r_id int if err := rows.Scan(&r_id); err != nil { - return []domain.Recipe{}, fmt.Errorf("Failed to scan ID from db. %s\n", err.Error()) + return []int{}, fmt.Errorf("Failed to scan ID from db. %s\n", err.Error()) } - recipe, err := r.GetRecipe(r_id, &id) - if err != nil { - return []domain.Recipe{}, err - } - - if recipe != nil { - recipes = append(recipes, *recipe) - } + ids = append(ids, r_id) } - return recipes, nil + return ids, nil } // GetUserRecipes gets a list of a users favorited recipes. This function does not ensure the user is // authenticated or exists. If nothing is found, a blank slice will be returned. The resulting list // is sorted by the created dates, newest first. Any errors will be bubbled to the caller. // -// TODO: This should just return the IDs -func (r *RecipeRepository) GetUserFavoriteRecipes(id int) ([]domain.Recipe, error) { +// 12/28/25: This now just returns the IDs, so the service can handle the fetching. +func (r *RecipeRepository) GetUserFavoriteRecipesIds(id int) ([]int, error) { query := ` SELECT r.id FROM favorites f @@ -518,24 +511,17 @@ func (r *RecipeRepository) GetUserFavoriteRecipes(id int) ([]domain.Recipe, erro } defer rows.Close() - var recipes []domain.Recipe + var ids []int for rows.Next() { var r_id int if err := rows.Scan(&r_id); err != nil { - return []domain.Recipe{}, fmt.Errorf("Failed to scan ID from db. %s\n", err.Error()) + return []int{}, fmt.Errorf("Failed to scan ID from db. %s\n", err.Error()) } - recipe, err := r.GetRecipe(r_id, &id) - if err != nil { - return []domain.Recipe{}, err - } - - if recipe != nil { - recipes = append(recipes, *recipe) - } + ids = append(ids, r_id) } - return recipes, nil + return ids, nil } // GetRecipeTags requires a recipe to be filled with at least an ID. This function will use the ID