implemented ratings, fixes
This commit is contained in:
parent
4801cf3a13
commit
d1da980501
31 changed files with 775 additions and 236 deletions
|
|
@ -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;
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue