diff --git a/app/controller/catering/desk.js b/app/controller/catering/desk.js new file mode 100644 index 0000000..e0da3ed --- /dev/null +++ b/app/controller/catering/desk.js @@ -0,0 +1,137 @@ +const Response = require('../../util/response') +const ServiceDesk = require('../../services/catering/desk') + +module.exports = { + /** + * @api {get} /api/catering/v1/stores/:storeId/desks desk列表 + * @apiGroup desk + * @apiPermission store + * @apiVersion 1.0.0 + * @apiParam {String} param 参数 + * @apiSuccess {String} data 返回数据 + * @apiSuccessExample {json} Visual Preview: + * { + "success": true, + "code": 200, + "data": [ + { + "id": 2, + "store_id": 1, + "name": "江边1", + "sort": 0, + "status": 1, + "created_at": "2018-07-12T06:50:27.000Z", + "updated_at": "2018-07-12T06:50:27.000Z" + } + ], + "message": "请求成功" + } + */ + index: async function (ctx) { + let data = await ServiceDesk.list(ctx.params.storeId) + + return Response.output(ctx, data) + }, + + /** + * @api {get} /api/catering/v1/stores/:storeId/desks/:deskId desk详情 + * @apiGroup desk + * @apiPermission store + * @apiVersion 1.0.0 + * @apiParam {String} param 参数 + * @apiSuccess {String} data 返回数据 + * @apiSuccessExample {json} Visual Preview: + * { + "success": true, + "code": 200, + "data": { + "id": 2, + "store_id": 1, + "name": "江边1", + "sort": 0, + "status": 1, + "created_at": "2018-07-12T06:50:27.000Z", + "updated_at": "2018-07-12T06:50:27.000Z" + }, + "message": "请求成功" + } + */ + detail: async function (ctx) { + let data = await ServiceDesk.detail(ctx.params.storeId, ctx.params.deskId) + + return Response.output(ctx, data) + }, + + /** + * @api {post} /api/catering/v1/stores/:storeId/desks 新增desk + * @apiGroup desk + * @apiPermission store + * @apiVersion 1.0.0 + * @apiParam {String} param 参数 + * @apiSuccess {String} data 返回数据 + * @apiSuccessExample {json} Visual Preview: + * { + "success": true, + "code": 200, + "data": { + "store_id": "1", + "name": "江边1", + "id": 2 + }, + "message": "请求成功" + } + */ + add: async function (ctx) { + let result = await ServiceDesk.add(ctx.params.storeId, ctx.input) + + return Response.output(ctx, result) + }, + + /** + * @api {put} /api/catering/v1/stores/:storeId/desks/:deskId 编辑desk + * @apiGroup desk + * @apiPermission store + * @apiVersion 1.0.0 + * + * @apiParam {String} param 参数 + * @apiSuccess {String} data 返回数据 + * + * @apiSuccessExample {json} Visual Preview: + * { + "success": true, + "code": 200, + "data": { + "name": "haibian", + "status": 1 + }, + "message": "请求成功" + } + */ + edit: async function (ctx) { + let result = await ServiceDesk.edit(ctx.params.storeId, ctx.params.deskId, ctx.input) + + return Response.output(ctx, result) + }, + + /** + * @api {delete} /api/catering/v1/stores/:storeId/desks/:deskId 删除desk + * @apiGroup desk + * @apiPermission store + * @apiVersion 1.0.0 + * @apiSuccessExample {json} Visual Preview: + * { + "success": true, + "code": 200, + "message": "请求成功", + "data": {} + } + */ + delete: async function (ctx) { + let data = { + status: -1 + } + let result = await ServiceDesk.edit(ctx.params.storeId, ctx.params.deskId, data) + return Response.output(ctx) + }, + +} diff --git a/app/controller/catering/product.js b/app/controller/catering/product.js new file mode 100644 index 0000000..05021d9 --- /dev/null +++ b/app/controller/catering/product.js @@ -0,0 +1,179 @@ +const Response = require('../../util/response') +const ServiceProduct = require('../../services/catering/product') + +module.exports = { + /** + * @api {get} /api/catering/v1/stores/:storeId/products product列表 + * @apiGroup product + * @apiPermission store + * @apiVersion 1.0.0 + * @apiParam {String} param 参数 + * @apiSuccess {String} data 返回数据 + * @apiSuccessExample {json} Visual Preview: + * { + "success": true, + "code": 200, + "data": [ + { + "id": 1, + "store_id": 1, + "title": "123", + "thumb": "products/1/sda", + "description": "ddd", + "price": 5, + "online_time": "2018-07-12T07:04:42.000Z", + "sort": 0, + "set_top": 0, + "product_type_id": 0, + "status": 1, + "approved_id": 1, + "viewed": 0, + "sales_volume": 0, + "created_at": "2018-07-12T07:04:42.000Z", + "updated_at": "2018-07-12T07:06:51.000Z" + }, + { + "id": 2, + "store_id": 1, + "title": "炒螺", + "thumb": "products/1/sda", + "description": "ddd", + "price": 15, + "online_time": "2018-07-12T07:06:57.000Z", + "sort": 0, + "set_top": 0, + "product_type_id": 0, + "status": 0, + "approved_id": 1, + "viewed": 0, + "sales_volume": 0, + "created_at": "2018-07-12T07:06:57.000Z", + "updated_at": "2018-07-12T07:06:57.000Z" + } + ], + "message": "请求成功" + } + */ + index: async function (ctx) { + let data = await ServiceProduct.list(ctx.params.storeId) + + return Response.output(ctx, data) + }, + + /** + * @api {get} /api/catering/v1/stores/:storeId/products/:productId product详情 + * @apiGroup product + * @apiPermission store + * @apiVersion 1.0.0 + * @apiParam {String} param 参数 + * @apiSuccess {String} data 返回数据 + * @apiSuccessExample {json} Visual Preview: + * { + "success": true, + "code": 200, + "data": { + "id": 1, + "store_id": 1, + "title": "炒螺", + "thumb": "products/1/sda", + "description": "ddd", + "price": 5, + "online_time": "2018-07-12T07:04:42.000Z", + "sort": 0, + "set_top": 0, + "product_type_id": 0, + "status": 0, + "approved_id": 1, + "viewed": 0, + "sales_volume": 0, + "created_at": "2018-07-12T07:04:42.000Z", + "updated_at": "2018-07-12T07:04:42.000Z" + }, + "message": "请求成功" + } + */ + detail: async function (ctx) { + let data = await ServiceProduct.detail(ctx.params.storeId, ctx.params.productId) + + return Response.output(ctx, data) + }, + + /** + * @api {post} /api/catering/v1/stores/:storeId/products 新增product + * @apiGroup product + * @apiPermission store + * @apiVersion 1.0.0 + * @apiParam {String} param 参数 + * @apiSuccess {String} data 返回数据 + * @apiSuccessExample {json} Visual Preview: + * { + "success": true, + "code": 200, + "data": { + "title": "炒螺", + "thumb": "products/1/sda", + "description": "ddd", + "price": "15", + "store_id": "1", + "id": 2 + }, + "message": "请求成功" + } + */ + add: async function (ctx) { + let result = await ServiceProduct.add(ctx.params.storeId, ctx.input) + + return Response.output(ctx, result) + }, + + /** + * @api {put} /api/catering/v1/stores/:storeId/products/:productId 编辑product + * @apiGroup product + * @apiPermission store + * @apiVersion 1.0.0 + * + * @apiParam {String} param 参数 + * @apiSuccess {String} data 返回数据 + * + * @apiSuccessExample {json} Visual Preview: + * { + "success": true, + "code": 200, + "data": { + "title": "123", + "thumb": "products/1/sda", + "description": "ddd", + "price": 5, + "status": "1" + }, + "message": "请求成功" + } + */ + edit: async function (ctx) { + let result = await ServiceProduct.edit(ctx.params.storeId, ctx.params.productId, ctx.input) + + return Response.output(ctx, result) + }, + + /** + * @api {delete} /api/catering/v1/stores/:storeId/products/:productId 删除product + * @apiGroup product + * @apiPermission store + * @apiVersion 1.0.0 + * @apiSuccessExample {json} Visual Preview: + * { + "success": true, + "code": 200, + "message": "请求成功", + "data": {} + } + */ + delete: async function (ctx) { + let data = { + status: -1 + } + let result = await ServiceProduct.edit(ctx.params.storeId, ctx.params.productId, data) + return Response.output(ctx) + }, + +} diff --git a/app/model/catering/desk.js b/app/model/catering/desk.js new file mode 100644 index 0000000..292aa43 --- /dev/null +++ b/app/model/catering/desk.js @@ -0,0 +1,43 @@ +const DB = require('../../libraries/db') +const ModelBase = require('../../model/base') + +const table = 'mist_desk' + +module.exports = { + add: async function (data) { + let res = await ModelBase.execInsert(table, data) + return res; + }, + + list: async function (storeId) { + + let result = DB.readMysql.select( + '*' + ) + .from(table) + .where('store_id', storeId) + .where('status', '!=', -1) + + return await result + + }, + + first: async function (id) { + + let result = DB.readMysql.first( + '*' + ) + .from(table) + .where('id', id) + + return await result + + }, + + edit: async function (data, where, notWhere = {}) { + let result = await ModelBase.execUpdate(table, data, where, notWhere) + + return await result + } + +} diff --git a/app/model/catering/product.js b/app/model/catering/product.js new file mode 100644 index 0000000..7d28ff4 --- /dev/null +++ b/app/model/catering/product.js @@ -0,0 +1,43 @@ +const DB = require('../../libraries/db') +const ModelBase = require('../../model/base') + +const table = 'mist_product' + +module.exports = { + add: async function (data) { + let res = await ModelBase.execInsert(table, data) + return res; + }, + + list: async function (storeId) { + + let result = DB.readMysql.select( + '*' + ) + .from(table) + .where('store_id', storeId) + .where('status', '!=', -1) + + return await result + + }, + + first: async function (id) { + + let result = DB.readMysql.first( + '*' + ) + .from(table) + .where('id', id) + + return await result + + }, + + edit: async function (data, where, notWhere = {}) { + let result = await ModelBase.execUpdate(table, data, where, notWhere) + + return await result + } + +} diff --git a/app/router/catering/index.js b/app/router/catering/index.js index 96af7a3..9b87b52 100644 --- a/app/router/catering/index.js +++ b/app/router/catering/index.js @@ -6,6 +6,8 @@ const Customer = require('../../controller/catering/customer') const Audit = require('../../controller/catering/audit') const Store = require('../../controller/catering/store') const Category = require('../../controller/catering/category') +const Desk = require('../../controller/catering/desk') +const Product = require('../../controller/catering/product') module.exports = function () { /** @@ -39,6 +41,20 @@ module.exports = function () { Router.get('/api/catering/v1/stores/:storeId/categories/:categoryId', CateringPermissions('store'), Category.detail) Router.put('/api/catering/v1/stores/:storeId/categories/:categoryId', CateringPermissions('store'), Category.edit) Router.delete('/api/catering/v1/stores/:storeId/categories/:categoryId', CateringPermissions('store'), Category.delete) + + // 店铺桌子 + Router.get('/api/catering/v1/stores/:storeId/desks', CateringPermissions('store'), Desk.index) + Router.post('/api/catering/v1/stores/:storeId/desks', CateringPermissions('store'), Desk.add) + Router.get('/api/catering/v1/stores/:storeId/desks/:deskId', CateringPermissions('store'), Desk.detail) + Router.put('/api/catering/v1/stores/:storeId/desks/:deskId', CateringPermissions('store'), Desk.edit) + Router.delete('/api/catering/v1/stores/:storeId/desks/:deskId', CateringPermissions('store'), Desk.delete) + + // 店铺商品 + Router.get('/api/catering/v1/stores/:storeId/products', CateringPermissions('store'), Product.index) + Router.post('/api/catering/v1/stores/:storeId/products', CateringPermissions('store'), Product.add) + Router.get('/api/catering/v1/stores/:storeId/products/:productId', CateringPermissions('store'), Product.detail) + Router.put('/api/catering/v1/stores/:storeId/products/:productId', CateringPermissions('store'), Product.edit) + Router.delete('/api/catering/v1/stores/:storeId/products/:productId', CateringPermissions('store'), Product.delete) return Router; } \ No newline at end of file diff --git a/app/services/catering/desk.js b/app/services/catering/desk.js new file mode 100644 index 0000000..f231119 --- /dev/null +++ b/app/services/catering/desk.js @@ -0,0 +1,60 @@ +const ModelDesk = require('../../model/catering/desk') +const ApiError = require('../../util/api_error') +const Validate = require('request-validate') +const _ = require('underscore') + +module.exports = { + list: async function (storeId) { + let result = await ModelDesk.list(storeId) + return result + }, + + detail: async function (storeId, id) { + let result = await ModelDesk.first(id) + if (_.isEmpty(result)) { + throw new ApiError('common.notExist', 'desk') + } + + if (result.store_id != storeId) { + throw new ApiError('common.notExist', 'desk') + } + + if (result.status == -1) { + throw new ApiError('common.notExist', 'desk') + } + + return result + }, + + add: async function (storeId, data) { + Validate(data, { + name: 'required', + }) + let insertData = { + store_id: storeId, + name: data.name + } + let insertResult = await ModelDesk.add(insertData) + insertData.id = insertResult.insertId + + return insertData + }, + + edit: async function (storeId, id, data) { + let detail = await this.detail(storeId, id) + + let where = { + id: id + } + + let updateData = { + name: _.has(data, 'name') ? data.name : detail.name, + status: _.has(data, 'status') ? data.status : detail.status + } + + let editResult = await ModelDesk.edit(updateData, where) + + return updateData + }, + +} diff --git a/app/services/catering/product.js b/app/services/catering/product.js new file mode 100644 index 0000000..b9e5789 --- /dev/null +++ b/app/services/catering/product.js @@ -0,0 +1,69 @@ +const ModelProduct = require('../../model/catering/product') +const ApiError = require('../../util/api_error') +const Validate = require('request-validate') +const _ = require('underscore') + +module.exports = { + list: async function (storeId) { + let result = await ModelProduct.list(storeId) + return result + }, + + detail: async function (storeId, id) { + let result = await ModelProduct.first(id) + if (_.isEmpty(result)) { + throw new ApiError('common.notExist', 'product') + } + + if (result.store_id != storeId) { + throw new ApiError('common.notExist', 'product') + } + + if (result.status == -1) { + throw new ApiError('common.notExist', 'product') + } + + return result + }, + + add: async function (storeId, data) { + Validate(data, { + title: 'required', + thumb: 'required', + description: 'required', + price: 'required', + }) + let insertData = { + title: data.title, + thumb: data.thumb, + description: data.description, + price: data.price, + store_id: storeId + } + let insertResult = await ModelProduct.add(insertData) + insertData.id = insertResult.insertId + + return insertData + }, + + edit: async function (storeId, id, data) { + let detail = await this.detail(storeId, id) + + let where = { + id: id + } + + let updateData = { + title: _.has(data, 'title') ? data.title : detail.title, + thumb: _.has(data, 'thumb') ? data.thumb : detail.thumb, + description: _.has(data, 'description') ? data.description : detail.description, + price: _.has(data, 'price') ? data.price : detail.price, + status: _.has(data, 'status') ? data.status : detail.status + } + + let editResult = await ModelProduct.edit(updateData, where) + + return updateData + }, + +} diff --git a/artisan.sh b/artisan.sh index 6b64556..bafda55 100644 --- a/artisan.sh +++ b/artisan.sh @@ -74,7 +74,7 @@ module.exports = { } */ detail: async function (ctx) { - let data = await Service${apiClass}.detail() + let data = await Service${apiClass}.detail(ctx.params.${apiName}Id) return Response.output(ctx, data) }, @@ -97,7 +97,7 @@ module.exports = { } */ add: async function (ctx) { - let result = [] + let result = await Service${apiClass}.add(ctx.input) return Response.output(ctx, result) }, @@ -161,8 +161,9 @@ const Validate = require('request-validate') const _ = require('underscore') module.exports = { - list: async function () { - return [] + list: async function (kid) { + let result = await Model${apiClass}.list(kid) + return result }, detail: async function (id) { @@ -170,6 +171,10 @@ module.exports = { if (_.isEmpty(result)) { throw new ApiError('common.notExist', '${apiName}') } + + if (result.status == -1) { + throw new ApiError('common.notExist', '${apiName}') + } return result }, @@ -199,7 +204,7 @@ module.exports = { status: _.has(data, 'status') ? data.status : detail.status } - let editResult = await Model${apiClass}.edit(data, where) + let editResult = await Model${apiClass}.edit(updateData, where) return updateData }, @@ -222,6 +227,19 @@ module.exports = { return res; }, + list: async function (kid) { + + let result = DB.readMysql.select( + '*' + ) + .from(table) + .where('kid', kid) + .where('status', '!=', -1) + + return await result + + }, + first: async function (id) { let result = DB.readMysql.first(