From 409850b22a0c8e5f2448f38ad357b2dfacbfb97b Mon Sep 17 00:00:00 2001 From: Philipinho <16838612+Philipinho@users.noreply.github.com> Date: Thu, 4 Apr 2024 21:24:55 +0100 Subject: [PATCH] Fix comment --- .../src/core/comment/comment.controller.ts | 17 ++++---- .../src/core/comment/comment.service.ts | 39 +++++++++++++++---- .../src/core/comment/dto/comments.input.ts | 6 +-- .../core/comment/dto/create-comment.dto.ts | 4 -- .../core/comment/dto/update-comment.dto.ts | 2 +- .../src/kysely/repos/comment/comment.repo.ts | 28 ++++++++++--- 6 files changed, 68 insertions(+), 28 deletions(-) diff --git a/apps/server/src/core/comment/comment.controller.ts b/apps/server/src/core/comment/comment.controller.ts index c7611a40..c05b9a02 100644 --- a/apps/server/src/core/comment/comment.controller.ts +++ b/apps/server/src/core/comment/comment.controller.ts @@ -9,7 +9,7 @@ import { import { CommentService } from './comment.service'; import { CreateCommentDto } from './dto/create-comment.dto'; import { UpdateCommentDto } from './dto/update-comment.dto'; -import { CommentsInput, SingleCommentInput } from './dto/comments.input'; +import { PageIdDto, CommentIdDto } from './dto/comments.input'; import { AuthUser } from '../../decorators/auth-user.decorator'; import { AuthWorkspace } from '../../decorators/auth-workspace.decorator'; import { JwtAuthGuard } from '../../guards/jwt-auth.guard'; @@ -34,7 +34,7 @@ export class CommentController { @HttpCode(HttpStatus.OK) @Post() findPageComments( - @Body() input: CommentsInput, + @Body() input: PageIdDto, @Body() pagination: PaginationOptions, //@AuthUser() user: User, @@ -45,19 +45,22 @@ export class CommentController { @HttpCode(HttpStatus.OK) @Post('info') - findOne(@Body() input: SingleCommentInput) { - return this.commentService.findWithCreator(input.id); + findOne(@Body() input: CommentIdDto) { + return this.commentService.findById(input.commentId); } @HttpCode(HttpStatus.OK) @Post('update') update(@Body() updateCommentDto: UpdateCommentDto) { - return this.commentService.update(updateCommentDto.id, updateCommentDto); + return this.commentService.update( + updateCommentDto.commentId, + updateCommentDto, + ); } @HttpCode(HttpStatus.OK) @Post('delete') - remove(@Body() input: SingleCommentInput) { - return this.commentService.remove(input.id); + remove(@Body() input: CommentIdDto) { + return this.commentService.remove(input.commentId); } } diff --git a/apps/server/src/core/comment/comment.service.ts b/apps/server/src/core/comment/comment.service.ts index 2991ed56..78b1b8e0 100644 --- a/apps/server/src/core/comment/comment.service.ts +++ b/apps/server/src/core/comment/comment.service.ts @@ -1,4 +1,8 @@ -import { BadRequestException, Injectable } from '@nestjs/common'; +import { + BadRequestException, + Injectable, + NotFoundException, +} from '@nestjs/common'; import { CreateCommentDto } from './dto/create-comment.dto'; import { UpdateCommentDto } from './dto/update-comment.dto'; import { PageService } from '../page/services/page.service'; @@ -14,8 +18,14 @@ export class CommentService { private pageService: PageService, ) {} - async findWithCreator(commentId: string) { - // todo: find comment with creator object + async findById(commentId: string) { + const comment = this.commentRepo.findById(commentId, { + includeCreator: true, + }); + if (!comment) { + throw new NotFoundException('Comment not found'); + } + return comment; } async create( @@ -55,15 +65,21 @@ export class CommentService { creatorId: userId, workspaceId: workspaceId, }); - // todo return created comment and creator relation - return createdComment; + // return created comment and creator relation + return this.findById(createdComment.id); } async findByPageId( pageId: string, pagination: PaginationOptions, ): Promise> { + const page = await this.pageService.findById(pageId); + + if (!page) { + throw new BadRequestException('Page not found'); + } + const pageComments = await this.commentRepo.findPageComments( pageId, pagination, @@ -78,15 +94,24 @@ export class CommentService { ): Promise { const commentContent = JSON.parse(updateCommentDto.content); + const comment = await this.commentRepo.findById(commentId); + if (!comment) { + throw new NotFoundException('Comment not found'); + } + + const editedAt = new Date(); + await this.commentRepo.updateComment( { content: commentContent, - editedAt: new Date(), + editedAt: editedAt, }, commentId, ); + comment.content = commentContent; + comment.editedAt = editedAt; - return this.commentRepo.findById(commentId); + return comment; } async remove(id: string): Promise { diff --git a/apps/server/src/core/comment/dto/comments.input.ts b/apps/server/src/core/comment/dto/comments.input.ts index 6aadebbb..4fb21e56 100644 --- a/apps/server/src/core/comment/dto/comments.input.ts +++ b/apps/server/src/core/comment/dto/comments.input.ts @@ -1,11 +1,11 @@ import { IsUUID } from 'class-validator'; -export class CommentsInput { +export class PageIdDto { @IsUUID() pageId: string; } -export class SingleCommentInput { +export class CommentIdDto { @IsUUID() - id: string; + commentId: string; } diff --git a/apps/server/src/core/comment/dto/create-comment.dto.ts b/apps/server/src/core/comment/dto/create-comment.dto.ts index 8377e32f..87b680f1 100644 --- a/apps/server/src/core/comment/dto/create-comment.dto.ts +++ b/apps/server/src/core/comment/dto/create-comment.dto.ts @@ -1,10 +1,6 @@ import { IsJSON, IsOptional, IsString, IsUUID } from 'class-validator'; export class CreateCommentDto { - @IsOptional() - @IsUUID() - id?: string; - @IsUUID() pageId: string; diff --git a/apps/server/src/core/comment/dto/update-comment.dto.ts b/apps/server/src/core/comment/dto/update-comment.dto.ts index bfd109ee..bfd1474d 100644 --- a/apps/server/src/core/comment/dto/update-comment.dto.ts +++ b/apps/server/src/core/comment/dto/update-comment.dto.ts @@ -2,7 +2,7 @@ import { IsJSON, IsUUID } from 'class-validator'; export class UpdateCommentDto { @IsUUID() - id: string; + commentId: string; @IsJSON() content: any; diff --git a/apps/server/src/kysely/repos/comment/comment.repo.ts b/apps/server/src/kysely/repos/comment/comment.repo.ts index 6646f903..8f957f42 100644 --- a/apps/server/src/kysely/repos/comment/comment.repo.ts +++ b/apps/server/src/kysely/repos/comment/comment.repo.ts @@ -9,16 +9,23 @@ import { } from '@docmost/db/types/entity.types'; import { PaginationOptions } from '@docmost/db/pagination/pagination-options'; import { executeWithPagination } from '@docmost/db/pagination/pagination'; +import { ExpressionBuilder } from 'kysely'; +import { DB } from '@docmost/db/types/db'; +import { jsonObjectFrom } from 'kysely/helpers/postgres'; @Injectable() export class CommentRepo { constructor(@InjectKysely() private readonly db: KyselyDB) {} // todo, add workspaceId - async findById(commentId: string): Promise { + async findById( + commentId: string, + opts?: { includeCreator: boolean }, + ): Promise { return await this.db .selectFrom('comments') - .selectAll() + .selectAll('comments') + .$if(opts?.includeCreator, (qb) => qb.select(this.withCreator)) .where('id', '=', commentId) .executeTakeFirst(); } @@ -26,7 +33,8 @@ export class CommentRepo { async findPageComments(pageId: string, pagination: PaginationOptions) { const query = this.db .selectFrom('comments') - .selectAll() + .selectAll('comments') + .select((eb) => this.withCreator(eb)) .where('pageId', '=', pageId) .orderBy('createdAt', 'asc'); @@ -44,8 +52,8 @@ export class CommentRepo { trx?: KyselyTransaction, ) { const db = dbOrTx(this.db, trx); - - db.updateTable('comments') + await db + .updateTable('comments') .set(updatableComment) .where('id', '=', commentId) .execute(); @@ -56,7 +64,6 @@ export class CommentRepo { trx?: KyselyTransaction, ): Promise { const db = dbOrTx(this.db, trx); - return db .insertInto('comments') .values(insertableComment) @@ -64,6 +71,15 @@ export class CommentRepo { .executeTakeFirst(); } + withCreator(eb: ExpressionBuilder) { + return jsonObjectFrom( + eb + .selectFrom('users') + .select(['users.id', 'users.name', 'users.avatarUrl']) + .whereRef('users.id', '=', 'comments.creatorId'), + ).as('creator'); + } + async deleteComment(commentId: string): Promise { await this.db.deleteFrom('comments').where('id', '=', commentId).execute(); }