'use strict';

/**
 * hobbs
 *
 * @author Zongmin Lei <leizongmin@gmail.com>
 */

const coroutine = require('lei-coroutine');

module.exports = function modelCommentLike() {

  const model = $.model.CommentLike = $.model.create('comment_likes');

  model.get = function (id, user_id) {
    return model.super.get({ id, user_id });
  };

  model.add = coroutine.wrap(function* (id, user_id) {
    $.utils.checkNotEmpty(id, 'id');
    $.utils.checkNotEmpty(user_id, 'user_id');
    const exists = yield model.get(id, user_id);
    if (exists) return exists;
    return model.super.add({ id, user_id, updated_at: false });
  });

  model.delete = function (id, user_id) {
    $.utils.checkNotEmpty(id, 'id');
    $.utils.checkNotEmpty(user_id, 'user_id');
    return model.super.delete({ id, user_id });
  };

  model.listById = function (id) {
    $.utils.checkNotEmpty(id, 'id');
    return model.table().select().where({ id }).orderBy('created_at', 'asc');
  };

  model.countById = coroutine.wrap(function* (id) {
    $.utils.checkNotEmpty(id, 'id');
    return model.count({ id });
  });

  model.fillLikesCount = $.utils.fillDataFunction(function (id) {
    return model.countById(id);
  });

  model.fillLikedStatus = $.utils.fillDataFunction(coroutine.wrap(function* (id, user_id) {
    const ret = yield model.get(id, user_id);
    return !!ret;
  }));

};
