瀏覽代碼

fixed food api bug

maguohua 8 年之前
父節點
當前提交
ef4d81c6bd
共有 3 個文件被更改,包括 38 次插入3 次删除
  1. 35 2
      controller/shopping/food.js
  2. 2 0
      controller/shopping/shop.js
  3. 1 1
      models/ugc/rating.js

+ 35 - 2
controller/shopping/food.js

@@ -6,11 +6,44 @@ import formidable from 'formidable'
 
 class Food extends BaseComponent{
 	constructor(){
-		super()
+		super();
+		this.defaultData = [{
+			name: '热销榜',
+			description: '大家喜欢吃,才叫真好吃。', 
+			icon_url: "5da3872d782f707b4c82ce4607c73d1ajpeg",
+			is_selected: true,
+			type: 1,
+		}, {
+			name: '优惠',
+			description: '美味又实惠, 大家快来抢!', 
+			icon_url: "4735c4342691749b8e1a531149a46117jpeg",
+			type: 1,
+		}]
+		this.initData = this.initData.bind(this);
 		this.addFood = this.addFood.bind(this);
 		this.getCategory = this.getCategory.bind(this);
 		this.addCategory = this.addCategory.bind(this);
 	}
+	async initData(restaurant_id){
+		this.defaultData.forEach(async (item) => {
+			let category_id;
+			try{
+				category_id = await this.getId('category_id');
+			}catch(err){
+				console.log('获取category_id失败');
+				throw new Error(err);
+			}
+			const Category = {...item, id: category_id, restaurant_id};
+			const newFood = new FoodModel(Category);
+			try{
+				await newFood.save();
+				console.log('初始化食品数据成功');
+			}catch(err){
+				console.log('初始化食品数据失败');
+				throw new Error(err);
+			}
+		})
+	}
 	async getCategory(req, res, next){
 		const restaurant_id = req.params.restaurant_id;
 		try{
@@ -271,7 +304,7 @@ class Food extends BaseComponent{
 			return
 		}
 		try{
-			const menu = await FoodModel.find({restaurant_id}, '-_id');
+			const menu = await FoodModel.find({restaurant_id, $where: function(){return this.foods.length}}, '-_id');
 			res.send(menu);
 		}catch(err){
 			console.log('获取食品数据失败', err);

+ 2 - 0
controller/shopping/shop.js

@@ -2,6 +2,7 @@
 
 import ShopModel from '../../models/shopping/shop'
 import AddressComponent from '../../prototype/addressComponent'
+import Food from './food'
 import formidable from 'formidable'
 import CategoryHandle from './category'
 import Rating from '../ugc/rating'
@@ -159,6 +160,7 @@ class Shop extends AddressComponent{
 				await shop.save();
 				CategoryHandle.addCategory(fields.category)
 				Rating.initData(restaurant_id);
+				Food.initData(restaurant_id);
 				res.send({
 					status: 1,
 					sussess: '添加餐馆成功',

+ 1 - 1
models/ugc/rating.js

@@ -45,7 +45,7 @@ rateSchema.index({restaurant_id: 1});
 
 rateSchema.statics.initData = async function (restaurant_id){
 	try{
-		const data = await this.findOne();
+		const data = await this.findOne({restaurant_id});
 		if (!data) {
 			const newRating = {
 				restaurant_id,