added book details, update, delete. fixes
This commit is contained in:
parent
d1da980501
commit
b5738c610d
12 changed files with 290 additions and 16 deletions
34
app/server/graphql/resolvers/bookUpdateResolver.ts
Normal file
34
app/server/graphql/resolvers/bookUpdateResolver.ts
Normal 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 });
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue