text: 'use strict';
  
  const Service = require('egg').Service;
  
  class User extends Service {
    async list({ offset = 0, limit = 10, order_by = 'created_at', order = 'ASC' }) {
      return this.ctx.model.User.findAndCountAll({
        offset,
        limit,
        order: [[ order_by, order.toUpperCase() ]],
      });
    }
  
    async find(id) {
      const user = await this.ctx.model.User.findById(id);
      if (!user) {
        this.ctx.throw(404, 'user not found');
      }
      return user;
    }
  
    async create(user) {
      return this.ctx.model.User.create(user);
    }
  
    async update({ id, updates }) {
      const user = await this.ctx.model.User.findById(id);
      if (!user) {
        this.ctx.throw(404, 'user not found');
      }
      return user.update(updates);
    }
  
    async del(id) {
      const user = await this.ctx.model.User.findById(id);
      if (!user) {
        this.ctx.throw(404, 'user not found');
      }
      return user.destroy();
    }
  }
  
  module.exports = User;
  