added mappers, updated models
This commit is contained in:
parent
6ada519e06
commit
a8375b99c2
11 changed files with 306 additions and 120 deletions
23
app/server/graphql/resolvers/mappers/bookChapterMapper.ts
Normal file
23
app/server/graphql/resolvers/mappers/bookChapterMapper.ts
Normal 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;
|
||||
})
|
||||
};
|
||||
}
|
||||
22
app/server/graphql/resolvers/mappers/bookMapper.ts
Normal file
22
app/server/graphql/resolvers/mappers/bookMapper.ts
Normal 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;
|
||||
})
|
||||
};
|
||||
}
|
||||
24
app/server/graphql/resolvers/mappers/reviewMapper.ts
Normal file
24
app/server/graphql/resolvers/mappers/reviewMapper.ts
Normal 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;
|
||||
})
|
||||
};
|
||||
}
|
||||
65
app/server/graphql/resolvers/mappers/workingMapper.ts
Normal file
65
app/server/graphql/resolvers/mappers/workingMapper.ts
Normal 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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue