added chapter view, fixes, added ratings, added chapter updates/deletions

This commit is contained in:
lnn0q 2025-06-01 00:37:26 +03:00
parent 029b7e9353
commit 4801cf3a13
34 changed files with 1345 additions and 191 deletions

View file

@ -1,9 +1,15 @@
import { call } from '@app/common';
import { ensureRatingType } from '@app/server/helpers';
import { IdentityContext } from '@app/server/identity';
import { prisma } from '@app/server/prisma/client';
import { BookChapter as BookChapterSchema } from '@generated/types';
import { BookChapter as BookChapterSchema, RatingType } from '@generated/types';
import { BookChapter } from '@prisma/client';
import { toBookSchema } from './bookMapper';
export async function toBookChapterSchema(model: BookChapter): Promise<BookChapterSchema> {
export async function toBookChapterSchema(
model: BookChapter,
{ req, res, identity }: IdentityContext
): Promise<BookChapterSchema> {
return {
id: model.id,
title: model.title,
@ -11,7 +17,44 @@ export async function toBookChapterSchema(model: BookChapter): Promise<BookChapt
updatedAt: model.createdAt.toISOString(),
foreword: model.foreword,
footnote: model.footnote,
content: model.content,
sequenceNumber: model.sequenceNumber,
previousChapterId: await call(async () => {
const isFirst = model.sequenceNumber === 1;
if (isFirst) return null;
const previousChapter = await prisma.bookChapter.findUniqueOrThrow({
where: {
bookId_sequenceNumber: {
bookId: model.bookId,
sequenceNumber: model.sequenceNumber - 1
}
},
select: {
id: true
}
});
return previousChapter.id;
}),
nextChapterId: await call(async () => {
const book = await prisma.book.findUniqueOrThrow({
where: { id: model.bookId },
select: { chaptersTotal: true }
});
const isLast = model.sequenceNumber === book.chaptersTotal;
if (isLast) return null;
const nextChapter = await prisma.bookChapter.findUniqueOrThrow({
where: {
bookId_sequenceNumber: {
bookId: model.bookId,
sequenceNumber: model.sequenceNumber + 1
}
},
select: { id: true }
});
return nextChapter.id;
}),
reviewsTotal: await call(async () => {
const reviewsCount = await prisma.review.count({
where: {
@ -19,6 +62,28 @@ export async function toBookChapterSchema(model: BookChapter): Promise<BookChapt
}
});
return reviewsCount;
}),
book: await call(async () => {
const book = await prisma.book.findUniqueOrThrow({
where: {
id: model.bookId
}
});
return toBookSchema(book, { req, res, identity });
}),
personalRating: await call(async () => {
if (identity == null) return RatingType.None;
const rating = await prisma.rating.findUnique({
where: {
userId_bookChapterId: {
userId: identity.id,
bookChapterId: model.id
}
}
});
if (rating == null) return RatingType.None;
ensureRatingType(rating.type);
return rating.type;
})
};
}