Source: api/category.js

'use strict';

const _ = require('lodash');

const ApiBase = require('./apiBase');
const Entities = require('../entities');

/**
 * @classdesc Singleton class that handle every Category requests.
 * @class
 * @hideconstructor
 * @extends ApiBase
 * @author Bruno Morceli - pirofagista@gmail.com
 * @memberof API
 */
class CategoryAPI extends ApiBase {

  /**
   * Get a Category by ID.
   * @method
   * @async
   * @param {string} categoryId Category UUID.
   * @returns {Promise}
   * @example
   * CannedIO.api.category
   * .get(categoryId)
   * .then(cat => console.log('success:', cat.toJSON()))
   * .catch(error => console.log('something is wrong:', error));
   */
  get(categoryId) {
    return new Promise((resolve, reject) => {

      this.request('get', '/api/categories/get', { id: categoryId })
      .then(category => resolve(new Entities.category().setData(category)))
      .catch(error => reject(error));

    });
  }

  /**
   * Get all active categories.
   * @method
   * @async
   * @returns {Promise}
   * @example
   * CannedIO.api.category
   * .list()
   * .then(list => console.log('I got', list.length, 'categories.'))
   * .catch(error => console.log('something is wrong:', error));
   */
  list() {
    return new Promise((resolve, reject) => {

      this.request('get', '/api/categories/list')
      .then(categories => 
        resolve(categories.map(i => new Entities.category().setData(i)))
      )
      .catch(error => reject(error));

    })
  }

  /**
   * Create a Category.
   * @method
   * @async
   * @param {Entity.CategoryEntity} categoryObj A Category entity instance.
   * @lin
   * @returns {Promise}
   * @example
   * const catData = new CannedIO.entity.category(
   *   null, // id
   *   'my new category', // label
   *   '#ff00ff' // color
   * );
   * 
   * CannedIO.api.category
   * .create(catData)
   * .then(newCat => console.log('success:', newCat.toJSON()))
   * .catch(error => console.log('something is wrong:', error));
   */
  create(categoryObj) {
    if (!(categoryObj instanceof Entities.category)) { 
      return Promise.reject('Param "categoryObj" must be instance of "Category" class.');
    }

    return new Promise((resolve, reject) => {

      const data = _.omit(categoryObj.toJSON(), ['id', 'createdAt', 'updatedAt']);
      this.request('post', '/api/categories/create', data)
      .then(category => resolve(new Entities.category().setData(category)))
      .catch(error => reject(error));

    });
  }

  /**
   * Update a Category.
   * @method
   * @async
   * @param {Entity.CategoryEntity} categoryObj Category entity instance.
   * @returns {Promise}
   * @example
   * // update an existing category (you can use a new instance as well).
   * myCat.label = 'my updated category';
   * myCat.color = '#00ff00';
   * 
   * CannedIO.api.category
   * .update(myCat)
   * .then(updatedCat => console.log('success:', updatedCat.toJSON()))
   * .catch(error => console.log('something is wrong:', error));
   */
  update(categoryObj) {
    if (!(categoryObj instanceof Entities.category)) { 
      return Promise.reject('Param "categoryObj" must be instance of "Category" class.');
    }

    return new Promise((resolve, reject) => {

      const data = _.omit(categoryObj.toJSON(),  ['createdAt', 'updatedAt']);
      this.request('put', '/api/categories/update', data)
      .then(category => resolve(new Entities.category().setData(category)))
      .catch(error => reject(error));

    });
  }

  /**
   * Remove a category by ID.
   * @method
   * @async
   * @param {string} categoryId category UUID.
   * @returns {Promise}
   * @example
   * CannedIO.api.category
   * .remove(categoryId)
   * .then(cat => console.log('success:', cat.toJSON()))
   * .catch(error => console.log('something is wrong:', error));
   */
  remove(categoryId) {
    return new Promise((resolve, reject) => {

      this.request('delete', '/api/categories/remove', { id: categoryId })
      .then(category => resolve(new Entities.category().setData(category)))
      .catch(error => reject(error));

    });
  }
}

let instance = null;
module.exports = (() => {
  if (!instance) {
    instance = new CategoryAPI();
  }

  return instance;
})();