mirror of
https://github.com/docmost/docmost.git
synced 2026-05-07 06:23:06 +08:00
limit
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||||
import { LabelRepo, LabelType } from '@docmost/db/repos/label/label.repo';
|
import { LabelRepo, LabelType } from '@docmost/db/repos/label/label.repo';
|
||||||
import { InjectKysely } from 'nestjs-kysely';
|
import { InjectKysely } from 'nestjs-kysely';
|
||||||
import { KyselyDB } from '@docmost/db/types/kysely.types';
|
import { KyselyDB } from '@docmost/db/types/kysely.types';
|
||||||
import { executeTx } from '@docmost/db/utils';
|
import { executeTx } from '@docmost/db/utils';
|
||||||
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
|
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
|
||||||
|
|
||||||
|
const MAX_LABELS_PER_PAGE = 25;
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class LabelService {
|
export class LabelService {
|
||||||
constructor(
|
constructor(
|
||||||
@@ -18,6 +20,13 @@ export class LabelService {
|
|||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
) {
|
) {
|
||||||
await executeTx(this.db, async (trx) => {
|
await executeTx(this.db, async (trx) => {
|
||||||
|
const currentCount = await this.labelRepo.getPageLabelCount(pageId, trx);
|
||||||
|
if (currentCount + names.length > MAX_LABELS_PER_PAGE) {
|
||||||
|
throw new BadRequestException(
|
||||||
|
`A page can have a maximum of ${MAX_LABELS_PER_PAGE} labels`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
for (const name of names) {
|
for (const name of names) {
|
||||||
const label = await this.labelRepo.findOrCreate(
|
const label = await this.labelRepo.findOrCreate(
|
||||||
name.trim(),
|
name.trim(),
|
||||||
|
|||||||
@@ -164,6 +164,20 @@ export class LabelRepo {
|
|||||||
.execute();
|
.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getPageLabelCount(
|
||||||
|
pageId: string,
|
||||||
|
trx?: KyselyTransaction,
|
||||||
|
): Promise<number> {
|
||||||
|
const db = dbOrTx(this.db, trx);
|
||||||
|
const result = await db
|
||||||
|
.selectFrom('pageLabels')
|
||||||
|
.select((eb) => eb.fn.count('id').as('count'))
|
||||||
|
.where('pageId', '=', pageId)
|
||||||
|
.executeTakeFirst();
|
||||||
|
|
||||||
|
return Number(result?.count ?? 0);
|
||||||
|
}
|
||||||
|
|
||||||
async getLabelPageCount(
|
async getLabelPageCount(
|
||||||
labelId: string,
|
labelId: string,
|
||||||
trx?: KyselyTransaction,
|
trx?: KyselyTransaction,
|
||||||
|
|||||||
Reference in New Issue
Block a user