added book details, update, delete. fixes

This commit is contained in:
lnn0q 2025-06-01 19:37:13 +03:00
parent d1da980501
commit b5738c610d
12 changed files with 290 additions and 16 deletions

View file

@ -0,0 +1,22 @@
import { ensureIdentity } from '@app/server/helpers';
import { IdentityContext } from '@app/server/identity';
import { prisma } from '@app/server/prisma/client';
import { MutationBookDeleteArgs } from '@generated/types';
export async function BookDeleteResolver(
_: any,
{ id }: MutationBookDeleteArgs,
{ identity }: IdentityContext
): Promise<void> {
ensureIdentity(identity);
const model = await prisma.book.findUniqueOrThrow({
where: { id }
});
if (identity.id !== model.authorId) throw new Error('Operation is not permitted.');
await prisma.book.delete({
where: { id }
});
}

View file

@ -0,0 +1,34 @@
import { ensureIdentity } from '@app/server/helpers';
import { IdentityContext } from '@app/server/identity';
import { prisma } from '@app/server/prisma/client';
import { Book, MutationBookUpdateArgs } from '@generated/types';
import { toBookSchema } from './mappers/bookMapper';
export async function BookUpdateResolver(
_: any,
{ input }: MutationBookUpdateArgs,
{ req, res, identity }: IdentityContext
): Promise<Book> {
ensureIdentity(identity);
const model = await prisma.book.findUniqueOrThrow({
where: { id: input.id }
});
if (identity.id !== model.authorId) throw new Error('Operation is not permitted.');
if (input.title != null && input.title.length > 60) throw new Error('Title is too long.');
if (input.description != null && input.description.length > 900)
throw new Error('Description is too long.');
const modelUpdated = await prisma.book.update({
where: { id: input.id },
data: {
title: input.title != null ? input.title : undefined,
description: input.description != null ? input.description : undefined,
progressStatus: input.progressStatus != null ? input.progressStatus : undefined
}
});
return toBookSchema(modelUpdated, { req, res, identity });
}