- 菜谱API基本OK

This commit is contained in:
gazebo
2019-09-17 14:36:39 +08:00
parent daa72b7b7d
commit bf8fd2f255
6 changed files with 401 additions and 16 deletions

View File

@@ -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
}

View 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")
}
}

View File

@@ -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)