|
|
@@ -15,6 +15,21 @@ const schema = new mongoose.Schema({
|
|
|
});
|
|
|
schema.plugin(mongoosePaginate);
|
|
|
|
|
|
+class TagQueryBuilder {
|
|
|
+
|
|
|
+ constructor(query) {
|
|
|
+ this.query = query;
|
|
|
+ }
|
|
|
+
|
|
|
+ addConditionToPagenate(offset, limit, sortOpt) {
|
|
|
+ this.query = this.query
|
|
|
+ .sort(sortOpt).skip(offset).limit(limit); // eslint-disable-line newline-per-chained-call
|
|
|
+
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* Tag Class
|
|
|
*
|
|
|
@@ -30,6 +45,24 @@ class Tag {
|
|
|
return tag;
|
|
|
}
|
|
|
|
|
|
+ static async findList(option) {
|
|
|
+ const opt = Object.assign({ sort: 'count', desc: -1 }, option);
|
|
|
+ const builder = new TagQueryBuilder(this.find({}));
|
|
|
+ const sortOpt = {};
|
|
|
+ sortOpt[opt.sort] = opt.desc;
|
|
|
+
|
|
|
+ // count
|
|
|
+ const totalCount = await builder.query.exec('count');
|
|
|
+
|
|
|
+ // find
|
|
|
+ builder.addConditionToPagenate(opt.offset, opt.limit, sortOpt);
|
|
|
+ const tags = await builder.query.exec('find');
|
|
|
+ const result = {
|
|
|
+ tags, totalCount, offset: opt.offset, limit: opt.limit,
|
|
|
+ };
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
module.exports = function() {
|