提交 009a684a authored 作者: vipcxj's avatar vipcxj

资源查询rest api重构

上级 86053616
import { castArray } from 'lodash';
import post from '../utils/post';
import config from '../utils/config';
import request from '../utils/request';
......@@ -61,6 +60,92 @@ export class Operations {
};
}
export class Query {
constructor() {
this.pageSize = -1;
}
setAfter = (after) => {
this.after = after;
};
setOrder = () => {
const argn = arguments.length;
if (argn > 0) {
this.orders = [];
let lastOrder;
for (let i = 0; i < argn; ++i) {
if (lastOrder !== undefined && (typeof arguments[i] === 'boolean')) {
this.orders.push({
target: lastOrder,
asc: arguments[i],
});
lastOrder = undefined;
} else if (lastOrder !== undefined) {
this.orders.push({
target: lastOrder,
asc: true,
});
lastOrder = arguments[i];
} else {
lastOrder = arguments[i];
}
}
if (lastOrder !== undefined) {
this.orders.push({
target: lastOrder,
asc: true,
});
}
}
};
setPageSize = (size) => {
this.pageSize = size >= 0 ? size : -1;
};
setCondition = (condition) => {
this.condition = condition;
};
setStores = (...stores) => {
this.stores = stores;
};
static tag = (...tags) => {
if (tags.length === 0) {
throw new Error('At least one tag!');
}
if (tags.length === 1) {
return {
type: 'tag',
value: tags[0],
};
} else {
return Query.and(...tags.map(tag => Query.tag(tag)));
}
};
static usage = (...usage) => {
if (usage.length === 0) {
throw new Error('At least one usage!');
}
if (usage.length === 1) {
return {
type: 'usage',
value: usage[0],
};
} else {
return Query.and(...usage.map(tag => Query.usage(tag)));
}
};
static and = (...conditions) => {
return {
type: 'and',
conditions,
};
};
static or = (...conditions) => {
return {
type: 'or',
conditions,
};
};
}
export const createOperations = () => {
return new Operations();
};
......@@ -69,38 +154,10 @@ export async function editResource(uri, operations) {
return post(`${config.apiContextPath}/api/resource/user/${encodeURIComponent(uri)}/meta`, { operations: operations.operations });
}
export async function getResourceInfo(uri) {
return request(`${config.apiContextPath}/api/resource/user/${encodeURIComponent(uri)}/meta`);
}
export async function queryResourceByUsage(usage, pst = -1, psz = -1) {
return request(`${config.apiContextPath}/api/resource/user/query`, { usage, pst, psz });
}
export async function queryResourceByUsageStartWith(usage, pst = -1, psz = -1) {
return request(`${config.apiContextPath}/api/resource/user/query`, { usage, startWith: true, pst, psz });
}
export async function queryResourceByAllTags(tags, pst = -1, psz = -1) {
return request(`${config.apiContextPath}/api/resource/user/query`, { tags: castArray(tags), pst, psz });
export async function queryResource(query) {
return post(`${config.apiContextPath}/api/resource/user/query`, query);
}
export async function queryResourceByAnyTags(tags, pst = -1, psz = -1) {
return request(`${config.apiContextPath}/api/resource/user/query`, { tags: castArray(tags), andOr: 'or', pst, psz });
}
export async function queryResourceByUsageAndAllTags(usage, tags, pst = -1, psz = -1) {
return request(`${config.apiContextPath}/api/resource/user/query`, { usage, tags: castArray(tags), pst, psz });
}
export async function queryResourceByUsageAndAnyTags(usage, tags, pst = -1, psz = -1) {
return request(`${config.apiContextPath}/api/resource/user/query`, { usage, tags: castArray(tags), andOr: 'or', pst, psz });
}
export async function queryResourceByUsageStartWithAndAllTags(usage, tags, pst = -1, psz = -1) {
return request(`${config.apiContextPath}/api/resource/user/query`, { usage, startWith: true, tags: castArray(tags), pst, psz });
}
export async function queryResourceByUsageStartWithAndAnyTags(usage, tags, pst = -1, psz = -1) {
return request(`${config.apiContextPath}/api/resource/user/query`, { usage, startWith: true, tags: castArray(tags), andOr: 'or', pst, psz });
export async function getResourceInfo(uri) {
return request(`${config.apiContextPath}/api/resource/user/${encodeURIComponent(uri)}/meta`);
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论