Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#164] Implement find many and count review repository method #239

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { Prisma } from '@prisma/client';

import { Review } from '@src/core/entities/review.entity';
import {
CreateReviewParams,
FindManyAndCountResponse,
FindReviewsParams,
ReviewRepository
} from '@src/core/ports/review.repository';
import { Direction, SortBy } from '@src/core/services/review/types';
import { AppErrorCode, CustomError } from '@src/error/errors';
import {
PrismaErrorCode,
Expand All @@ -13,6 +18,17 @@ import {
ExtendedPrismaTransactionClient
} from '@src/infrastructure/prisma/types';

type OrderBy =
| {
movieName: Direction;
}
| { createdAt: Direction };

type SortingCriteria = {
sortBy: SortBy;
direction: Direction;
};

export class PostgresqlReviewRepository implements Partial<ReviewRepository> {
constructor(private readonly client: ExtendedPrismaClient) {}

Expand Down Expand Up @@ -99,6 +115,67 @@ export class PostgresqlReviewRepository implements Partial<ReviewRepository> {
}
};

public findManyAndCount = async (
params: FindReviewsParams,
txClient?: ExtendedPrismaTransactionClient
): Promise<FindManyAndCountResponse> => {
try {
const client = txClient ?? this.client;
const args: Prisma.ReviewFindManyArgs = {
skip: (params.pageOffset - 1) * params.pageSize,
take: params.pageSize,
where: {
AND: [
{ user: { nickname: { contains: params.nickname } } },
{ title: { contains: params.title, mode: 'insensitive' } },
{
movieName: { contains: params.movieName, mode: 'insensitive' }
}
]
},
orderBy: this.convertToOrderBy(params)
};

const reviewResults = await client.review.findMany({
skip: args.skip,
take: args.take,
where: args.where,
include: {
_count: {
select: { Reply: true }
}
},
orderBy: args.orderBy
});
const reviewCount = await client.review.count({ where: args.where });

const reviews = reviewResults.map((review) =>
this.convertToEntity(
review.id,
review.userId,
review.title,
review.movieName,
review.content,
review._count.Reply,
review.createdAt,
review.updatedAt
)
);

return {
reviews,
reviewCount
};
} catch (error: unknown) {
throw new CustomError({
code: AppErrorCode.INTERNAL_ERROR,
cause: error,
message: 'failed to find many reviews and count',
context: { params }
});
}
};

private convertToEntity = (
id: number,
userId: string,
Expand All @@ -120,4 +197,17 @@ export class PostgresqlReviewRepository implements Partial<ReviewRepository> {
updatedAt
);
};

private convertToOrderBy = (criteria: SortingCriteria): OrderBy => {
switch (criteria.sortBy) {
case 'movieName':
return {
movieName: criteria.direction
};
case 'createdAt':
return {
createdAt: criteria.direction
};
}
};
}