- 修改菜谱的几个bug
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -169,22 +169,34 @@ func UpdateFoodRecipe(ctx *jxcontext.Context, recipeID int, mapData map[string]i
|
||||
return err
|
||||
}
|
||||
|
||||
func QueryFoodRecipes(ctx *jxcontext.Context, keyword, authorID string) (recipeList []*dao.FoodRecipeWithAction, err error) {
|
||||
func QueryFoodRecipes(ctx *jxcontext.Context, keyword, authorID string, offset, pageSize int) (recipeInfo *model.PagedInfo, err error) {
|
||||
_, userID := ctx.GetMobileAndUserID()
|
||||
recipeList, err = dao.QueryFoodRecipes(dao.GetDB(), keyword, 0, authorID, userID)
|
||||
return recipeList, err
|
||||
recipeList, totalCount, err := dao.QueryFoodRecipes(dao.GetDB(), keyword, 0, authorID, userID, offset, pageSize)
|
||||
if err == nil {
|
||||
recipeInfo = &model.PagedInfo{
|
||||
TotalCount: totalCount,
|
||||
Data: recipeList,
|
||||
}
|
||||
}
|
||||
return recipeInfo, err
|
||||
}
|
||||
|
||||
func GetRecommendFoodRecipes(ctx *jxcontext.Context, keyword string) (recipeList []*model.FoodRecipe, err error) {
|
||||
func GetRecommendFoodRecipes(ctx *jxcontext.Context, keyword string, offset, pageSize int) (recipeInfo *model.PagedInfo, err error) {
|
||||
_, userID := ctx.GetMobileAndUserID()
|
||||
recipeList, err = dao.GetRecommendFoodRecipes(dao.GetDB(), keyword, userID)
|
||||
return recipeList, err
|
||||
recipeList, totalCount, err := dao.GetRecommendFoodRecipes(dao.GetDB(), keyword, userID, offset, pageSize)
|
||||
if err == nil {
|
||||
recipeInfo = &model.PagedInfo{
|
||||
TotalCount: totalCount,
|
||||
Data: recipeList,
|
||||
}
|
||||
}
|
||||
return recipeInfo, err
|
||||
}
|
||||
|
||||
func GetRecipeDetail(ctx *jxcontext.Context, recipeID int) (recipeDetail *FoodRecipeDetail, err error) {
|
||||
_, userID := ctx.GetMobileAndUserID()
|
||||
db := dao.GetDB()
|
||||
recipeList, err := dao.QueryFoodRecipes(db, "", recipeID, "", userID)
|
||||
recipeList, _, err := dao.QueryFoodRecipes(db, "", recipeID, "", userID, 0, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -231,7 +243,7 @@ func VoteFoodRecipe(ctx *jxcontext.Context, recipeID, voteType int) (err error)
|
||||
}
|
||||
|
||||
db := dao.GetDB()
|
||||
recipeList, err := dao.QueryFoodRecipes(db, "", recipeID, "", userID)
|
||||
recipeList, _, err := dao.QueryFoodRecipes(db, "", recipeID, "", userID, 0, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -274,7 +286,7 @@ func VoteFoodRecipe(ctx *jxcontext.Context, recipeID, voteType int) (err error)
|
||||
recipe.DownvoteCount--
|
||||
}
|
||||
|
||||
if recipeUser.ActionType&actionMask != 0 || recipeUser.ActionType&(model.RecipeActionUpvote|model.RecipeActionDownvote) == 0 {
|
||||
if recipeUser.ActionType&actionMask != 0 || (recipeUser.ActionType&(model.RecipeActionUpvote|model.RecipeActionDownvote) == 0 && voteType == 0) {
|
||||
return fmt.Errorf("已经做过此操作了")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user