- 菜谱API基本OK
This commit is contained in:
@@ -1,27 +1,108 @@
|
||||
package dao
|
||||
|
||||
import "git.rosy.net.cn/jx-callback/business/model"
|
||||
import (
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
)
|
||||
|
||||
func QueryFoodRecipes(db *DaoDB, keyword, userID string) (recipeList []*model.FoodRecipe, err error) {
|
||||
sql := `
|
||||
SELECT t1.*
|
||||
FROM food_recipe t1
|
||||
WHERE 1 = 1`
|
||||
sqlParams := []interface{}{}
|
||||
type FoodRecipeWithAction struct {
|
||||
model.FoodRecipe
|
||||
ActionType int8 `json:"actionType"`
|
||||
}
|
||||
|
||||
func QueryFoodRecipes(db *DaoDB, keyword string, recipeID int, authorID, userID string) (recipeList []*FoodRecipeWithAction, err error) {
|
||||
var sql string
|
||||
var sqlParams []interface{}
|
||||
if userID != "" {
|
||||
sql = `
|
||||
SELECT 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 = ?`
|
||||
sqlParams = []interface{}{
|
||||
userID,
|
||||
utils.DefaultTimeValue,
|
||||
utils.DefaultTimeValue,
|
||||
}
|
||||
} else {
|
||||
sql = `
|
||||
SELECT t1.*
|
||||
FROM food_recipe t1
|
||||
WHERE t1.deleted_at = ?`
|
||||
sqlParams = []interface{}{
|
||||
utils.DefaultTimeValue,
|
||||
}
|
||||
}
|
||||
if keyword != "" {
|
||||
keywordLike := "%" + keyword + "%"
|
||||
sql += " AND (t1.name LIKE ?"
|
||||
sqlParams = append(sqlParams, keywordLike)
|
||||
sql += ")"
|
||||
}
|
||||
if userID != "" {
|
||||
if recipeID > 0 {
|
||||
sql += " AND t1.id = ?"
|
||||
sqlParams = append(sqlParams, recipeID)
|
||||
}
|
||||
if authorID != "" {
|
||||
sql += " AND t1.author_id = ?"
|
||||
sqlParams = append(sqlParams, userID)
|
||||
sqlParams = append(sqlParams, authorID)
|
||||
}
|
||||
err = GetRows(db, &recipeList, sql, sqlParams...)
|
||||
return recipeList, err
|
||||
}
|
||||
|
||||
func GetRecommendFoodRecipes(db *DaoDB, keyword, userID string) (recipeList []*model.FoodRecipe, err error) {
|
||||
return QueryFoodRecipes(db, keyword, userID)
|
||||
if list, err := QueryFoodRecipes(db, keyword, 0, userID, ""); err == nil {
|
||||
recipeList = FoodRecipeWithActionList2Recipe(list)
|
||||
}
|
||||
return recipeList, err
|
||||
}
|
||||
|
||||
func FoodRecipeWithActionList2Recipe(recipeList []*FoodRecipeWithAction) (outRecipeList []*model.FoodRecipe) {
|
||||
for _, v := range recipeList {
|
||||
outRecipeList = append(outRecipeList, &v.FoodRecipe)
|
||||
}
|
||||
return outRecipeList
|
||||
}
|
||||
|
||||
func QueryFoodRecipesItems(db *DaoDB, recipeID int) (recipeItemList []*model.FoodRecipeItem, err error) {
|
||||
sql := `
|
||||
SELECT t1.*
|
||||
FROM food_recipe_item t1
|
||||
WHERE t1.deleted_at = ? AND t1.recipe_id = ?`
|
||||
sqlParams := []interface{}{
|
||||
utils.DefaultTimeValue,
|
||||
recipeID,
|
||||
}
|
||||
sql += " ORDER BY t1.index"
|
||||
err = GetRows(db, &recipeItemList, sql, sqlParams...)
|
||||
return recipeItemList, err
|
||||
}
|
||||
|
||||
func QueryFoodRecipesItemChoices(db *DaoDB, recipeID int) (recipeItemChoiceList []*model.FoodRecipeItemChoice, err error) {
|
||||
sql := `
|
||||
SELECT t1.*
|
||||
FROM food_recipe_item_choice t1
|
||||
WHERE t1.deleted_at = ? AND t1.recipe_id = ?`
|
||||
sqlParams := []interface{}{
|
||||
utils.DefaultTimeValue,
|
||||
recipeID,
|
||||
}
|
||||
sql += " ORDER BY t1.index, t1.choice_index"
|
||||
err = GetRows(db, &recipeItemChoiceList, sql, sqlParams...)
|
||||
return recipeItemChoiceList, err
|
||||
}
|
||||
|
||||
func QueryFoodRecipesSteps(db *DaoDB, recipeID int) (recipeStepList []*model.FoodRecipeStep, err error) {
|
||||
sql := `
|
||||
SELECT t1.*
|
||||
FROM food_recipe_step t1
|
||||
WHERE t1.deleted_at = ? AND t1.recipe_id = ?`
|
||||
sqlParams := []interface{}{
|
||||
utils.DefaultTimeValue,
|
||||
recipeID,
|
||||
}
|
||||
sql += " ORDER BY t1.index"
|
||||
err = GetRows(db, &recipeStepList, sql, sqlParams...)
|
||||
return recipeStepList, err
|
||||
}
|
||||
|
||||
40
business/model/dao/food_recipe_test.go
Normal file
40
business/model/dao/food_recipe_test.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestQueryRecipes(t *testing.T) {
|
||||
db := GetDB()
|
||||
recipeList, err := QueryFoodRecipes(db, "", -1, "", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(recipeList) > 0 {
|
||||
t.Fatal("should not return list")
|
||||
}
|
||||
|
||||
recipeItemList, err := QueryFoodRecipesItems(db, -1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(recipeItemList) > 0 {
|
||||
t.Fatal("should not return list")
|
||||
}
|
||||
|
||||
choiceList, err := QueryFoodRecipesItemChoices(db, -1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(choiceList) > 0 {
|
||||
t.Fatal("should not return list")
|
||||
}
|
||||
|
||||
stepList, err := QueryFoodRecipesSteps(db, -1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(stepList) > 0 {
|
||||
t.Fatal("should not return list")
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ func TestFormalizeStoreStatus(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetStoreList4Role(t *testing.T) {
|
||||
storeList, err := GetStoreList(GetDB(), "NiuBi")
|
||||
storeList, err := GetStoreList(GetDB(), nil, nil, "NiuBi")
|
||||
t.Log(utils.Format4Output(storeList, false))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -19,6 +19,17 @@ type FoodRecipeStepParam struct {
|
||||
Img string `orm:"size(48)" json:"img"`
|
||||
}
|
||||
|
||||
type FoodRecipeItem struct {
|
||||
model.FoodRecipeItem
|
||||
ItemChoiceList []*model.FoodRecipeItemChoice
|
||||
}
|
||||
|
||||
type FoodRecipeDetail struct {
|
||||
model.FoodRecipe
|
||||
ItemList []*FoodRecipeItem
|
||||
StepList []*model.FoodRecipeStep
|
||||
}
|
||||
|
||||
func updateFoodRecipeItemAndStep(ctx *jxcontext.Context, db *dao.DaoDB, recipeID int, itemList []*FoodRecipeItemParam, stepList []*FoodRecipeStepParam) (err error) {
|
||||
for k, v := range itemList {
|
||||
if len(v.SkuIDs) == 0 {
|
||||
@@ -29,7 +40,7 @@ func updateFoodRecipeItemAndStep(ctx *jxcontext.Context, db *dao.DaoDB, recipeID
|
||||
return err
|
||||
}
|
||||
if len(v.SkuIDs) != len(skuIDs) {
|
||||
return fmt.Errorf("某些SkuIDs不存在")
|
||||
return fmt.Errorf("某些SkuID不存在")
|
||||
}
|
||||
|
||||
item := &model.FoodRecipeItem{
|
||||
@@ -158,7 +169,131 @@ func UpdateFoodRecipe(ctx *jxcontext.Context, recipeID int, mapData map[string]i
|
||||
return err
|
||||
}
|
||||
|
||||
func QueryFoodRecipes(ctx *jxcontext.Context, keyword, userID string) (recipeList []*model.FoodRecipe, err error) {
|
||||
recipeList, err = dao.QueryFoodRecipes(dao.GetDB(), keyword, userID)
|
||||
func QueryFoodRecipes(ctx *jxcontext.Context, keyword, authorID string) (recipeList []*dao.FoodRecipeWithAction, err error) {
|
||||
_, userID := ctx.GetMobileAndUserID()
|
||||
recipeList, err = dao.QueryFoodRecipes(dao.GetDB(), keyword, 0, authorID, userID)
|
||||
return recipeList, err
|
||||
}
|
||||
|
||||
func GetRecommendFoodRecipes(ctx *jxcontext.Context, keyword string) (recipeList []*model.FoodRecipe, err error) {
|
||||
_, userID := ctx.GetMobileAndUserID()
|
||||
recipeList, err = dao.GetRecommendFoodRecipes(dao.GetDB(), keyword, userID)
|
||||
return recipeList, 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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(recipeList) == 0 {
|
||||
return nil, fmt.Errorf("找不到菜谱:%d", recipeID)
|
||||
}
|
||||
|
||||
itemList, err := dao.QueryFoodRecipesItems(db, recipeID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
itemChoiceList, err := dao.QueryFoodRecipesItemChoices(db, recipeID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
choiceMap := make(map[int8][]*model.FoodRecipeItemChoice)
|
||||
for _, v := range itemChoiceList {
|
||||
choiceMap[v.Index] = append(choiceMap[v.Index], v)
|
||||
}
|
||||
|
||||
stepList, err := dao.QueryFoodRecipesSteps(db, recipeID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
recipeDetail = &FoodRecipeDetail{
|
||||
FoodRecipe: recipeList[0].FoodRecipe,
|
||||
StepList: stepList,
|
||||
}
|
||||
for _, v := range itemList {
|
||||
recipeDetail.ItemList = append(recipeDetail.ItemList, &FoodRecipeItem{
|
||||
FoodRecipeItem: *v,
|
||||
ItemChoiceList: choiceMap[v.Index],
|
||||
})
|
||||
}
|
||||
return recipeDetail, err
|
||||
}
|
||||
|
||||
func VoteFoodRecipe(ctx *jxcontext.Context, recipeID, voteType int) (err error) {
|
||||
_, userID := ctx.GetMobileAndUserID()
|
||||
if userID == "" {
|
||||
return fmt.Errorf("内部错误,找不到用户")
|
||||
}
|
||||
|
||||
db := dao.GetDB()
|
||||
recipeList, err := dao.QueryFoodRecipes(db, "", recipeID, "", userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(recipeList) == 0 {
|
||||
return fmt.Errorf("找不到菜谱:%d", recipeID)
|
||||
}
|
||||
recipe := &recipeList[0].FoodRecipe
|
||||
|
||||
dao.Begin(db)
|
||||
defer func() {
|
||||
if r := recover(); r != nil || err != nil {
|
||||
dao.Rollback(db)
|
||||
if r != nil {
|
||||
panic(r)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
recipeUser := &model.FoodRecipeUser{
|
||||
RecipeID: recipeID,
|
||||
UserID: userID,
|
||||
}
|
||||
if err = dao.GetEntity(db, recipeUser); err != nil {
|
||||
if dao.IsNoRowsError(err) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
actionMask := int8(0)
|
||||
if voteType > 0 {
|
||||
recipe.UpvoteCount++
|
||||
actionMask = model.RecipeActionUpvote
|
||||
} else if voteType < 0 {
|
||||
recipe.DownvoteCount++
|
||||
actionMask = model.RecipeActionDownvote
|
||||
}
|
||||
if recipeUser.ActionType&model.RecipeActionUpvote != 0 {
|
||||
recipe.UpvoteCount--
|
||||
} else if recipeUser.ActionType&model.RecipeActionDownvote != 0 {
|
||||
recipe.DownvoteCount--
|
||||
}
|
||||
|
||||
if recipeUser.ActionType&actionMask != 0 || recipeUser.ActionType&(model.RecipeActionUpvote|model.RecipeActionDownvote) == 0 {
|
||||
return fmt.Errorf("已经做过此操作了")
|
||||
}
|
||||
|
||||
recipeUser.ActionType &= actionMask | ^(model.RecipeActionUpvote | model.RecipeActionDownvote)
|
||||
if recipeUser.ID == 0 {
|
||||
dao.WrapAddIDCULDEntity(recipeUser, ctx.GetUserName())
|
||||
err = dao.CreateEntity(db, recipeUser)
|
||||
} else {
|
||||
recipeUser.LastOperator = ctx.GetUserName()
|
||||
_, err = dao.UpdateEntity(db, recipeUser)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = dao.UpdateEntity(db, recipe, "UpvoteCount", "DownvoteCount")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dao.Commit(db)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user