Browse Source

友情链接表

reloaded 4 years ago
parent
commit
890978f352
5 changed files with 95 additions and 0 deletions
  1. 35 0
      app/controller/.link.js
  2. 18 0
      app/controller/link.js
  3. 20 0
      app/model/link.js
  4. 4 0
      app/router.js
  5. 18 0
      app/service/link.js

+ 35 - 0
app/controller/.link.js

@@ -0,0 +1,35 @@
+module.exports = {
+  create: {
+    requestBody: ["!name", "!url", "img_url"],
+  },
+  destroy: {
+    params: ["!id"],
+    service: "delete",
+  },
+  update: {
+    params: ["!id"],
+    requestBody: ["name", "url", "img_url"],
+  },
+  show: {
+    parameters: {
+      params: ["!id"],
+    },
+    service: "fetch",
+  },
+  index: {
+    parameters: {
+      query: {
+        name: "name",
+        url: "url",
+        img_url: "img_url",
+      },
+    },
+    service: "query",
+    options: {
+      query: ["skip", "limit"],
+      sort: ["meta.createdAt"],
+      desc: true,
+      count: true,
+    },
+  },
+};

+ 18 - 0
app/controller/link.js

@@ -0,0 +1,18 @@
+'use strict';
+
+const _ = require('lodash');
+const meta = require('./.link.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 友情链接表管理
+class LinkController extends Controller {
+
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.link;
+  }
+
+}
+
+module.exports = CrudController(LinkController, meta);

+ 20 - 0
app/model/link.js

@@ -0,0 +1,20 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+
+// 友情链接表
+const LinkSchema = {
+  name: { type: String, required: false, maxLength: 200 }, // 链接名称
+  url: { type: String, required: true, maxLength: 200 }, // 链接地址
+  img_url: { type: String, required: false, maxLength: 200 }, // 图片路径
+};
+
+
+const schema = new Schema(LinkSchema, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Link', schema, 'link');
+};

+ 4 - 0
app/router.js

@@ -44,4 +44,8 @@ module.exports = app => {
   // 需求表设置路由
   router.resources('demand', '/api/count/demand', controller.demand); // index、create、show、destroy
   router.post('demand', '/api/count/demand/update/:id', controller.demand.update);
+
+  // 需求表设置路由
+  router.resources('link', '/api/count/link', controller.link); // index、create、show、destroy
+  router.post('link', '/api/count/link/update/:id', controller.link.update);
 };

+ 18 - 0
app/service/link.js

@@ -0,0 +1,18 @@
+'use strict';
+
+
+const assert = require('assert');
+const _ = require('lodash');
+const { ObjectId } = require('mongoose').Types;
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+
+class LinkService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'link');
+    this.model = this.ctx.model.Link;
+  }
+
+}
+
+module.exports = LinkService;