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

@ -17,6 +17,7 @@ export const Query: QueryResolvers = {
bookOne: BookOneResolver,
identity: IdentityResolver,
userOne: UserOneResolver
// workingMany:
};
export const Mutation: MutationResolvers = {

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);
}

View file

@ -1,8 +1,6 @@
scalar Void
type Query {
booksMany: [Book!]!
bookOne(id: ID!): Book!
identity: Identity!
userOne(id: ID!): User!
workingMany(input: WorkingManyInput!): WorkingManyOutput!
@ -44,14 +42,6 @@ type User {
updatedAt: String!
}
type Book {
id: ID!
title: String!
author: String!
}
input Working
input PaginationFilterInput {
terms: String
offset: Int
@ -60,12 +50,18 @@ input PaginationFilterInput {
input WorkingManyInput {
filter: PaginationFilterInput!
type: WorkingType!
}
enum WorkingType {
book
notepad
}
enum WorkingProgressStatus {
inProgress
frozen
finished
complete
}
type WorkingManyOutput {
@ -80,35 +76,58 @@ type Working {
author: User!
createdAt: String!
updatedAt: String!
type: WorkingType!
book: Book
notepad: Notepad
progressStatus: WorkingProgressStatus!
wordsTotal: Int!
ratingTotal: Int!
chapters: ChapterManyOutput!
reviews: ReviewManyOutput!
reviewsTotal: Int!
}
type ChapterManyOutput {
items: [Chapter!]!
total: Int!
type Book {
id: ID!
workingId: ID!
createdAt: String!
updatedAt: String!
wordsTotal: Int!
chaptersTotal: Int!
}
type Chapter {
type BookChapter {
id: ID!
title: String!
createdAt: String!
updatedAt: String!
foreword: String!
footnote: String!
reviewsTotal: Int!
}
type ReviewManyOutput {
items: [Review!]!
input BookChapterManyInput {
filter: PaginationFilterInput!
}
type BookChapterManyOutput {
items: [BookChapter!]!
total: Int!
}
type Notepad {
id: ID!
}
type Review {
id: ID!
author: User!
text: String!
ratingTotal: Int!
}
input ReviewManyInput {
filter: PaginationFilterInput!
}
type ReviewManyOutput {
items: [Review!]!
total: Int!
}