'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 TaskService extends CrudService { constructor(ctx) { super(ctx, 'task'); this.model = this.ctx.model.Task; } // 插入作业 async create(data) { const { code, name, title } = data; assert(code, '科目代码不能为空'); assert(name, '科目名称不能为空'); assert(title, '标题不能为空'); // 插入数据时默认状态为1 const newdata = { ...data, status: '1' }; const entity = await this.model.create(newdata); return await this.fetch({ id: entity.id }); } // 根据id删除 async delete({ id }) { await this.model.findByIdAndDelete(id); return 'deleted'; } // 根据id更新作业信息 async upadate({ id }, data) { const task = await this.model.findById(id); if (task.code) { task.code = data.code; } if (task.name) { task.name = data.name; } if (task.title) { task.title = data.title; } if (task.status) { task.status = data.status; } task.question = data.question; return await task.save(); } // 查询 async query({ skip, limit, ...num }) { return await this.model.find(num).skip(Number(skip)).limit(Number(limit)); } // 查询详情 async show({ id }) { return await this.model.findById(id); } } module.exports = TaskService;