lrf402788946 %!s(int64=4) %!d(string=hai) anos
pai
achega
782537c2eb

+ 49 - 0
app/controller/.answer.js

@@ -0,0 +1,49 @@
+module.exports = {
+  create: {
+    requestBody: ["questionnaire_id", "answer", "user_id"],
+  },
+  destroy: {
+    params: ["!id"],
+    service: "delete",
+  },
+  update: {
+    params: ["!id"],
+    requestBody: ["questionnaire_id", "answer", "user_id"],
+  },
+  show: {
+    parameters: {
+      params: ["!id"],
+    },
+    service: "fetch",
+  },
+  index: {
+    parameters: {
+      query: {
+        questionnaire_id: "questionnaire_id",
+        answer: "answer",
+        user_id: "user_id",
+        "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,
+    },
+  },
+  getAnswer: {
+    parameters: {
+      query: {
+        questionnaire_id: "questionnaire_id",
+        user_id: "user_id",
+      },
+    },
+    service: "findOne",
+  },
+};

+ 2 - 2
app/controller/.questionnaire.js

@@ -1,6 +1,6 @@
 module.exports = {
   create: {
-    requestBody: ["title", "user_id", "questions"],
+    requestBody: ["title", "user_id", "questions", "brief"],
   },
   destroy: {
     params: ["!id"],
@@ -8,7 +8,7 @@ module.exports = {
   },
   update: {
     params: ["!id"],
-    requestBody: ["title", "user_id", "questions"],
+    requestBody: ["title", "user_id", "questions", "brief"],
   },
   show: {
     parameters: {

+ 13 - 0
app/controller/answer.js

@@ -0,0 +1,13 @@
+'use strict';
+const meta = require('./.answer.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 问卷回答
+class AnswerController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.answer;
+  }
+}
+module.exports = CrudController(AnswerController, meta);

+ 23 - 0
app/model/answer.js

@@ -0,0 +1,23 @@
+'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 answer = {
+  questionnaire_id: { type: ObjectId }, // 问卷id
+  answer: { type: Array }, // 答案
+  user_id: { type: ObjectId },
+  remark: { type: String, maxLength: 200 },
+  create_time: { type: String, default: moment(new Date()).format('YYYY-MM-DD HH:mm:ss') },
+};
+const schema = new Schema(answer, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ questionnaire_id: 1 });
+schema.index({ user_id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.plugin(metaPlugin);
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Answer', schema, 'answer');
+};

+ 1 - 1
app/model/question.js

@@ -6,7 +6,7 @@ const { ObjectId } = require('mongoose').Types;
 // 问卷题目表
 const question = {
   title: { type: String },
-  type: { type: String }, // 类型:0=>简答;1=>单选;2=>多选
+  type: { type: String }, // 类型:0=>单选;1=>多选;2=>简答
   selects: { type: Array }, // 选项
   user_id: { type: ObjectId }, // 所属用户
   remark: { type: String, maxLength: 200 },

+ 1 - 0
app/model/questionnaire.js

@@ -8,6 +8,7 @@ const questionnaire = {
   title: { type: String },
   user_id: { type: ObjectId },
   questions: { type: Array, select: false },
+  brief: { type: String }, // 简介
   remark: { type: String, maxLength: 200 },
   create_time: { type: String, default: moment(new Date()).format('YYYY-MM-DD HH:mm:ss') },
 };

+ 1 - 0
app/router.js

@@ -8,4 +8,5 @@ module.exports = app => {
   router.get('/', controller.home.index);
   require('./router/question')(app); // 问题
   require('./router/questionnaire')(app); // 问卷
+  require('./router/answer')(app); // 回答
 };

+ 11 - 0
app/router/answer.js

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

+ 22 - 0
app/service/answer.js

@@ -0,0 +1,22 @@
+'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 AnswerService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'answer');
+    this.model = this.ctx.model.Answer;
+  }
+
+  async findOne({ questionnaire_id, user_id }) {
+    assert(questionnaire_id, '缺少问卷信息');
+    assert(user_id, '缺少填写人信息');
+    const res = await this.model.findOne({ questionnaire_id, user_id });
+    return res;
+  }
+}
+
+module.exports = AnswerService;

+ 9 - 1
app/service/question.js

@@ -3,6 +3,7 @@ const { CrudService } = require('naf-framework-mongoose/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const _ = require('lodash');
 const assert = require('assert');
+const { ObjectId } = require('mongoose').Types;
 
 // 问题
 class QuestionService extends CrudService {
@@ -28,7 +29,14 @@ class QuestionService extends CrudService {
 
   async getQuestions(ids) {
     const res = await this.model.find({ _id: ids });
-    return res;
+    // 按选择顺序排序
+    const arr = [];
+    for (let i = 0; i < ids.length; i++) {
+      const id = ids[i];
+      const r = res.find(f => ObjectId(id).equals(f._id));
+      if (r) arr.push(r);
+    }
+    return arr;
   }
 }
 

+ 3 - 2
app/service/questionnaire.js

@@ -14,8 +14,9 @@ class QuestionnaireService extends CrudService {
   async fetch({ id }) {
     const data = await this.model.findById(id, '+questions');
     if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定问卷!');
-    const ids = data.questions;
-    const questions = await this.question.getQuestions(ids);
+    const obj = data.questions;
+    if (obj.every(e => _.isObject(e))) return data;
+    const questions = await this.question.getQuestions(obj);
     data.questions = questions;
     return data;
   }