forchange-api
Version:
A sdk of forchange api.
187 lines (171 loc) • 4.72 kB
JavaScript
const co = require('co');
const HttpClient = require('st-api').HttpClient;
const APIs = require('st-api').APIs;
const StatusCode = require('./common/status-code');
const apiconfig = require('./config/assess.api.config');
/* eslint camelcase:0 */
const __instance = (function () {
let instance = null;
return (_ins) => {
if (_ins) {
instance = _ins;
}
return instance;
};
})();
let apis = {};
let tokenModel = null;
class QuestionModel {
constructor(opts) {
if (__instance()) {
return __instance();
}
__instance(this);
}
static config(opts) {
apiconfig.defaultHost = opts.host;
let httpClient = new HttpClient();
apis = new APIs(apiconfig, httpClient);
tokenModel = opts.tokenModel;
}
getQuestionList(book_id, page, per_page) {
return co(function* () {
if (!(tokenModel && apis.questionList)) {
throw new Error('You must config QuestionModel with QuestionModel.config(options) before use this method.');
}
let token = yield tokenModel.getToken();
page = page || 1;
per_page = per_page || 10;
let urlParam = {
book_id
};
let qs = {
token,
page,
per_page
};
let data = yield apis.questionList.get(urlParam, qs);
if (!data.data) {
throw new Error(`An error occured when request ${apis.questionList.url}`);
}
return {
questions: data.data,
total: data.meta.pagination.total,
count: data.meta.pagination.count,
per_page: data.meta.pagination.per_page,
current_page: data.meta.pagination.current_page,
total_pages: data.meta.pagination.total_pages
};
});
}
getQuestion(id, book_id) {
return co(function* () {
if (!(tokenModel && apis.question)) {
throw new Error('You must config QuestionModel with QuestionModel.config(options) before use this method.');
}
let token = yield tokenModel.getToken();
let urlParam = {
id,
book_id
};
let qs = {
token
};
let data = yield apis.question.get(urlParam, qs);
if (!data.data) {
throw new Error(`An error occured when request ${apis.question.url}`);
}
return data.data;
});
}
switchQuestionStatus(id, book_id, question_status) {
return co(function* () {
if (!(tokenModel && apis.switchStatus)) {
throw new Error('You must config QuestionModel with QuestionModel.config(options) before use this method.');
}
let token = yield tokenModel.getToken();
let urlParam = {
id,
book_id
};
let qs = {
token
};
let body = {
question_status
};
let data = yield apis.switchStatus.put(body, urlParam, qs);
// 这里应该要记录下错误,只是接口文档中没给出错误信息的响应格式
if (data.status_code === StatusCode.OK) {
return true;
} else {
throw new Error(`An error occured when request ${apis.switchStatus.url}: ${data.message}`);
}
});
}
deleteQuestion(id, book_id) {
return co(function* () {
if (!(tokenModel && apis.deleteQuestion)) {
throw new Error('You must config QuestionModel with QuestionModel.config(options) before use this method.');
}
let token = yield tokenModel.getToken();
let urlParam = {
id,
book_id
};
let qs = {
token
};
let data = yield apis.deleteQuestion.delete(urlParam, qs);
if (data.status_code === StatusCode.OK) {
return true;
} else {
throw new Error(`An error occured when request ${apis.deleteQuestion.url}: ${data.message}`);
}
});
}
updateQuestion(id, book_id, question) {
return co(function* () {
if (!(tokenModel && apis.updateQuestion)) {
throw new Error('You must config QuestionModel with QuestionModel.config(options) before use this method.');
}
let token = yield tokenModel.getToken();
let urlParam = {
id,
book_id
};
let qs = {
token
};
let body = question;
let data = yield apis.updateQuestion.put(body, urlParam, qs);
if (data.status_code == StatusCode.OK) {
return true;
} else {
throw new Error(`An error occured when request ${apis.updateQuestion.url}: ${data.message}`);
}
});
}
addQuestion(book_id, question) {
return co(function* () {
if (!(tokenModel && apis.addQuestion)) {
throw new Error('You must config QuestionModel with QuestionModel.config(options) before use this method.');
}
let token = yield tokenModel.getToken();
let urlParam = {
book_id
};
let qs = {
token
};
let body = question;
let data = yield apis.addQuestion.post(body, urlParam, qs);
if (data.status_code == StatusCode.OK) {
return true;
} else {
throw new Error(`An error occured when request ${apis.addQuestion.url}: ${data.message}`);
}
});
}
};
module.exports = QuestionModel;