2021-12-16 08:46:54 +00:00
|
|
|
import { MultipleRelationRepository, Repository } from '@nocobase/database';
|
2022-03-27 07:51:48 +00:00
|
|
|
import { Context } from '.';
|
2021-12-16 08:46:54 +00:00
|
|
|
|
2023-08-15 03:32:51 +00:00
|
|
|
export function pageArgsToLimitArgs(
|
|
|
|
page: number,
|
|
|
|
pageSize: number,
|
|
|
|
): {
|
|
|
|
offset: number;
|
|
|
|
limit: number;
|
|
|
|
} {
|
|
|
|
return {
|
|
|
|
offset: (page - 1) * pageSize,
|
|
|
|
limit: pageSize,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-16 08:46:54 +00:00
|
|
|
export function getRepositoryFromParams(ctx: Context) {
|
2022-01-23 04:42:35 +00:00
|
|
|
const { resourceName, resourceOf } = ctx.action;
|
2021-12-16 08:46:54 +00:00
|
|
|
|
2022-01-23 04:42:35 +00:00
|
|
|
if (resourceOf) {
|
|
|
|
return ctx.db.getRepository<MultipleRelationRepository>(resourceName, resourceOf);
|
2021-12-16 08:46:54 +00:00
|
|
|
}
|
|
|
|
|
2022-01-23 04:42:35 +00:00
|
|
|
return ctx.db.getRepository<Repository>(resourceName);
|
2021-12-16 08:46:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function RelationRepositoryActionBuilder(method: 'remove' | 'set') {
|
|
|
|
return async function (ctx: Context, next) {
|
|
|
|
const repository = getRepositoryFromParams(ctx);
|
|
|
|
|
2023-08-23 16:27:57 +00:00
|
|
|
const filterByTk = ctx.action.params.filterByTk || ctx.action.params.filterByTks || ctx.action.params.values;
|
|
|
|
|
|
|
|
await repository[method](filterByTk);
|
2022-11-28 09:27:12 +00:00
|
|
|
|
|
|
|
ctx.status = 200;
|
2021-12-16 08:46:54 +00:00
|
|
|
await next();
|
|
|
|
};
|
|
|
|
}
|