Skip to content

Commit

Permalink
Merge pull request #126 from game-node-app/dev
Browse files Browse the repository at this point in the history
Fixes and improvements in the new deep nested comments sytem
  • Loading branch information
Lamarcke authored Dec 4, 2024
2 parents 8ff49b8 + b12ef6d commit 3936465
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion server_swagger.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/comment/comment.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export class CommentController {
@HttpCode(HttpStatus.OK)
@UseInterceptors(PaginationInterceptor)
async findAll(@Body() dto: FindAllCommentsDto) {
return this.commentService.findAll(dto);
const result = await this.commentService.findAll(dto);
return result;
}

@Get(":sourceType/:id")
Expand Down
4 changes: 2 additions & 2 deletions src/comment/comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class CommentService {
},
relations: {
// Includes comments of comments in a list in each element
childOf: true,
parentOf: true,
},
});
case CommentSourceType.ACTIVITY:
Expand All @@ -74,7 +74,7 @@ export class CommentService {
},
relations: {
// Includes comments of comments in a list in each element
childOf: true,
parentOf: true,
},
});
default:
Expand Down
2 changes: 1 addition & 1 deletion src/comment/comment.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import { UserComment } from "./entity/user-comment.entity";
* Only one level of "children" is allowed for performance reasons.
*/
export interface ThreadEnabledComment<T extends UserComment> {
childOf: T | null;
childOf: T[] | null;
childOfId: string | null;
}
8 changes: 8 additions & 0 deletions src/comment/dto/find-comments-paginated-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ import {
} from "../../utils/pagination/pagination-response.dto";
import { ReviewComment } from "../entity/review-comment.entity";
import { ActivityComment } from "../entity/activity-comment.entity";
import { ApiProperty, getSchemaPath } from "@nestjs/swagger";

export class FindCommentsPaginatedResponseDto extends PaginationResponseDto {
@ApiProperty({
type: "array",
oneOf: [
{ $ref: getSchemaPath(ReviewComment) },
{ $ref: getSchemaPath(ActivityComment) },
],
})
data: ReviewComment[] | ActivityComment[] = [];
pagination: PaginationInfo = new PaginationInfo();
}
16 changes: 11 additions & 5 deletions src/comment/entity/activity-comment.entity.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { UserComment } from "./user-comment.entity";
import { Column, Entity, Index, ManyToOne } from "typeorm";
import { Column, Entity, Index, ManyToOne, OneToMany } from "typeorm";
import { Activity } from "../../activities/activities-repository/entities/activity.entity";
import { ReviewComment } from "./review-comment.entity";
import { ThreadEnabledComment } from "../comment.types";

@Entity()
@Index(["profile", "activity"])
export class ActivityComment extends UserComment {
export class ActivityComment
extends UserComment
implements ThreadEnabledComment<ActivityComment>
{
@ManyToOne(() => Activity, {
nullable: false,
onDelete: "CASCADE",
Expand All @@ -16,11 +19,14 @@ export class ActivityComment extends UserComment {
})
activityId: string;

@ManyToOne(() => ReviewComment, {
@OneToMany(() => ActivityComment, (comment) => comment.childOf)
parentOf: ActivityComment[] | null;

@ManyToOne(() => ActivityComment, {
nullable: true,
onDelete: "CASCADE",
})
childOf: ReviewComment | null;
childOf: ActivityComment[] | null;
@Column({
nullable: true,
})
Expand Down
7 changes: 5 additions & 2 deletions src/comment/entity/review-comment.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UserComment } from "./user-comment.entity";
import { Column, Entity, Index, ManyToOne } from "typeorm";
import { Column, Entity, Index, ManyToOne, OneToMany } from "typeorm";
import { Review } from "../../reviews/entities/review.entity";
import { ThreadEnabledComment } from "../comment.types";

Expand All @@ -19,11 +19,14 @@ export class ReviewComment
})
reviewId: string;

@OneToMany(() => ReviewComment, (comment) => comment.childOf)
parentOf: ReviewComment[] | null;

@ManyToOne(() => ReviewComment, {
nullable: true,
onDelete: "CASCADE",
})
childOf: ReviewComment | null;
childOf: ReviewComment[] | null;
@Column({
nullable: true,
})
Expand Down

0 comments on commit 3936465

Please sign in to comment.