Skip to content

Commit adf3745

Browse files
committed
feat: persist attachments using Prisma repositories
1 parent f1aadf3 commit adf3745

File tree

4 files changed

+74
-10
lines changed

4 files changed

+74
-10
lines changed

src/infra/database/prisma/mappers/prisma-question-attachment-mapper.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Attachment as PrismaAttachment } from '@prisma/client'
1+
import { Prisma, Attachment as PrismaAttachment } from '@prisma/client'
22
import { UniqueEntityID } from '@/core/entities/unique-entity-id'
33
import { QuestionAttachment } from '@/domain/forum/enterprise/entities/question-attachment'
44

@@ -16,4 +16,23 @@ export class PrismaQuestionAttachmentMapper {
1616
new UniqueEntityID(raw.id),
1717
)
1818
}
19+
20+
static toPrismaUpdateMany(
21+
attachments: QuestionAttachment[],
22+
): Prisma.AttachmentUpdateManyArgs {
23+
const attachmentIds = attachments.map((attachment) => {
24+
return attachment.id.toString()
25+
})
26+
27+
return {
28+
where: {
29+
id: {
30+
in: attachmentIds,
31+
},
32+
},
33+
data: {
34+
questionId: attachments[0].questionId.toString(),
35+
},
36+
}
37+
}
1938
}

src/infra/database/prisma/repositories/prisma-question-attachments-repository.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,34 @@ export class PrismaQuestionAttachmentsRepository
2222
return questionAttachments.map(PrismaQuestionAttachmentMapper.toDomain)
2323
}
2424

25+
async createMany(attachments: QuestionAttachment[]): Promise<void> {
26+
if (attachments.length === 0) {
27+
return
28+
}
29+
30+
const data = PrismaQuestionAttachmentMapper.toPrismaUpdateMany(attachments)
31+
32+
await this.prisma.attachment.updateMany(data)
33+
}
34+
35+
async deleteMany(attachments: QuestionAttachment[]): Promise<void> {
36+
if (attachments.length === 0) {
37+
return
38+
}
39+
40+
const attachmentIds = attachments.map((attachment) => {
41+
return attachment.id.toString()
42+
})
43+
44+
await this.prisma.attachment.deleteMany({
45+
where: {
46+
id: {
47+
in: attachmentIds,
48+
},
49+
},
50+
})
51+
}
52+
2553
async deleteManyByQuestionId(questionId: string): Promise<void> {
2654
await this.prisma.attachment.deleteMany({
2755
where: {

src/infra/database/prisma/repositories/prisma-questions-repository.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ import { Question } from '@/domain/forum/enterprise/entities/question'
44
import { Injectable } from '@nestjs/common'
55
import { PrismaService } from '../prisma.service'
66
import { PrismaQuestionMapper } from '../mappers/prisma-question-mapper'
7+
import { QuestionAttachmentsRepository } from '@/domain/forum/application/repositories/question-attachments-repository'
78

89
@Injectable()
910
export class PrismaQuestionsRepository implements QuestionsRepository {
10-
constructor(private prisma: PrismaService) {}
11+
constructor(
12+
private prisma: PrismaService,
13+
private questionAttachmentsRepository: QuestionAttachmentsRepository,
14+
) {}
1115

1216
async findById(id: string): Promise<Question | null> {
1317
const question = await this.prisma.question.findUnique({
@@ -55,17 +59,29 @@ export class PrismaQuestionsRepository implements QuestionsRepository {
5559
await this.prisma.question.create({
5660
data,
5761
})
62+
63+
await this.questionAttachmentsRepository.createMany(
64+
question.attachments.getItems(),
65+
)
5866
}
5967

6068
async save(question: Question): Promise<void> {
6169
const data = PrismaQuestionMapper.toPrisma(question)
6270

63-
await this.prisma.question.update({
64-
where: {
65-
id: data.id,
66-
},
67-
data,
68-
})
71+
await Promise.all([
72+
this.prisma.question.update({
73+
where: {
74+
id: data.id,
75+
},
76+
data,
77+
}),
78+
this.questionAttachmentsRepository.createMany(
79+
question.attachments.getNewItems(),
80+
),
81+
this.questionAttachmentsRepository.deleteMany(
82+
question.attachments.getRemovedItems(),
83+
),
84+
])
6985
}
7086

7187
async delete(question: Question): Promise<void> {

src/infra/http/controllers/create-question.controller.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { CreateQuestionUseCase } from '@/domain/forum/application/use-cases/crea
88
const createQuestionBodySchema = z.object({
99
title: z.string(),
1010
content: z.string(),
11+
attachments: z.array(z.string().uuid()),
1112
})
1213

1314
const bodyValidationPipe = new ZodValidationPipe(createQuestionBodySchema)
@@ -23,14 +24,14 @@ export class CreateQuestionController {
2324
@Body(bodyValidationPipe) body: CreateQuestionBodySchema,
2425
@CurrentUser() user: UserPayload,
2526
) {
26-
const { title, content } = body
27+
const { title, content, attachments } = body
2728
const userId = user.sub
2829

2930
const result = await this.createQuestion.execute({
3031
title,
3132
content,
3233
authorId: userId,
33-
attachmentsIds: [],
34+
attachmentsIds: attachments,
3435
})
3536

3637
if (result.isLeft()) {

0 commit comments

Comments
 (0)