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 by ID review repository method #237

Merged
Show file tree
Hide file tree
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
Expand Up @@ -4,6 +4,10 @@ import {
ReviewRepository
} from '@src/core/ports/review.repository';
import { AppErrorCode, CustomError } from '@src/error/errors';
import {
PrismaErrorCode,
isErrorWithCode
} from '@src/infrastructure/prisma/errors';
import {
ExtendedPrismaClient,
ExtendedPrismaTransactionClient
Expand Down Expand Up @@ -50,6 +54,51 @@ export class PostgresqlReviewRepository implements Partial<ReviewRepository> {
}
};

public findById = async (
id: number,
txClient?: ExtendedPrismaTransactionClient
): Promise<Review> => {
try {
const client = txClient ?? this.client;
const review = await client.review.findFirstOrThrow({
where: { id },
include: {
_count: {
select: { Reply: true }
}
}
});

return this.convertToEntity(
review.id,
review.userId,
review.title,
review.movieName,
review.content,
review._count.Reply,
review.createdAt,
review.updatedAt
);
} catch (error: unknown) {
if (
isErrorWithCode(error) &&
error.code === PrismaErrorCode.RECORD_NOT_FOUND
) {
throw new CustomError({
code: AppErrorCode.NOT_FOUND,
message: 'review not found',
context: { id }
});
}
throw new CustomError({
code: AppErrorCode.INTERNAL_ERROR,
cause: error,
message: 'failed to find review by ID',
context: { id }
});
}
};

private convertToEntity = (
id: number,
userId: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
generateUserTag
} from '@src/core/nickname.generator';
import { CreateReviewParams } from '@src/core/ports/review.repository';
import { AppErrorCode, CustomError } from '@src/error/errors';
import { generatePrismaClient } from '@src/infrastructure/prisma/prisma.client';
import { ExtendedPrismaClient } from '@src/infrastructure/prisma/types';
import { PostgresqlReviewRepository } from '@src/infrastructure/repositories/postgresql/review.repository';
Expand Down Expand Up @@ -77,4 +78,67 @@ describe('Test review repository', () => {
);
});
});

describe('Test find by ID', () => {
const userId = randomUUID();
const createdAt = new Date();
let reviewCreated: Review;

beforeAll(async () => {
await prismaClient.user.create({
data: {
id: userId,
nickname: generateUserNickname(userId),
tag: generateUserTag(userId),
idp: Idp.GOOGLE,
email: `${userId}@gmail.com`,
accessLevel: AccessLevel.USER,
createdAt,
updatedAt: createdAt
}
});
const reviewResultCreated = await prismaClient.review.create({
data: {
userId,
title: 'randomTitle',
movieName: 'randomMovieName',
content: 'randomContent',
createdAt,
updatedAt: createdAt
}
});
reviewCreated = reviewRepository['convertToEntity'](
reviewResultCreated.id,
reviewResultCreated.userId,
reviewResultCreated.title,
reviewResultCreated.movieName,
reviewResultCreated.content,
0,
reviewResultCreated.createdAt,
reviewResultCreated.updatedAt
);
});

afterAll(async () => {
await prismaClient.review.delete({ where: { id: reviewCreated.id } });
await prismaClient.user.delete({ where: { id: userId } });
});

it('should success when valid', async () => {
const reviewFound = await reviewRepository.findById(reviewCreated.id);

expect(JSON.stringify(reviewFound)).toEqual(
JSON.stringify(reviewCreated)
);
});

it('should fail when no existing review is found for the given ID', async () => {
try {
await reviewRepository.findById(reviewCreated.id + 1);
} catch (error: unknown) {
expect(error).toBeInstanceOf(CustomError);
expect(error).toHaveProperty('code', AppErrorCode.NOT_FOUND);
}
});
});
});