32 lines
1 KiB
TypeScript
32 lines
1 KiB
TypeScript
import { IdentityContext } from '@app/server/identity';
|
|
import { prisma } from '@app/server/prisma/client';
|
|
import { BookChapterManyOutput, QueryBookChapterManyArgs } from '@generated/types';
|
|
import { Prisma } from '@prisma/client';
|
|
import { toBookChapterSchema } from './mappers/bookChapterMapper';
|
|
|
|
export async function BookChapterManyResolver(
|
|
_: any,
|
|
{ input }: QueryBookChapterManyArgs,
|
|
{ req, res, identity }: IdentityContext
|
|
): Promise<BookChapterManyOutput> {
|
|
const { bookId, filter } = input;
|
|
const terms = filter.terms?.toLowerCase();
|
|
|
|
const bookChapterWhere: Prisma.BookChapterWhereInput = {
|
|
AND: [{ bookId }, { OR: terms != null ? [{ title: { contains: terms } }] : undefined }]
|
|
};
|
|
|
|
const items = await prisma.bookChapter.findMany({
|
|
where: bookChapterWhere,
|
|
skip: filter.offset ?? 0,
|
|
take: filter.size ?? 10,
|
|
orderBy: { sequenceNumber: 'asc' }
|
|
});
|
|
|
|
return {
|
|
items: await Promise.all(
|
|
items.map(value => toBookChapterSchema(value, { req, res, identity }))
|
|
),
|
|
total: items.length
|
|
};
|
|
}
|