added mappers, updated models

This commit is contained in:
lnn0q 2025-05-17 17:16:26 +03:00
parent 6ada519e06
commit a8375b99c2
11 changed files with 306 additions and 120 deletions

View file

@ -1,13 +0,0 @@
import { IdentityContext } from '@app/server/identity';
import { Book, QueryBookOneArgs } from '@generated/types';
import { books } from './booksManyResolver';
export async function BookOneResolver(
_: any,
{ id }: QueryBookOneArgs,
{ identity }: IdentityContext
): Promise<Book> {
const book = books.filter(item => item.id === id)[0];
return book;
}

View file

@ -1,25 +0,0 @@
import { IdentityContext } from '@app/server/identity';
import { Book } from '@generated/types';
export const books = [
{
id: '1',
title: 'some-title-1',
author: 'someone'
},
{
id: '2',
title: 'some-title-1',
author: 'someone'
}
];
export async function BooksManyResolver(
_: any,
__: any,
{ req, res, identity }: IdentityContext
): Promise<Book[]> {
if (identity == null) throw new Error('Unauthorized');
return books;
}

View file

@ -0,0 +1,23 @@
import { call } from '@app/common';
import { prisma } from '@app/server/prisma/client';
import { BookChapter as BookChapterSchema } from '@generated/types';
import { BookChapter } from '@prisma/client';
export async function toBookChapterMapping(model: BookChapter): Promise<BookChapterSchema> {
return {
id: model.id,
title: model.title,
createdAt: model.createdAt.toISOString(),
updatedAt: model.createdAt.toISOString(),
foreword: model.foreword,
footnote: model.footnote,
reviewsTotal: await call(async () => {
const reviewsCount = await prisma.review.count({
where: {
bookChapterId: model.id
}
});
return reviewsCount;
})
};
}

View file

@ -0,0 +1,22 @@
import { call } from '@app/common';
import { prisma } from '@app/server/prisma/client';
import { Book as BookSchema } from '@generated/types';
import { Book } from '@prisma/client';
export async function toBookSchema(model: Book): Promise<BookSchema> {
return {
id: model.id,
workingId: model.workingId,
createdAt: model.createdAt.toISOString(),
updatedAt: model.updatedAt.toISOString(),
wordsTotal: model.wordsTotal,
chaptersTotal: await call(async () => {
const chaptersCount = await prisma.bookChapter.count({
where: {
bookId: model.id
}
});
return chaptersCount;
})
};
}

View file

@ -0,0 +1,24 @@
import { Review } from '@prisma/client';
import { Review as ReviewSchema } from '@generated/types';
import { toUserSchema } from './userMapper';
import { call } from '@app/common';
import { prisma } from '@app/server/prisma/client';
export async function toReviewSchema(model: Review): Promise<ReviewSchema> {
return {
id: model.id,
author: await call(async () => {
const userModel = await prisma.user.findUniqueOrThrow({
where: {
id: model.userId
}
});
return toUserSchema(userModel);
}),
text: model.text,
//todo: ratings implementation
ratingTotal: await call(async () => {
return 0;
})
};
}

View file

@ -0,0 +1,65 @@
import { assertUnreachable } from '@app/client/helpers';
import { call } from '@app/common';
import { prisma } from '@app/server/prisma/client';
import {
WorkingProgressStatus as WorkingProgressStatusSchema,
Working as WorkingSchema,
WorkingType as WorkingTypeSchema
} from '@generated/types';
import { Working, WorkingProgressStatus, WorkingType } from '@prisma/client';
import { toUserSchema } from './userMapper';
export async function toWorkingSchema(model: Working): Promise<WorkingSchema> {
return {
id: model.id,
title: model.title,
description: model.description,
author: await call(async () => {
const userModel = await prisma.user.findUniqueOrThrow({ where: { id: model.authorId } });
return toUserSchema(userModel);
}),
createdAt: model.createdAt.toISOString(),
updatedAt: model.updatedAt.toISOString(),
type: call(() => {
ensureWorkingType(model.type);
return model.type;
}),
progressStatus: call(() => {
ensureWorkingProgressStatus(model.progressStatus);
return model.progressStatus;
}),
//todo: ratings implementation
//todo: book/notes implementation
ratingTotal: await call(async () => {
const ratingsCount = await prisma.rating.count({
where: {
workingId: model.id
}
});
return ratingsCount;
}),
reviewsTotal: await call(async () => {
const reviewsCount = await prisma.review.count({
where: {
workingId: model.id
}
});
return reviewsCount;
})
};
}
function ensureWorkingType(value: WorkingType): asserts value is WorkingTypeSchema {
if (value == WorkingType.book) return;
if (value == WorkingType.notepad) return;
assertUnreachable(value);
}
function ensureWorkingProgressStatus(
value: WorkingProgressStatus
): asserts value is WorkingProgressStatusSchema {
if (value == WorkingProgressStatus.inProgress) return;
if (value == WorkingProgressStatus.frozen) return;
if (value == WorkingProgressStatus.complete) return;
assertUnreachable(value);
}