Skip to content

Commit

Permalink
Merge pull request #247 from skgndi12/feature/issue-164/implement-del…
Browse files Browse the repository at this point in the history
…ete-by-id-review-repository-method

[#164] Implement delete by ID review repository method
  • Loading branch information
skgndi12 authored Mar 11, 2024
2 parents 5cbc23d + 1668798 commit a378162
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type SortingCriteria = {
direction: Direction;
};

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

public create = async (
Expand Down Expand Up @@ -229,6 +229,33 @@ export class PostgresqlReviewRepository implements Partial<ReviewRepository> {
}
};

public deleteById = async (
id: number,
txClient?: ExtendedPrismaTransactionClient
): Promise<void> => {
try {
const client = txClient ?? this.client;
await client.review.delete({ where: { id } });
} catch (error: unknown) {
if (
isErrorWithCode(error) &&
error.code === PrismaErrorCode.RECORD_NOT_FOUND
) {
throw new CustomError({
code: AppErrorCode.NOT_FOUND,
message: 'review not found for deletion',
context: { id }
});
}
throw new CustomError({
code: AppErrorCode.INTERNAL_ERROR,
cause: error,
message: 'failed to delete 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 @@ -15,6 +15,7 @@ import {
} from '@src/core/ports/review.repository';
import { AccessLevelEnum, IdpEnum } from '@src/core/types';
import { AppErrorCode, CustomError } from '@src/error/errors';
import { PrismaErrorCode } from '@src/infrastructure/prisma/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 @@ -665,4 +666,96 @@ describe('Test review repository', () => {
}
});
});

describe('Test delete by ID', () => {
const userId = randomUUID();
const content = 'randomContent';
const createdAt = new Date();
let reviewCreated: Review;
const replyCount = 10;

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

beforeEach(async () => {
const reviewResultCreated = await prismaClient.review.create({
data: {
userId,
title: 'randomTitle',
movieName: 'randomMovieName',
content,
createdAt,
updatedAt: createdAt
}
});
reviewCreated = reviewRepository['convertToEntity'](
reviewResultCreated.id,
reviewResultCreated.userId,
reviewResultCreated.title,
reviewResultCreated.movieName,
reviewResultCreated.content,
0,
reviewResultCreated.createdAt,
reviewResultCreated.updatedAt
);

for (let i = 1; i <= replyCount; i++) {
await prismaClient.reply.create({
data: {
reviewId: reviewResultCreated.id,
userId,
content,
createdAt,
updatedAt: createdAt
}
});
}
});

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

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

try {
await prismaClient.review.findFirstOrThrow({
where: { id: reviewCreated.id }
});
} catch (error: unknown) {
expect(error).toHaveProperty('code', PrismaErrorCode.RECORD_NOT_FOUND);
}

const replies = await prismaClient.reply.findMany({
where: { reviewId: reviewCreated.id }
});

expect(replies).toEqual([]);
});

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

0 comments on commit a378162

Please sign in to comment.