Skip to content
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
@@ -0,0 +1,9 @@
-- AlterTable
ALTER TABLE "Comment" ADD COLUMN "isSpoiler" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN "parentId" TEXT;

-- CreateIndex
CREATE INDEX "Comment_parentId_idx" ON "Comment"("parentId");

-- AddForeignKey
ALTER TABLE "Comment" ADD CONSTRAINT "Comment_parentId_fkey" FOREIGN KEY ("parentId") REFERENCES "Comment"("id") ON DELETE CASCADE ON UPDATE CASCADE;
5 changes: 5 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ model Comment {
id String @id @default(uuid(7))
content String
type CommentType
isSpoiler Boolean @default(false)
userId String
parentId String?
animeId String?
mangaId String?
tvShowId String?
Expand All @@ -200,6 +202,8 @@ model Comment {
updatedAt DateTime @updatedAt

user User @relation(fields: [userId], references: [id], onDelete: Cascade)
parent Comment? @relation("CommentReplies", fields: [parentId], references: [id], onDelete: Cascade)
replies Comment[] @relation("CommentReplies")
anime Anime? @relation(fields: [animeId], references: [id], onDelete: Cascade)
manga Manga? @relation(fields: [mangaId], references: [id], onDelete: Cascade)
tvShow TvShow? @relation(fields: [tvShowId], references: [id], onDelete: Cascade)
Expand All @@ -218,6 +222,7 @@ model Comment {
@@index([gameId])
@@index([bookId])
@@index([profileId])
@@index([parentId])
}

enum ReactionType {
Expand Down
17 changes: 17 additions & 0 deletions src/modules/comment/dto/create-comment.dto.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ApiProperty } from "@nestjs/swagger";
import { CommentType } from "@prisma/generated/enums";
import {
IsBoolean,
IsEnum,
IsNotEmpty,
IsOptional,
IsUUID,
MaxLength,
registerDecorator,
ValidationArguments,
Expand Down Expand Up @@ -48,6 +50,21 @@ export class CreateCommentDto {
@ApiProperty({ type: "string", maxLength: 500 })
readonly content: string;

@IsOptional()
@IsBoolean()
@ApiProperty({ type: "boolean", required: false, default: false })
readonly isSpoiler?: boolean;

@IsOptional()
@IsUUID()
@ApiProperty({
type: "string",
format: "uuid",
required: false,
description: "Parent comment id when replying",
})
readonly parentId?: string;

@IsOptional()
@CommentRequiredForType(CommentType.Anime)
@ApiProperty({
Expand Down
61 changes: 37 additions & 24 deletions src/modules/comment/service/comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,50 @@ export class CommentService {
}

async getComments({ page, itemsPerPage, ...getCommentsDto }: GetCommentsDto) {
const userSelect = {
id: true,
name: true,
username: true,
profile: {
select: {
id: true,
avatarUrl: true,
},
},
} as const;

const reactionsInclude = {
orderBy: { createdAt: "desc" },
select: {
id: true,
emoji: true,
createdAt: true,
user: {
select: {
id: true,
username: true,
},
},
},
} as const;

const pagination = await this.databaseService.offsetPagination<CommentFindManyArgs>({
model: "comment",
where: { ...getCommentsDto },
where: { ...getCommentsDto, parentId: null },
page,
itemsPerPage,
orderBy: { createdAt: "desc" },
include: {
user: {
select: {
id: true,
name: true,
username: true,
profile: {
select: {
id: true,
avatarUrl: true,
},
select: userSelect,
},
replies: {
orderBy: { createdAt: "asc" },
include: {
user: {
select: userSelect,
},
reactions: reactionsInclude,
},
},
anime: {
Expand Down Expand Up @@ -121,20 +147,7 @@ export class CommentService {
imageUrl: true,
},
},
reactions: {
take: 3,
orderBy: { createdAt: "desc" },
select: {
id: true,
emoji: true,
createdAt: true,
user: {
select: {
username: true,
},
},
},
},
reactions: reactionsInclude,
},
});

Expand Down