1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- 'use strict';
- const assert = require('assert');
- const Service = require('egg').Service;
- class CatalogueService extends Service {
- constructor(ctx) {
- super(ctx);
- this.model = this.ctx.model.Catalogue;
- }
- async create({ content, name, describe, status, url, date, column, bind }) {
- assert(name, '名称不存在');
- assert(date, '发布时间不存在');
- assert(status, '状态不存在');
- assert(bind, '绑定ID不存在');
- try {
- const res = await this.model.create({ content, name, describe, status, url, date, column, bind });
- return { errcode: 0, errmsg: 'ok', data: res };
- } catch (error) {
- throw error;
- }
- }
- async update({ id, content, name, describe, status, url, date, column, bind }) {
- assert(id, 'id不存在');
- try {
- await this.model.updateOne({ _id: id }, { content, name, describe, status, url, date, column, bind });
- return { errcode: 0, errmsg: 'ok', data: '' };
- } catch (error) {
- throw error;
- }
- }
- async delete({ id }) {
- assert(id, 'id不存在');
- try {
- await this.model.deleteOne({ _id: id });
- return { errcode: 0, errmsg: 'ok', data: '' };
- } catch (error) {
- throw error;
- }
- }
- async query({ skip, limit, name, status, date, column, bind }) {
- const filter = {};
- const arr = { name, status, date, column, bind };
- for (const e in arr) {
- const data = `{ "${e}": { "$regex": "${arr[e]}" } }`;
- if (arr[e]) {
- filter.$or = [];
- filter.$or.push(JSON.parse(data));
- }
- }
- try {
- const total = await this.model.find({ ...filter });
- let res;
- if (skip && limit) {
- res = await this.model.find({ ...filter }, { content: false, url: false }).skip(Number(skip) * Number(limit)).limit(Number(limit));
- } else {
- res = await this.model.find({ ...filter }, { content: false, url: false });
- }
- return { errcode: 0, errmsg: 'ok', data: res, total: total.length };
- } catch (error) {
- throw error;
- }
- }
- async fetch({ id }) {
- assert(id, 'id不存在');
- try {
- const res = await this.model.findOne({ _id: id });
- return { errcode: 0, errmsg: 'ok', data: res };
- } catch (error) {
- throw error;
- }
- }
- }
- module.exports = CatalogueService;
|