docClassify.js 1.45 KB
/*
 * @Author: huyue
 * @Date: 2021-09-09 15:45:14
 * @Last Modified by: huyue
 * @Last Modified time: 2021-09-10 11:20:31
 */
// app/service/Classify.js

const Service = require("egg").Service;
class ClassifyService extends Service {
  constructor(ctx) {
    super(ctx);
    this.helper = ctx.helper;
    this._reg_models_alias_();
  }
  /**
   * 绑定model 在 this 上的 别名
   */
  _reg_models_alias_() {
    const { DocClassify } = this.app.model;
    this.DI = DocClassify;
  }

  /** 搜索分类列表 */
  async findAll({ pager = {} }) {
    return await this.DI.findAll({
      attributes: [
        "id",
        "title",
        "description"
      ],
    });
  }

  /** 新增分类 */
  async create(params) {
    const now = Date.now();
    const p = await this.DI.create({
      title: params.title,
      description: params.description,
      created_at: now,
      updated_at: now,
    });
    return {
      id: p.id,
    };
  }
  async update(params) {
    const now = Date.now();
    const p = await this.DI.update(
      {
        title: params.title,
        description: params.description,
        updated_at: now
      },
      {
        where: {
          id: params.id,
        },
      }
    );
    return {
      id: p,
    };
  }

  async delete(params) {
    const p = await this.DI.destroy(
      {
        where: {
          id: params.id,
        },
      }
    );
    return {
      id: p,
    };
  }
}

module.exports = ClassifyService;