- 修改菜谱的几个bug

This commit is contained in:
gazebo
2019-09-17 17:25:24 +08:00
parent 8357152344
commit 8dbb872f4c
3 changed files with 48 additions and 20 deletions

View File

@@ -10,12 +10,12 @@ type FoodRecipeWithAction struct {
ActionType int8 `json:"actionType"`
}
func QueryFoodRecipes(db *DaoDB, keyword string, recipeID int, authorID, userID string) (recipeList []*FoodRecipeWithAction, err error) {
func QueryFoodRecipes(db *DaoDB, keyword string, recipeID int, authorID, userID string, offset, pageSize int) (recipeList []*FoodRecipeWithAction, totalCount int, err error) {
var sql string
var sqlParams []interface{}
if userID != "" {
sql = `
SELECT t1.*, t2.action_type
SELECT SQL_CALC_FOUND_ROWS t1.*, t2.action_type
FROM food_recipe t1
LEFT JOIN food_recipe_user t2 ON t2.recipe_id = t1.id AND t2.user_id = ? AND t2.deleted_at = ?
WHERE t1.deleted_at = ?`
@@ -26,7 +26,7 @@ func QueryFoodRecipes(db *DaoDB, keyword string, recipeID int, authorID, userID
}
} else {
sql = `
SELECT t1.*
SELECT SQL_CALC_FOUND_ROWS t1.*
FROM food_recipe t1
WHERE t1.deleted_at = ?`
sqlParams = []interface{}{
@@ -47,15 +47,27 @@ func QueryFoodRecipes(db *DaoDB, keyword string, recipeID int, authorID, userID
sql += " AND t1.author_id = ?"
sqlParams = append(sqlParams, authorID)
}
err = GetRows(db, &recipeList, sql, sqlParams...)
return recipeList, err
offset = FormalizePageOffset(offset)
pageSize = FormalizePageSize(pageSize)
sql += `
ORDER BY t1.created_at DESC
LIMIT ? OFFSET ?`
sqlParams = append(sqlParams, pageSize, offset)
Begin(db)
defer Rollback(db)
if err = GetRows(db, &recipeList, sql, sqlParams...); err == nil {
totalCount = GetLastTotalRowCount(db)
}
return recipeList, totalCount, err
}
func GetRecommendFoodRecipes(db *DaoDB, keyword, userID string) (recipeList []*model.FoodRecipe, err error) {
if list, err := QueryFoodRecipes(db, keyword, 0, userID, ""); err == nil {
func GetRecommendFoodRecipes(db *DaoDB, keyword, userID string, offset, pageSize int) (recipeList []*model.FoodRecipe, totalCount int, err error) {
list, totalCount, err := QueryFoodRecipes(db, keyword, 0, userID, "", offset, pageSize)
if err == nil {
recipeList = FoodRecipeWithActionList2Recipe(list)
}
return recipeList, err
return recipeList, totalCount, err
}
func FoodRecipeWithActionList2Recipe(recipeList []*FoodRecipeWithAction) (outRecipeList []*model.FoodRecipe) {