38 lines
1.4 KiB
TypeScript
38 lines
1.4 KiB
TypeScript
import { countWords, ensureIdentity } from '@app/server/helpers';
|
|
import { IdentityContext } from '@app/server/identity';
|
|
import { prisma, uuid } from '@app/server/prisma/client';
|
|
import { updateBookStats } from '@app/server/service/book';
|
|
import { BookChapter, MutationBookChapterCreateArgs } from '@generated/types';
|
|
import { toBookChapterSchema } from './mappers/bookChapterMapper';
|
|
|
|
export async function BookChapterCreateResolver(
|
|
_: any,
|
|
{ input }: MutationBookChapterCreateArgs,
|
|
{ req, res, identity }: IdentityContext
|
|
): Promise<BookChapter> {
|
|
ensureIdentity(identity);
|
|
|
|
if (input.title.length > 60) throw new Error('Title is too long.');
|
|
if (input.foreword != null && input.foreword.length > 500)
|
|
throw new Error('Foreword is too long.');
|
|
if (input.footnote != null && input.footnote.length > 500)
|
|
throw new Error('Footnote is too long.');
|
|
|
|
const chapterCount = await prisma.bookChapter.count({ where: { bookId: input.bookId } });
|
|
|
|
const model = await prisma.bookChapter.create({
|
|
data: {
|
|
id: uuid(),
|
|
bookId: input.bookId,
|
|
title: input.title,
|
|
foreword: input.foreword,
|
|
footnote: input.footnote,
|
|
content: input.content,
|
|
wordsTotal: countWords(input.content),
|
|
sequenceNumber: chapterCount + 1
|
|
}
|
|
});
|
|
|
|
await updateBookStats(input.bookId);
|
|
return toBookChapterSchema(model, { req, res, identity });
|
|
}
|