dygapp 7 年 前
コミット
3737c83a96
6 ファイル変更110 行追加6 行削除
  1. 42 0
      app/controller/README.md
  2. 1 1
      app/controller/home.js
  3. 18 0
      app/model/README.md
  4. 45 0
      app/service/README.md
  5. 0 1
      config/config.default.js
  6. 4 4
      package.json

+ 42 - 0
app/controller/README.md

@@ -0,0 +1,42 @@
+#Crud控制器
+```
+'use strict';
+
+const Controller = require('egg').Controller;
+const meta = require('./category.json');
+const { CrudController } = require('naf-framework-mongoose').controller;
+
+class CategoryController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.category;
+  }
+}
+
+module.exports = CrudController(CategoryController, meta);
+```
+#Crud描述文档
+```
+{
+  "create": {
+    "requestBody": ["code","name","order"]
+  },
+  "delete": {
+    "query": ["id"]
+  },
+  "update": {
+    "query": ["id"],
+    "requestBody": ["name","order"]
+  },
+  "list": {
+    "parameters": {},
+    "service": "query",
+    "options": {
+      "sort": ["order", "code"]
+    }
+},
+  "fetch": {
+    "query": ["id"]
+  }
+}
+```

+ 1 - 1
app/controller/home.js

@@ -4,7 +4,7 @@ const Controller = require('egg').Controller;
 
 class HomeController extends Controller {
   async index() {
-    this.ctx.body = 'smart jobs platform user service';
+    this.ctx.body = '请通过服务接口进行调用';
   }
 }
 

+ 18 - 0
app/model/README.md

@@ -0,0 +1,18 @@
+#数据模型
+```
+'use strict';
+const Schema = require('mongoose').Schema;
+
+const SchemaDefine = {
+  code: { type: String, required: true, maxLength: 64 },
+  name: { type: String, required: true, maxLength: 128 },
+  order: Number,
+};
+const schema = new Schema(SchemaDefine);
+schema.index({ code: 1 }, { unique: true });
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('CodeCategory', schema, 'naf_code_category');
+};
+```

+ 45 - 0
app/service/README.md

@@ -0,0 +1,45 @@
+#Crud服务
+```
+'use strict';
+
+const ObjectID = require('mongodb').ObjectID;
+const assert = require('assert');
+const util = require('core-util-is');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const service = require('naf-framework-mongoose').service;
+const { CrudService } = service;
+
+class CategoryService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'naf_code_category');
+    this.model = ctx.model.Category;
+    this.mItems = ctx.model.Items;
+  }
+
+  async delete({ id }) {
+    assert(id);
+
+    // TODO:检查数据是否存在
+    const entity = await this._findOne({ _id: ObjectID(id) });
+    if (util.isNullOrUndefined(entity)) throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
+
+    // TODO: 检查是否包含字典项数据
+    const count = await this._count({ category: entity.code }, this.mItems);
+    if (count > 0) {
+      throw new BusinessError(ErrorCode.SERVICE_FAULT, '存在字典项数据,不能删除');
+    }
+
+    await this._remove({ _id: ObjectID(id) });
+    return 'deleted';
+  }
+
+  // async query({ skip, limit, order } = {}, data = {}) {
+  //   // const rs = await this._find(trimData(data), null, trimData({ skip, limit, sort: order && { [order]: 1 } }));
+  //   const rs = await this.model.find({}, null, {}).exec();
+  //   return rs;
+  // }
+
+}
+
+module.exports = CategoryService;
+```

+ 0 - 1
config/config.default.js

@@ -17,7 +17,6 @@ module.exports = appInfo => {
 
   // mongoose config
   config.mongoose = {
-    // url: 'mongodb://root:Ziyouyanfa%23%40!@localhost:27017/naf?authSource=admin',
     url: 'mongodb://localhost:27017/platform',
     options: {
       useMongoClient: true,

+ 4 - 4
package.json

@@ -1,7 +1,7 @@
 {
-  "name": "platform-user",
+  "name": "service-template",
   "version": "1.0.0",
-  "description": "smart jobs platform user service",
+  "description": "smart jobs platform service template",
   "private": true,
   "egg": {
     "framework": "naf-framework-mongoose"
@@ -26,8 +26,8 @@
     "node": ">=8.9.0"
   },
   "scripts": {
-    "start": "egg-scripts start --daemon --title=paltform-user",
-    "stop": "egg-scripts stop --title=paltform-user",
+    "start": "egg-scripts start --daemon --title=service-tmpl",
+    "stop": "egg-scripts stop --title=service-tmpl",
     "dev": "egg-bin dev",
     "debug": "egg-bin debug",
     "test": "npm run lint -- --fix && npm run test-local",