Kaynağa Gözat

更新query方法fetch方法

reloaded 5 yıl önce
ebeveyn
işleme
960d88289b

+ 4 - 4
app/controller/.demand.js

@@ -2,14 +2,14 @@ module.exports = {
   create: {
     requestBody: [
       "!name",
-      "!field",
-      "!budget",
+      "field",
+      "budget",
       "enddate",
       "problem",
       "condition",
       "type",
-      "uid",
-      "is_display",
+      "!uid",
+      "!is_display",
       "status",
     ],
   },

+ 1 - 1
app/controller/.news.js

@@ -7,7 +7,7 @@ module.exports = {
       "content",
       "publish_time",
       "publish_unit",
-      "publisher",
+      "!publisher",
       "type",
       "url",
       "img_url",

+ 4 - 1
app/controller/demand.js

@@ -7,12 +7,15 @@ const { CrudController } = require('naf-framework-mongoose/lib/controller');
 
 // 部门表管理
 class DemandController extends Controller {
-
   constructor(ctx) {
     super(ctx);
     this.service = this.ctx.service.demand;
   }
 
+  async index() {
+    const res = await this.service.query(this.ctx.query);
+    this.ctx.ok({ ...res });
+  }
 }
 
 module.exports = CrudController(DemandController, meta);

+ 4 - 4
app/model/demand.js

@@ -5,14 +5,14 @@ const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
 // 需求表
 const DemandSchema = {
   name: { type: String, required: true, maxLength: 200 }, // 需求技术名称
-  field: { type: String, required: true, maxLength: 200 }, // 所属领域
-  budget: { type: String, required: true, maxLength: 200 }, // 拟投入预算 (万)
+  field: { type: String, required: false, maxLength: 200 }, // 所属领域
+  budget: { type: String, required: false, maxLength: 200 }, // 拟投入预算 (万)
   enddate: { type: String, required: false, maxLength: 200 }, // 需求截止日期
   problem: { type: String, required: false, maxLength: 500 }, // 难题或瓶颈问题
   condition: { type: String, required: false, maxLength: 500 }, // 企业解决技术需求已具备的条件
   type: { type: String, required: false, maxLength: 200 }, // 合作方式
-  uid: { type: String, required: false, maxLength: 200 }, // 发布人用户id
-  is_display: { type: String, required: false, maxLength: 200 }, // 是否在网页上显示,0-显示,1-不显示
+  uid: { type: String, required: true, maxLength: 200 }, // 发布人用户id
+  is_display: { type: String, required: true, maxLength: 200 }, // 是否在网页上显示,0-显示,1-不显示
   status: { type: String, required: false, maxLength: 200, default: '0' }, // 状态,0-草稿,1-未审核,2-已审核
 };
 

+ 1 - 1
app/model/news.js

@@ -10,7 +10,7 @@ const NewsSchema = {
   content: { type: String, required: false }, // 内容
   publish_time: { type: String, required: false, maxLength: 200 }, // 发布时间
   publish_unit: { type: String, required: false, maxLength: 200 }, // 发布单位
-  publisher: { type: String, required: false, maxLength: 200 }, // 发布人
+  publisher: { type: String, required: true, maxLength: 200 }, // 发布人
   type: { type: String, required: false, maxLength: 200 }, // 0-自有,1-外链
   url: { type: String, required: false, maxLength: 200 }, // 链接地址
   img_url: { type: String, required: false, maxLength: 200 }, // 图片路径

+ 15 - 4
app/service/comment.js

@@ -9,23 +9,34 @@ const { BusinessError, ErrorCode } = require('naf-core').Error;
 
 class CommentService extends CrudService {
   constructor(ctx) {
-    super(ctx, 'comment');
+    super(ctx, "comment");
     this.model = this.ctx.model.Comment;
     this.umodel = this.ctx.model.User;
   }
 
   async query({ skip, limit, ...info }) {
     const total = await (await this.model.find(info)).length;
-    const comments = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
+    const comments = await this.model
+      .find(info)
+      .skip(Number(skip))
+      .limit(Number(limit));
     const newdatas = [];
-    for (const comment of comments) {
+    for (let comment of comments) {
+      comment = JSON.parse(JSON.stringify(comment));
       const user = await this.umodel.findById(comment.uid);
-      comment.uname = user.data.name;
+      comment.uname = user.name;
       newdatas.push(comment);
     }
     return { data: newdatas, total };
   }
 
+  async fetch({ id }) {
+    let comment = await this.model.findById(id);
+    comment = JSON.parse(JSON.stringify(comment));
+    const user = await this.umodel.findById(comment.uid);
+    comment.uname = user.name;
+    return comment;
+  }
 }
 
 module.exports = CommentService;

+ 24 - 0
app/service/demand.js

@@ -11,8 +11,32 @@ class DemandService extends CrudService {
   constructor(ctx) {
     super(ctx, 'demand');
     this.model = this.ctx.model.Demand;
+    this.umodel = this.ctx.model.User;
   }
 
+  async query({ skip, limit, ...info }) {
+    const total = await (await this.model.find(info)).length;
+    const demands = await this.model
+      .find(info)
+      .skip(Number(skip))
+      .limit(Number(limit));
+    const newdatas = [];
+    for (let demand of demands) {
+      demand = JSON.parse(JSON.stringify(demand));
+      const user = await this.umodel.findById(demand.uid);
+      demand.uname = user.name;
+      newdatas.push(demand);
+    }
+    return { data: newdatas, total };
+  }
+
+  async fetch({ id }) {
+    let demand = await this.model.findById(id);
+    demand = JSON.parse(JSON.stringify(demand));
+    const user = await this.umodel.findById(demand.uid);
+    demand.uname = user.name;
+    return demand;
+  }
 }
 
 module.exports = DemandService;