123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- 'use strict';
- import CategoryModel from '../../models/shopping/category'
- import BaseComponent from '../../prototype/baseComponent'
- import DeliveryModel from '../../models/shopping/delivery'
- import ActivityModel from '../../models/shopping/activity'
- class Category extends BaseComponent{
- constructor(){
- super()
- }
- //获取所有餐馆分类和数量
- async getCategories(req, res, next){
- try{
- const categories = await CategoryModel.find({}, '-_id');
- res.send(categories);
- }catch(err){
- console.log('获取categories失败');
- res.send({
- status: 0,
- type: 'ERROR_DATA',
- message: '获取categories失败'
- })
- }
- }
- async addCategory(type){
- try{
- await CategoryModel.addCategory(type)
- }catch(err){
- console.log('增加category数量失败');
- }
- }
- async findById(id){
- try{
- const CateEntity = await CategoryModel.findOne({'sub_categories.id': id});
- let categoName = CateEntity.name;
- CateEntity.sub_categories.forEach(item => {
- if (item.id == id) {
- categoName += '/' + item.name;
- }
- })
- return categoName
- }catch(err){
- console.log('通过category id获取数据失败')
- throw new Error(err)
- }
- }
- //获取配送方式
- async getDelivery(req, res, next){
- try{
- const deliveries = await DeliveryModel.find({}, '-_id');
- res.send(deliveries)
- }catch(err){
- console.log('获取配送方式数据失败');
- res.send({
- status: 0,
- type: 'ERROR_DATA',
- message: '获取配送方式数据失败'
- })
- }
- }
- //获取活动列表
- async getActivity(req, res, next){
- try{
- const activities = await ActivityModel.find({}, '-_id');
- res.send(activities)
- }catch(err){
- console.log('获取活动数据失败');
- res.send({
- status: 0,
- type: 'ERROR_DATA',
- message: '获取活动数据失败'
- })
- }
- }
- }
- export default new Category()
|