commons.js 1.17 KB
/*
 * @Author: huyue
 * @Date: 2021-09-02 14:15:48
 * @Last Modified by: huyue
 * @Last Modified time: 2021-09-02 14:43:02
 */
// app/service/Commons.js

const Service = require("egg").Service;

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

  /** 搜索列表 */
  async findAll({ project_id }) {
    return await this.DI.findAll({
      where: { project_id: project_id },
      attributes: [
        "id",
        "user_id",
        "project_id",
        "content",
        "created_at",
        "updated_at",
      ],
    });
  }

  /** 新增评论 */
  async create(params) {
    const now = Date.now();
    const p = await this.DI.create({
      //目前还没有用户,所以都是默认值 ’1‘
      // user_id:'',
      project_id: params.project_id,
      content: params.content,
      created_at: now,
      updated_at: now,
    });
    // console.log(p);
    return {
      id: p.id,
    };
  }
}

module.exports = CommonsService;