lrf402788946 4 tahun lalu
induk
melakukan
1d1b66e533
6 mengubah file dengan 125 tambahan dan 0 penghapusan
  1. 57 0
      app/controller/.topic.js
  2. 13 0
      app/controller/topic.js
  3. 27 0
      app/model/topic.js
  4. 1 0
      app/router.js
  5. 12 0
      app/router/topic.js
  6. 15 0
      app/service/topic.js

+ 57 - 0
app/controller/.topic.js

@@ -0,0 +1,57 @@
+module.exports = {
+  create: {
+    requestBody: [
+      "user_id",
+      "content",
+      "origin",
+      "renew_time",
+      "type",
+      "imgUrl",
+      "fileUrl",
+      "website",
+      "remark",
+    ],
+  },
+  destroy: {
+    params: ["!id"],
+    service: "delete",
+  },
+  update: {
+    params: ["!id"],
+    requestBody: [
+      "user_id",
+      "content",
+      "origin",
+      "renew_time",
+      "type",
+      "imgUrl",
+      "fileUrl",
+      "website",
+      "remark",
+    ],
+  },
+  show: {
+    parameters: {
+      params: ["!id"],
+    },
+    service: "fetch",
+  },
+  index: {
+    parameters: {
+      query: {
+        "create_time@start": "create_time@start",
+        "create_time@end": "create_time@end",
+      },
+      // options: {
+      //   "meta.state": 0 // 默认条件
+      // },
+    },
+    service: "query",
+    options: {
+      query: ["skip", "limit"],
+      sort: ["meta.createdAt"],
+      desc: true,
+      count: true,
+    },
+  },
+};

+ 13 - 0
app/controller/topic.js

@@ -0,0 +1,13 @@
+'use strict';
+const meta = require('./.topic.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 社区话题
+class TopicController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.topic;
+  }
+}
+module.exports = CrudController(TopicController, meta);

+ 27 - 0
app/model/topic.js

@@ -0,0 +1,27 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const moment = require('moment');
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+const { ObjectId } = require('mongoose').Types;
+// 社区话题表
+const topic = {
+  user_id: { type: ObjectId },
+  content: { type: String }, // 内容
+  origin: { type: String }, // 来源
+  renew_time: { type: String }, // 更新时间
+  type: { type: String }, // 类型:0=>图文;1=>视频
+  imgUrl: { type: Array }, // 图片
+  fileUrl: { type: Array }, // 视频
+  website: { type: String }, // 网址
+  read: { type: Number, default: 0 }, // 阅读数
+  remark: { type: String, maxLength: 200 },
+  create_time: { type: String, default: moment(new Date()).format('YYYY-MM-DD HH:mm:ss') },
+};
+const schema = new Schema(topic, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.plugin(metaPlugin);
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Topic', schema, 'topic');
+};

+ 1 - 0
app/router.js

@@ -10,4 +10,5 @@ module.exports = app => {
   router.post('user', `${profix}login`, controller.user.login);
   router.post('user', `${profix}init`, controller.user.init);
   require('./router/refute')(app); // 文章/辟谣
+  require('./router/topic')(app); // 社区话题
 };

+ 12 - 0
app/router/topic.js

@@ -0,0 +1,12 @@
+'use strict';
+
+
+module.exports = app => {
+  const { router, controller } = app;
+  const profix = '/api/article/';
+  const target = 'topic';
+  const readMiddleware = app.middleware.read(target);
+  router.get(target, `${profix}${target}/:id`, readMiddleware, controller[target].show);
+  router.resources(target, `${profix}${target}`, controller[target]); // index、create、show、destroy
+  router.post(target, `${profix}${target}/update/:id`, controller[target].update);
+};

+ 15 - 0
app/service/topic.js

@@ -0,0 +1,15 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const _ = require('lodash');
+const assert = require('assert');
+
+// 社区话题
+class TopicService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'topic');
+    this.model = this.ctx.model.Topic;
+  }
+}
+
+module.exports = TopicService;