Fix comment

This commit is contained in:
Philipinho
2024-04-04 21:24:55 +01:00
parent 7d14a353cc
commit 409850b22a
6 changed files with 68 additions and 28 deletions

View File

@ -9,7 +9,7 @@ import {
import { CommentService } from './comment.service'; import { CommentService } from './comment.service';
import { CreateCommentDto } from './dto/create-comment.dto'; import { CreateCommentDto } from './dto/create-comment.dto';
import { UpdateCommentDto } from './dto/update-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 { AuthUser } from '../../decorators/auth-user.decorator';
import { AuthWorkspace } from '../../decorators/auth-workspace.decorator'; import { AuthWorkspace } from '../../decorators/auth-workspace.decorator';
import { JwtAuthGuard } from '../../guards/jwt-auth.guard'; import { JwtAuthGuard } from '../../guards/jwt-auth.guard';
@ -34,7 +34,7 @@ export class CommentController {
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
@Post() @Post()
findPageComments( findPageComments(
@Body() input: CommentsInput, @Body() input: PageIdDto,
@Body() @Body()
pagination: PaginationOptions, pagination: PaginationOptions,
//@AuthUser() user: User, //@AuthUser() user: User,
@ -45,19 +45,22 @@ export class CommentController {
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
@Post('info') @Post('info')
findOne(@Body() input: SingleCommentInput) { findOne(@Body() input: CommentIdDto) {
return this.commentService.findWithCreator(input.id); return this.commentService.findById(input.commentId);
} }
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
@Post('update') @Post('update')
update(@Body() updateCommentDto: UpdateCommentDto) { update(@Body() updateCommentDto: UpdateCommentDto) {
return this.commentService.update(updateCommentDto.id, updateCommentDto); return this.commentService.update(
updateCommentDto.commentId,
updateCommentDto,
);
} }
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
@Post('delete') @Post('delete')
remove(@Body() input: SingleCommentInput) { remove(@Body() input: CommentIdDto) {
return this.commentService.remove(input.id); return this.commentService.remove(input.commentId);
} }
} }

View File

@ -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 { CreateCommentDto } from './dto/create-comment.dto';
import { UpdateCommentDto } from './dto/update-comment.dto'; import { UpdateCommentDto } from './dto/update-comment.dto';
import { PageService } from '../page/services/page.service'; import { PageService } from '../page/services/page.service';
@ -14,8 +18,14 @@ export class CommentService {
private pageService: PageService, private pageService: PageService,
) {} ) {}
async findWithCreator(commentId: string) { async findById(commentId: string) {
// todo: find comment with creator object const comment = this.commentRepo.findById(commentId, {
includeCreator: true,
});
if (!comment) {
throw new NotFoundException('Comment not found');
}
return comment;
} }
async create( async create(
@ -55,15 +65,21 @@ export class CommentService {
creatorId: userId, creatorId: userId,
workspaceId: workspaceId, workspaceId: workspaceId,
}); });
// todo return created comment and creator relation
return createdComment; // return created comment and creator relation
return this.findById(createdComment.id);
} }
async findByPageId( async findByPageId(
pageId: string, pageId: string,
pagination: PaginationOptions, pagination: PaginationOptions,
): Promise<PaginationResult<Comment>> { ): Promise<PaginationResult<Comment>> {
const page = await this.pageService.findById(pageId);
if (!page) {
throw new BadRequestException('Page not found');
}
const pageComments = await this.commentRepo.findPageComments( const pageComments = await this.commentRepo.findPageComments(
pageId, pageId,
pagination, pagination,
@ -78,15 +94,24 @@ export class CommentService {
): Promise<Comment> { ): Promise<Comment> {
const commentContent = JSON.parse(updateCommentDto.content); 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( await this.commentRepo.updateComment(
{ {
content: commentContent, content: commentContent,
editedAt: new Date(), editedAt: editedAt,
}, },
commentId, commentId,
); );
comment.content = commentContent;
comment.editedAt = editedAt;
return this.commentRepo.findById(commentId); return comment;
} }
async remove(id: string): Promise<void> { async remove(id: string): Promise<void> {

View File

@ -1,11 +1,11 @@
import { IsUUID } from 'class-validator'; import { IsUUID } from 'class-validator';
export class CommentsInput { export class PageIdDto {
@IsUUID() @IsUUID()
pageId: string; pageId: string;
} }
export class SingleCommentInput { export class CommentIdDto {
@IsUUID() @IsUUID()
id: string; commentId: string;
} }

View File

@ -1,10 +1,6 @@
import { IsJSON, IsOptional, IsString, IsUUID } from 'class-validator'; import { IsJSON, IsOptional, IsString, IsUUID } from 'class-validator';
export class CreateCommentDto { export class CreateCommentDto {
@IsOptional()
@IsUUID()
id?: string;
@IsUUID() @IsUUID()
pageId: string; pageId: string;

View File

@ -2,7 +2,7 @@ import { IsJSON, IsUUID } from 'class-validator';
export class UpdateCommentDto { export class UpdateCommentDto {
@IsUUID() @IsUUID()
id: string; commentId: string;
@IsJSON() @IsJSON()
content: any; content: any;

View File

@ -9,16 +9,23 @@ import {
} from '@docmost/db/types/entity.types'; } from '@docmost/db/types/entity.types';
import { PaginationOptions } from '@docmost/db/pagination/pagination-options'; import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
import { executeWithPagination } from '@docmost/db/pagination/pagination'; 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() @Injectable()
export class CommentRepo { export class CommentRepo {
constructor(@InjectKysely() private readonly db: KyselyDB) {} constructor(@InjectKysely() private readonly db: KyselyDB) {}
// todo, add workspaceId // todo, add workspaceId
async findById(commentId: string): Promise<Comment> { async findById(
commentId: string,
opts?: { includeCreator: boolean },
): Promise<Comment> {
return await this.db return await this.db
.selectFrom('comments') .selectFrom('comments')
.selectAll() .selectAll('comments')
.$if(opts?.includeCreator, (qb) => qb.select(this.withCreator))
.where('id', '=', commentId) .where('id', '=', commentId)
.executeTakeFirst(); .executeTakeFirst();
} }
@ -26,7 +33,8 @@ export class CommentRepo {
async findPageComments(pageId: string, pagination: PaginationOptions) { async findPageComments(pageId: string, pagination: PaginationOptions) {
const query = this.db const query = this.db
.selectFrom('comments') .selectFrom('comments')
.selectAll() .selectAll('comments')
.select((eb) => this.withCreator(eb))
.where('pageId', '=', pageId) .where('pageId', '=', pageId)
.orderBy('createdAt', 'asc'); .orderBy('createdAt', 'asc');
@ -44,8 +52,8 @@ export class CommentRepo {
trx?: KyselyTransaction, trx?: KyselyTransaction,
) { ) {
const db = dbOrTx(this.db, trx); const db = dbOrTx(this.db, trx);
await db
db.updateTable('comments') .updateTable('comments')
.set(updatableComment) .set(updatableComment)
.where('id', '=', commentId) .where('id', '=', commentId)
.execute(); .execute();
@ -56,7 +64,6 @@ export class CommentRepo {
trx?: KyselyTransaction, trx?: KyselyTransaction,
): Promise<Comment> { ): Promise<Comment> {
const db = dbOrTx(this.db, trx); const db = dbOrTx(this.db, trx);
return db return db
.insertInto('comments') .insertInto('comments')
.values(insertableComment) .values(insertableComment)
@ -64,6 +71,15 @@ export class CommentRepo {
.executeTakeFirst(); .executeTakeFirst();
} }
withCreator(eb: ExpressionBuilder<DB, 'comments'>) {
return jsonObjectFrom(
eb
.selectFrom('users')
.select(['users.id', 'users.name', 'users.avatarUrl'])
.whereRef('users.id', '=', 'comments.creatorId'),
).as('creator');
}
async deleteComment(commentId: string): Promise<void> { async deleteComment(commentId: string): Promise<void> {
await this.db.deleteFrom('comments').where('id', '=', commentId).execute(); await this.db.deleteFrom('comments').where('id', '=', commentId).execute();
} }