implemented ratings, fixes
This commit is contained in:
parent
4801cf3a13
commit
d1da980501
31 changed files with 775 additions and 236 deletions
|
|
@ -29,12 +29,17 @@ export function toAppConfig(): AppConfig {
|
|||
const host = process.env.VITE_HOST || 'localhost';
|
||||
const port = parseInt(process.env.VITE_PORT || '3000');
|
||||
|
||||
const hostRemote = process.env.VITE_HOST_REMOTE || 'localhost';
|
||||
const domainName = process.env.VITE_DOMAIN_NAME || 'localhost';
|
||||
|
||||
const urlIp = `http://${hostRemote}:${port}`;
|
||||
const urlDomain = `http://${domainName}:${port}`;
|
||||
const urlRemote = `https://${host}:${port}`;
|
||||
const urlLocal = host !== 'localhost' ? `https://localhost:${port}` : undefined;
|
||||
|
||||
const limit = { limit: '50mb' };
|
||||
|
||||
const origin = urlLocal != null ? [urlLocal, urlRemote] : [urlRemote];
|
||||
const origin = urlLocal != null ? [urlLocal, urlRemote, urlIp, urlDomain] : [urlRemote];
|
||||
const corsOptions: cors.CorsOptions = {
|
||||
origin,
|
||||
credentials: true
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import { BookChapterOneResolver } from './resolvers/bookChapterOneResolver';
|
|||
import { BookManyResolver } from './resolvers/bookManyResolver';
|
||||
import { BookOneResolver } from './resolvers/bookOneResolver';
|
||||
import { IdentityResolver } from './resolvers/identityResolver';
|
||||
import { ReviewManyResolver } from './resolvers/reviewManyResolver';
|
||||
import { UserOneResolver } from './resolvers/userOneResolver';
|
||||
|
||||
/* Mutation */
|
||||
|
|
@ -17,6 +18,8 @@ import { BookChapterCreateResolver } from './resolvers/bookChapterCreateResolver
|
|||
import { BookChapterDeleteResolver } from './resolvers/bookChapterDelete';
|
||||
import { BookChapterUpdateResolver } from './resolvers/bookChapterUpdateResolver';
|
||||
import { BookCreateResolver } from './resolvers/bookCreateResolver';
|
||||
import { RatingSetResolver } from './resolvers/ratingSetResolver';
|
||||
import { ReviewCreateResolver } from './resolvers/reviewCreateResolver';
|
||||
|
||||
export const Query: QueryResolvers = {
|
||||
bookOne: BookOneResolver,
|
||||
|
|
@ -24,6 +27,7 @@ export const Query: QueryResolvers = {
|
|||
bookChapterOne: BookChapterOneResolver,
|
||||
bookChapterMany: BookChapterManyResolver,
|
||||
identity: IdentityResolver,
|
||||
reviewMany: ReviewManyResolver,
|
||||
userOne: UserOneResolver
|
||||
};
|
||||
|
||||
|
|
@ -34,7 +38,9 @@ export const Mutation: MutationResolvers = {
|
|||
bookCreate: BookCreateResolver,
|
||||
bookChapterCreate: BookChapterCreateResolver,
|
||||
bookChapterDelete: BookChapterDeleteResolver,
|
||||
bookChapterUpdate: BookChapterUpdateResolver
|
||||
bookChapterUpdate: BookChapterUpdateResolver,
|
||||
ratingSet: RatingSetResolver,
|
||||
reviewCreate: ReviewCreateResolver
|
||||
};
|
||||
|
||||
export const Subscription = {};
|
||||
|
|
|
|||
|
|
@ -13,12 +13,29 @@ export async function BookChapterDeleteResolver(
|
|||
ensureIdentity(identity);
|
||||
const model = await prisma.bookChapter.findUniqueOrThrow({
|
||||
where: { id },
|
||||
select: { book: { select: { id: true, authorId: true } } }
|
||||
select: {
|
||||
sequenceNumber: true,
|
||||
book: { select: { id: true, authorId: true } }
|
||||
}
|
||||
});
|
||||
if (identity.id !== model.book.authorId) throw new Error('Operation is not permitted.');
|
||||
|
||||
await prisma.bookChapter.delete({ where: { id } });
|
||||
|
||||
await prisma.bookChapter.updateMany({
|
||||
where: {
|
||||
bookId: model.book.id,
|
||||
sequenceNumber: {
|
||||
gt: model.sequenceNumber
|
||||
}
|
||||
},
|
||||
data: {
|
||||
sequenceNumber: {
|
||||
decrement: 1
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
await updateBookStats(model.book.id);
|
||||
const book = await prisma.book.findUniqueOrThrow({ where: { id: model.book.id } });
|
||||
return toBookSchema(book, { req, res, identity });
|
||||
|
|
|
|||
|
|
@ -12,13 +12,26 @@ export async function BookChapterUpdateResolver(
|
|||
): Promise<BookChapter> {
|
||||
ensureIdentity(identity);
|
||||
|
||||
const model = await prisma.bookChapter.findUniqueOrThrow({
|
||||
where: { id: input.id },
|
||||
select: {
|
||||
book: {
|
||||
select: {
|
||||
authorId: true
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (identity.id !== model.book.authorId) throw new Error('Operation is not permitted.');
|
||||
|
||||
if (input.title.length > 60) throw new Error('Title is too long.');
|
||||
if (input.foreword != null && input.foreword.length > 500)
|
||||
throw new Error('Foreword is too long.');
|
||||
if (input.footnote != null && input.footnote.length > 500)
|
||||
throw new Error('Footnote is too long.');
|
||||
|
||||
const model = await prisma.bookChapter.update({
|
||||
const modelUpdated = await prisma.bookChapter.update({
|
||||
where: { id: input.id },
|
||||
data: {
|
||||
title: input.title,
|
||||
|
|
@ -29,6 +42,6 @@ export async function BookChapterUpdateResolver(
|
|||
}
|
||||
});
|
||||
|
||||
await updateBookStats(model.bookId);
|
||||
return toBookChapterSchema(model, { req, res, identity });
|
||||
await updateBookStats(modelUpdated.bookId);
|
||||
return toBookChapterSchema(modelUpdated, { req, res, identity });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,10 +5,10 @@ import { IdentityContext } from '@app/server/identity';
|
|||
import { prisma } from '@app/server/prisma/client';
|
||||
import {
|
||||
Book as BookSchema,
|
||||
RatingType,
|
||||
RatingType as RatingTypeSchema,
|
||||
WorkingProgressStatus as WorkingProgressStatusSchema
|
||||
} from '@generated/types';
|
||||
import { Book, WorkingProgressStatus } from '@prisma/client';
|
||||
import { Book, RatingType, WorkingProgressStatus } from '@prisma/client';
|
||||
import { toUserSchema } from './userMapper';
|
||||
|
||||
export async function toBookSchema(
|
||||
|
|
@ -38,14 +38,22 @@ export async function toBookSchema(
|
|||
});
|
||||
return chaptersCount;
|
||||
}),
|
||||
// todo: ratings implementation
|
||||
ratingTotal: await call(async () => {
|
||||
const ratingsCount = await prisma.rating.count({
|
||||
const ratingsUpCount = await prisma.rating.count({
|
||||
where: {
|
||||
bookId: model.id
|
||||
bookId: model.id,
|
||||
type: RatingType.up
|
||||
}
|
||||
});
|
||||
return ratingsCount;
|
||||
|
||||
const ratingsDownCount = await prisma.rating.count({
|
||||
where: {
|
||||
bookId: model.id,
|
||||
type: RatingType.down
|
||||
}
|
||||
});
|
||||
|
||||
return ratingsUpCount - ratingsDownCount;
|
||||
}),
|
||||
reviewsTotal: await call(async () => {
|
||||
const reviewsCount = await prisma.review.count({
|
||||
|
|
@ -56,16 +64,16 @@ export async function toBookSchema(
|
|||
return reviewsCount;
|
||||
}),
|
||||
personalRating: await call(async () => {
|
||||
if (identity == null) return RatingType.None;
|
||||
if (identity == null) return RatingTypeSchema.None;
|
||||
const rating = await prisma.rating.findUnique({
|
||||
where: {
|
||||
userId_bookChapterId: {
|
||||
userId_bookId: {
|
||||
userId: identity.id,
|
||||
bookChapterId: model.id
|
||||
bookId: model.id
|
||||
}
|
||||
}
|
||||
});
|
||||
if (rating == null) return RatingType.None;
|
||||
if (rating == null) return RatingTypeSchema.None;
|
||||
ensureRatingType(rating.type);
|
||||
return rating.type;
|
||||
})
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ export async function toReviewSchema(model: Review): Promise<ReviewSchema> {
|
|||
return toUserSchema(userModel);
|
||||
}),
|
||||
text: model.text,
|
||||
createdAt: model.createdAt.toISOString(),
|
||||
updatedAt: model.updatedAt.toISOString(),
|
||||
// todo: ratings implementation
|
||||
ratingTotal: await call(async () => {
|
||||
return 0;
|
||||
|
|
|
|||
|
|
@ -3,13 +3,12 @@ import { IdentityContext } from '@app/server/identity';
|
|||
import { prisma, uuid } from '@app/server/prisma/client';
|
||||
import { MutationRatingSetArgs, RatingType } from '@generated/types';
|
||||
|
||||
export async function BookCreateResolver(
|
||||
export async function RatingSetResolver(
|
||||
_: any,
|
||||
{ input }: MutationRatingSetArgs,
|
||||
{ identity }: IdentityContext
|
||||
): Promise<void> {
|
||||
ensureIdentity(identity);
|
||||
|
||||
if (input.bookId != null) {
|
||||
const rating = await prisma.rating.findUnique({
|
||||
where: {
|
||||
|
|
|
|||
26
app/server/graphql/resolvers/reviewCreateResolver.ts
Normal file
26
app/server/graphql/resolvers/reviewCreateResolver.ts
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import { ensureIdentity } from '@app/server/helpers';
|
||||
import { IdentityContext } from '@app/server/identity';
|
||||
import { prisma, uuid } from '@app/server/prisma/client';
|
||||
import { MutationReviewCreateArgs, Review } from '@generated/types';
|
||||
import { toReviewSchema } from './mappers/reviewMapper';
|
||||
|
||||
export async function ReviewCreateResolver(
|
||||
_: any,
|
||||
{ input }: MutationReviewCreateArgs,
|
||||
{ identity }: IdentityContext
|
||||
): Promise<Review> {
|
||||
ensureIdentity(identity);
|
||||
|
||||
if (input.text.length > 700) throw new Error('Review is too long.');
|
||||
|
||||
const model = await prisma.review.create({
|
||||
data: {
|
||||
id: uuid(),
|
||||
text: input.text,
|
||||
bookId: input.bookId,
|
||||
userId: identity.id
|
||||
}
|
||||
});
|
||||
|
||||
return toReviewSchema(model);
|
||||
}
|
||||
0
app/server/graphql/resolvers/reviewDeleteResolver.ts
Normal file
0
app/server/graphql/resolvers/reviewDeleteResolver.ts
Normal file
26
app/server/graphql/resolvers/reviewManyResolver.ts
Normal file
26
app/server/graphql/resolvers/reviewManyResolver.ts
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import { IdentityContext } from '@app/server/identity';
|
||||
import { prisma } from '@app/server/prisma/client';
|
||||
import { QueryReviewManyArgs, ReviewManyOutput } from '@generated/types';
|
||||
import { toReviewSchema } from './mappers/reviewMapper';
|
||||
|
||||
export async function ReviewManyResolver(
|
||||
_: any,
|
||||
{ input }: QueryReviewManyArgs,
|
||||
{ req, res, identity }: IdentityContext
|
||||
): Promise<ReviewManyOutput> {
|
||||
const { bookId, filter } = input;
|
||||
|
||||
const items = await prisma.review.findMany({
|
||||
where: { bookId },
|
||||
skip: filter.offset ?? 0,
|
||||
take: filter.size ?? 10,
|
||||
orderBy: {
|
||||
createdAt: 'desc'
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
items: await Promise.all(items.map(value => toReviewSchema(value))),
|
||||
total: await prisma.review.count({ where: { bookId } })
|
||||
};
|
||||
}
|
||||
0
app/server/graphql/resolvers/reviewUpdateResolver.ts
Normal file
0
app/server/graphql/resolvers/reviewUpdateResolver.ts
Normal file
|
|
@ -97,6 +97,13 @@ type Book {
|
|||
personalRating: RatingType!
|
||||
}
|
||||
|
||||
input BookUpdate {
|
||||
id: ID!
|
||||
title: String
|
||||
description: String
|
||||
progressStatus: WorkingProgressStatus
|
||||
}
|
||||
|
||||
type BookChapter {
|
||||
id: ID!
|
||||
title: String!
|
||||
|
|
@ -144,6 +151,8 @@ type Review {
|
|||
author: User!
|
||||
text: String!
|
||||
ratingTotal: Int!
|
||||
createdAt: String!
|
||||
updatedAt: String!
|
||||
}
|
||||
|
||||
input ReviewCreateInput {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue