'use strict'; const Service = require('egg').Service; const assert = require('assert'); const moment = require('moment'); class ContentService extends Service { async create({ title, shortTitle, slug, path, annex, content, istop, columnList }) { assert(title, '标题不存在'); assert(shortTitle, '短标题不存在'); assert(slug, '摘要不存在'); assert(path, '缩略图不存在'); assert(istop, '置顶不存在'); assert(content, '内容不存在'); const { Content: model } = this.ctx.model; const createAt = moment().format('x'); try { await model.create({ title, shortTitle, slug, path, annex, content, istop, columnList, createAt }); return { errmsg: '', errcode: 0 }; } catch (error) { throw new Error({ errcode: -2001, errmsg: '添加失败' }); } } async update({ title, shortTitle, slug, path, annex, content, istop, columnList, id }) { assert(id, 'id不存在'); const { Content: model } = this.ctx.model; try { await model.findByIdAndUpdate(id, { title, shortTitle, slug, path, annex, content, istop, columnList }); return { errmsg: '', errcode: 0 }; } catch (error) { throw new Error({ errcode: -2001, errmsg: '修改失败' }); } } async del({ id }) { assert(id, 'id不存在'); const { Content: model } = this.ctx.model; try { await model.findByIdAndDelete(id); return { errmsg: '', errcode: 0 }; } catch (error) { throw new Error({ errcode: -2001, errmsg: '删除失败' }); } } async details({ id }) { assert(id, 'id不存在'); const { Content: model } = this.ctx.model; try { const res = await model.findById(id); return { errmsg: '', errcode: 0, data: res }; } catch (error) { throw new Error({ errcode: -2001, errmsg: '删除失败' }); } } async query({ skip, limit }) { const { Content: model } = this.ctx.model; try { let res; if (skip && limit) { res = await model.find({ content: false }).skip(skip * limit).limit(limit); } else { res = await model.find({ content: false }); } return { errmsg: '', errcode: 0, data: res }; } catch (error) { throw new Error({ errcode: -2001, errmsg: '查询失败' }); } } } module.exports = ContentService;