|
@@ -4,27 +4,27 @@ 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 }) {
|
|
|
+ async create({ title, shortTitle, slug, path, annex, content }) {
|
|
|
assert(title, '标题不存在');
|
|
|
assert(shortTitle, '短标题不存在');
|
|
|
assert(slug, '摘要不存在');
|
|
|
- assert(path, '缩略图不存在');
|
|
|
- assert(istop, '置顶不存在');
|
|
|
+ assert(path, '轮播图不存在');
|
|
|
assert(content, '内容不存在');
|
|
|
- const { Content: model } = this.ctx.model;
|
|
|
+ const { Banner: model } = this.ctx.model;
|
|
|
const createAt = moment().format('x');
|
|
|
try {
|
|
|
- await model.create({ title, shortTitle, slug, path, annex, content, istop, createAt });
|
|
|
+ await model.create({ title, shortTitle, slug, path, annex, content, createAt });
|
|
|
return { errmsg: '', errcode: 0 };
|
|
|
} catch (error) {
|
|
|
+ console.log(error);
|
|
|
throw new Error({ errcode: -2001, errmsg: '添加失败' });
|
|
|
}
|
|
|
}
|
|
|
- async update({ title, shortTitle, slug, path, annex, content, istop, id }) {
|
|
|
- assert(id, 'id不存在');
|
|
|
- const { Content: model } = this.ctx.model;
|
|
|
+ async update({ title, shortTitle, slug, path, annex, content, istop, _id }) {
|
|
|
+ assert(_id, 'id不存在');
|
|
|
+ const { Banner: model } = this.ctx.model;
|
|
|
try {
|
|
|
- await model.findByIdAndUpdate(id, { title, shortTitle, slug, path, annex, content, istop });
|
|
|
+ await model.findById(_id).update({ title, shortTitle, slug, path, annex, content, istop });
|
|
|
return { errmsg: '', errcode: 0 };
|
|
|
} catch (error) {
|
|
|
throw new Error({ errcode: -2001, errmsg: '修改失败' });
|
|
@@ -32,9 +32,9 @@ class ContentService extends Service {
|
|
|
}
|
|
|
async del({ id }) {
|
|
|
assert(id, 'id不存在');
|
|
|
- const { Content: model } = this.ctx.model;
|
|
|
+ const { Banner: model } = this.ctx.model;
|
|
|
try {
|
|
|
- await model.findByIdAndDelete(id);
|
|
|
+ await model.findById(id).remove();
|
|
|
return { errmsg: '', errcode: 0 };
|
|
|
} catch (error) {
|
|
|
throw new Error({ errcode: -2001, errmsg: '删除失败' });
|
|
@@ -42,7 +42,7 @@ class ContentService extends Service {
|
|
|
}
|
|
|
async details({ id }) {
|
|
|
assert(id, 'id不存在');
|
|
|
- const { Content: model } = this.ctx.model;
|
|
|
+ const { Banner: model } = this.ctx.model;
|
|
|
try {
|
|
|
const res = await model.findById(id);
|
|
|
return { errmsg: '', errcode: 0, data: res };
|
|
@@ -50,16 +50,19 @@ class ContentService extends Service {
|
|
|
throw new Error({ errcode: -2001, errmsg: '查询失败' });
|
|
|
}
|
|
|
}
|
|
|
- async query({ skip, limit }) {
|
|
|
- const { Content: model } = this.ctx.model;
|
|
|
+ async query({ skip, limit, title }) {
|
|
|
+ const { Banner: model } = this.ctx.model;
|
|
|
+ const filter = {};
|
|
|
+ if (title) filter.title = title;
|
|
|
+ const total = await model.find();
|
|
|
try {
|
|
|
let res;
|
|
|
if (skip && limit) {
|
|
|
- res = await model.find({ content: false }).skip(skip * limit).limit(limit);
|
|
|
+ res = await model.find({ ...filter }, { content: false }).skip(Number(skip) * Number(limit)).limit(Number(limit));
|
|
|
} else {
|
|
|
- res = await model.find({ content: false });
|
|
|
+ res = await model.find({ ...filter }, { content: false });
|
|
|
}
|
|
|
- return { errmsg: '', errcode: 0, data: res };
|
|
|
+ return { errmsg: '', errcode: 0, data: res, total: total.length };
|
|
|
} catch (error) {
|
|
|
throw new Error({ errcode: -2001, errmsg: '查询失败' });
|
|
|
}
|