123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- 'use strict';
- const assert = require('assert');
- const _ = require('lodash');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const { isNullOrUndefined, trimData } = require('naf-core').Util;
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- class NewsService extends CrudService {
- constructor(ctx) {
- super(ctx);
- this.model = this.ctx.model.News;
- }
- async create({ site }, { title, pic, content, type, parent_id, parent, publish_time,attachment, is_use }) {
- // 检查数据
- assert(_.isString(site), 'site不能为空');
- assert(_.isString(title), 'title不能为空');
- assert(!pic || _.isString(pic), 'pic必须为字符串');
- assert(!content || _.isString(content), 'content必须为字符串');
- assert(!type || _.isString(type), 'type必须为字符串');
- assert(!parent_id || _.isString(parent_id), 'parent_id必须为字符串');
- assert(!parent || _.isString(parent), 'parent必须为字符串');
- assert(!publish_time || _.isString(publish_time), 'publish_time必须为字符串');
- assert(!attachment || _.isArray(attachment), 'attachment必须为数组');
- assert(!is_use || _.isString(is_use), 'is_use必须为字符串');
- // TODO: 检查用户信息
- const userid = this.ctx.userid;
- if (!_.isString(userid)) throw new BusinessError(ErrorCode.NOT_LOGIN);
- // TODO:保存数据
- const data = {
- site, title, pic, content, type, parent_id, parent, publish_time, attachment, is_use,
- meta: { createdBy: userid },
- };
- const res = await this.model.create(data);
- return res;
- }
- async update({ id }, payload) {
- // 检查数据
- const { title, pic, content, type, parent_id, parent, publish_time,attachment, is_use } = payload;
- assert(id, 'id不能为空');
- assert(!title || _.isString(title), 'title必须为字符串');
- assert(!pic || _.isString(pic), 'pic必须为字符串');
- assert(!content || _.isString(content), 'content必须为字符串');
- assert(!type || _.isString(type), 'type必须为字符串');
- assert(!parent_id || _.isString(parent_id), 'parent_id必须为字符串');
- assert(!parent || _.isString(parent), 'parent必须为字符串');
- assert(!publish_time || _.isString(publish_time), 'publish_time必须为字符串');
- assert(!attachment || _.isArray(attachment), 'attachment必须为数组');
- assert(!is_use || _.isString(is_use), 'is_use必须为字符串');
- // TODO: 检查用户信息
- const userid = this.ctx.userid;
- if (!_.isString(userid)) throw new BusinessError(ErrorCode.NOT_LOGIN);
- // TODO:检查数据是否存在
- const doc = await this.model.findById(id).exec();
- if (isNullOrUndefined(doc)) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
- }
- // TODO:保存数据
- const data = trimData(payload);
- await this.model.findByIdAndUpdate(doc.id, { ...data, 'meta.updatedBy': userid }, { new: true }).exec();
- const res = this.model.findById(id, '+content').exec();
- return res;
- }
- async status({ id, state }) {
- // TODO: 检查数据状态
- const doc = await this.model.findById(id).exec();
- if (!doc) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
- }
- doc.meta.state = state;
- return await doc.save();
- }
- async delete({ id }) {
- return await this.status({ id, state: 1 });
- }
- async restore({ id }) {
- return await this.status({ id, state: 0 });
- }
- }
- module.exports = NewsService;
|