(FIX): Migrated services to returning the IDs.

This commit is contained in:
Hayden Hargreaves 2025-12-28 18:06:21 -07:00
parent 04b6ac918a
commit 6bbd58b471
4 changed files with 31 additions and 35 deletions

View File

@ -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 // 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. // ID should be provided. Any errors will be bubbled to the caller.
func (s *RecipeService) GetUserRecipes(id int) ([]domain.Recipe, error) { func (s *RecipeService) GetUserRecipes(userId int) ([]domain.Recipe, error) {
return s.recipeRepository.GetUserRecipes(id) 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 // 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. // 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) { func (s *RecipeService) GetUserFavoriteRecipes(userId int) ([]domain.Recipe, error) {
return s.recipeRepository.GetUserFavoriteRecipes(id) 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 // GetUserViewedRecipes returns a list of the most recent x (limit) recipes viewed by a user, from

View File

@ -6,8 +6,8 @@ type RecipeRepository interface {
GetRecipes(ids []int, userId *int) ([]Recipe, error) GetRecipes(ids []int, userId *int) ([]Recipe, error)
SearchRecipes(filters SearchFilters, userId *int, favorites bool) ([]int, error) SearchRecipes(filters SearchFilters, userId *int, favorites bool) ([]int, error)
CreateRecipeTags(recipe Recipe, tags []string) error CreateRecipeTags(recipe Recipe, tags []string) error
GetUserRecipes(id int) ([]Recipe, error) GetUserRecipesIds(userId int) ([]int, error)
GetUserFavoriteRecipes(id int) ([]Recipe, error) GetUserFavoriteRecipesIds(userId int) ([]int, error)
GetRecipeTags(recipe *Recipe) error GetRecipeTags(recipe *Recipe) error
GetRecipeFavorite(recipe *Recipe, userId int) error GetRecipeFavorite(recipe *Recipe, userId int) error
GetRecipeOfTheWeekId(userId *int) (*int, error) GetRecipeOfTheWeekId(userId *int) (*int, error)

View File

@ -8,8 +8,8 @@ type RecipeService interface {
CreateRecipe(ctx *gin.Context) (*Recipe, error) CreateRecipe(ctx *gin.Context) (*Recipe, error)
GetRecipe(id int, userId *int) (*Recipe, error) GetRecipe(id int, userId *int) (*Recipe, error)
SearchRecipes(filters SearchFilters, userId *int, favorites bool) ([]Recipe, error) SearchRecipes(filters SearchFilters, userId *int, favorites bool) ([]Recipe, error)
GetUserRecipes(id int) ([]Recipe, error) GetUserRecipes(userId int) ([]Recipe, error)
GetUserFavoriteRecipes(id int) ([]Recipe, error) GetUserFavoriteRecipes(userId int) ([]Recipe, error)
GetUserViewedRecipes(userId, limit int) ([]Recipe, error) GetUserViewedRecipes(userId, limit int) ([]Recipe, error)
GetUserMadeRecipes(userId, limit int) ([]Recipe, error) GetUserMadeRecipes(userId, limit int) ([]Recipe, error)
GetRecipeOfTheWeek(userId *int) (*Recipe, error) GetRecipeOfTheWeek(userId *int) (*Recipe, error)

View File

@ -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 // 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. // is sorted by the created dates, newest first. Any errors will be bubbled to the caller.
// //
// TODO: This should just return the IDs // 12/28/25: This now returns just the IDs, the service can handle fetching them.
func (r *RecipeRepository) GetUserRecipes(id int) ([]domain.Recipe, error) { func (r *RecipeRepository) GetUserRecipesIds(user_id int) ([]int, error) {
query := ` query := `
SELECT id SELECT id
FROM recipes FROM recipes
@ -473,38 +473,31 @@ func (r *RecipeRepository) GetUserRecipes(id int) ([]domain.Recipe, error) {
ORDER BY created DESC; ORDER BY created DESC;
` `
rows, err := r.db.Query(query, id) rows, err := r.db.Query(query, user_id)
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to query DB for user recipes. %s\n", err.Error()) return nil, fmt.Errorf("Failed to query DB for user recipes. %s\n", err.Error())
} }
defer rows.Close() defer rows.Close()
var recipes []domain.Recipe var ids []int
for rows.Next() { for rows.Next() {
var r_id int var r_id int
if err := rows.Scan(&r_id); err != nil { 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) ids = append(ids, r_id)
if err != nil {
return []domain.Recipe{}, err
}
if recipe != nil {
recipes = append(recipes, *recipe)
}
} }
return recipes, nil return ids, nil
} }
// GetUserRecipes gets a list of a users favorited recipes. This function does not ensure the user is // 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 // 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. // is sorted by the created dates, newest first. Any errors will be bubbled to the caller.
// //
// TODO: This should just return the IDs // 12/28/25: This now just returns the IDs, so the service can handle the fetching.
func (r *RecipeRepository) GetUserFavoriteRecipes(id int) ([]domain.Recipe, error) { func (r *RecipeRepository) GetUserFavoriteRecipesIds(id int) ([]int, error) {
query := ` query := `
SELECT r.id SELECT r.id
FROM favorites f FROM favorites f
@ -518,24 +511,17 @@ func (r *RecipeRepository) GetUserFavoriteRecipes(id int) ([]domain.Recipe, erro
} }
defer rows.Close() defer rows.Close()
var recipes []domain.Recipe var ids []int
for rows.Next() { for rows.Next() {
var r_id int var r_id int
if err := rows.Scan(&r_id); err != nil { 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) ids = append(ids, r_id)
if err != nil {
return []domain.Recipe{}, err
}
if recipe != nil {
recipes = append(recipes, *recipe)
}
} }
return recipes, nil return ids, nil
} }
// GetRecipeTags requires a recipe to be filled with at least an ID. This function will use the ID // GetRecipeTags requires a recipe to be filled with at least an ID. This function will use the ID