-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement create reply repository method
- Loading branch information
Showing
2 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
api/src/infrastructure/repositories/postgresql/reply.repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Reply } from '@src/core/entities/review.entity'; | ||
import { | ||
CreateReplyParams, | ||
ReplyRepository | ||
} from '@src/core/ports/reply.repository'; | ||
import { AppErrorCode, CustomError } from '@src/error/errors'; | ||
import { | ||
ExtendedPrismaClient, | ||
ExtendedPrismaTransactionClient | ||
} from '@src/infrastructure/prisma/types'; | ||
|
||
export class PostgresqlReplyRepository implements Partial<ReplyRepository> { | ||
constructor(private readonly client: ExtendedPrismaClient) {} | ||
|
||
public create = async ( | ||
params: CreateReplyParams, | ||
txClient?: ExtendedPrismaTransactionClient | ||
): Promise<Reply> => { | ||
const createdAt = new Date(); | ||
try { | ||
const client = txClient ?? this.client; | ||
const reply = await client.reply.create({ | ||
data: { | ||
reviewId: params.reviewId, | ||
userId: params.userId, | ||
content: params.content, | ||
createdAt, | ||
updatedAt: createdAt | ||
} | ||
}); | ||
|
||
return reply.convertToEntity(); | ||
} catch (error: unknown) { | ||
throw new CustomError({ | ||
code: AppErrorCode.INTERNAL_ERROR, | ||
cause: error, | ||
message: 'failed to create reply', | ||
context: { params } | ||
}); | ||
} | ||
}; | ||
} |
95 changes: 95 additions & 0 deletions
95
api/test/infrastructure/repositories/postgresql/reply.repository.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { AccessLevel, Idp } from '@prisma/client'; | ||
import { randomUUID } from 'crypto'; | ||
import seedrandom from 'seedrandom'; | ||
import { Logger } from 'winston'; | ||
|
||
import { | ||
generateUserNickname, | ||
generateUserTag | ||
} from '@src/core/nickname.generator'; | ||
import { | ||
CreateReplyParams, | ||
} from '@src/core/ports/reply.repository'; | ||
import { generatePrismaClient } from '@src/infrastructure/prisma/prisma.client'; | ||
import { ExtendedPrismaClient } from '@src/infrastructure/prisma/types'; | ||
import { PostgresqlReplyRepository } from '@src/infrastructure/repositories/postgresql/reply.repository'; | ||
|
||
function generateRandomNumber(userId: string): number { | ||
const rand = seedrandom(userId); | ||
return Math.floor(rand() * 100000); | ||
} | ||
|
||
describe('Test reply repository', () => { | ||
const logger: Partial<Logger> = { | ||
error: jest.fn() | ||
}; | ||
let prismaClient: ExtendedPrismaClient; | ||
let replyRepository: PostgresqlReplyRepository; | ||
|
||
beforeAll(() => { | ||
const config = { | ||
host: '127.0.0.1', | ||
port: 5435, | ||
user: 'mrc-client', | ||
password: 'Client123!' | ||
}; | ||
prismaClient = generatePrismaClient(logger as Logger, config); | ||
replyRepository = new PostgresqlReplyRepository(prismaClient); | ||
}); | ||
|
||
afterAll(async () => { | ||
await prismaClient.$disconnect(); | ||
}); | ||
|
||
describe('Test create', () => { | ||
const userId = randomUUID(); | ||
const reviewId = generateRandomNumber(userId); | ||
const content = 'randomContent'; | ||
const createdAt = new Date(); | ||
|
||
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 | ||
} | ||
}); | ||
await prismaClient.review.create({ | ||
data: { | ||
id: reviewId, | ||
userId, | ||
title: 'randomTitle', | ||
movieName: 'randomMovieName', | ||
content, | ||
createdAt, | ||
updatedAt: createdAt | ||
} | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await prismaClient.review.delete({ where: { id: reviewId } }); | ||
await prismaClient.user.delete({ where: { id: userId } }); | ||
}); | ||
|
||
it('should success when valid', async () => { | ||
const params: CreateReplyParams = { | ||
reviewId, | ||
userId, | ||
content | ||
}; | ||
|
||
const replyCreated = await replyRepository.create(params); | ||
|
||
expect(replyCreated.getData()).toEqual( | ||
expect.objectContaining({ reviewId, userId, content }) | ||
); | ||
}); | ||
}); | ||
}); |