teambition-sdk-socket
Version:
Front-End SDK for Teambition
98 lines • 4.08 kB
JavaScript
'use strict';
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var index_1 = require("../utils/index");
var BaseModel_1 = require("./BaseModel");
var Post_1 = require("../schemas/Post");
var BaseCollection_1 = require("./BaseCollection");
var PostModel = (function (_super) {
__extends(PostModel, _super);
function PostModel() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this._schemaName = 'Post';
return _this;
}
PostModel.prototype.addOne = function (post) {
var result = index_1.dataToSchema(post, Post_1.default);
return this._save(result);
};
PostModel.prototype.getOne = function (postId) {
return this._get(postId);
};
/**
* _collections 索引为 `project:posts/${projectId}`
*/
PostModel.prototype.addPosts = function (projectId, posts, page) {
var dbIndex = "project:posts/" + projectId;
var result = index_1.datasToSchemas(posts, Post_1.default);
var collection = this._collections.get(dbIndex);
if (!collection) {
collection = new BaseCollection_1.default(this._schemaName, function (data) {
return data._projectId === projectId && !data.isArchived;
}, dbIndex);
this._collections.set(dbIndex, collection);
}
return collection.addPage(page, result);
};
PostModel.prototype.getPosts = function (projectId, page) {
var collection = this._collections.get("project:posts/" + projectId);
if (collection) {
return collection.get(page);
}
return null;
};
PostModel.prototype.addMyPosts = function (userId, projectId, posts, page) {
var dbIndex = "project:my:posts/" + projectId;
var result = index_1.datasToSchemas(posts, Post_1.default);
var collection = this._collections.get(dbIndex);
if (!collection) {
collection = new BaseCollection_1.default(this._schemaName, function (data) {
return data._projectId === projectId &&
!data.isArchived &&
data._creatorId === userId;
}, dbIndex);
this._collections.set(dbIndex, collection);
}
return collection.addPage(page, result);
};
PostModel.prototype.getMyPosts = function (projectId, page) {
var collection = this._collections.get("project:my:posts/" + projectId);
if (collection) {
return collection.get(page);
}
return null;
};
PostModel.prototype.addByTagId = function (tagId, posts, page) {
var dbIndex = "tag:posts/" + tagId;
var result = index_1.datasToSchemas(posts, Post_1.default);
var collection = this._collections.get(dbIndex);
if (!collection) {
collection = new BaseCollection_1.default(this._schemaName, function (data) {
return !data.isArchived && data.tagIds && data.tagIds.indexOf(tagId) !== -1;
}, dbIndex);
this._collections.set(dbIndex, collection);
}
return collection.addPage(page, result);
};
PostModel.prototype.getByTagId = function (tagId, page) {
var dbIndex = "tag:posts/" + tagId;
var collection = this._collections.get(dbIndex);
if (collection) {
return collection.get(page);
}
return null;
};
return PostModel;
}(BaseModel_1.default));
exports.PostModel = PostModel;
exports.default = new PostModel;
//# sourceMappingURL=PostModel.js.map