diff --git a/apps/backend/src/comment/comment.controller.ts b/apps/backend/src/comment/comment.controller.ts new file mode 100644 index 0000000..61fc9fa --- /dev/null +++ b/apps/backend/src/comment/comment.controller.ts @@ -0,0 +1,28 @@ +import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, UseGuards } from '@nestjs/common'; +import { CommentService } from './comment.service'; +import { CreateCommentDto } from './dto/create-comment.dto'; +import { Comment } from './entities/comment.entity'; + +import { AuthGuard } from '@nestjs/passport'; + +@Controller('posts/:postId') +export class CommentController { + constructor(private readonly commentService: CommentService) {} + + @Post('comments') + @UseGuards(AuthGuard('jwt')) + create(@Param('postId', ParseIntPipe) postId: number, @Body() createCommentDto: CreateCommentDto): Promise { + return this.commentService.create(postId, createCommentDto); + } + + @Delete('comments/:id') + @UseGuards(AuthGuard('jwt')) + remove(@Param('id', ParseIntPipe) id: number): Promise { + return this.commentService.remove(id); + } + + @Get('comments') + findAll(@Param('postId', ParseIntPipe) postId: number): Promise<{ comments: Comment[] }> { + return this.commentService.findAll(postId); + } +} diff --git a/apps/backend/src/comment/comment.module.ts b/apps/backend/src/comment/comment.module.ts new file mode 100644 index 0000000..2eeed0a --- /dev/null +++ b/apps/backend/src/comment/comment.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { CommentController } from './comment.controller'; +import { CommentService } from './comment.service'; + +@Module({ + controllers: [CommentController], + providers: [CommentService], +}) +export class CommentModule {} diff --git a/apps/backend/src/comment/comment.service.ts b/apps/backend/src/comment/comment.service.ts new file mode 100644 index 0000000..b6913b2 --- /dev/null +++ b/apps/backend/src/comment/comment.service.ts @@ -0,0 +1,45 @@ +import { Injectable, NotFoundException } from '@nestjs/common'; +import { Comment } from '@prisma/client'; +import { PrismaService } from 'nestjs-prisma'; +import { CreateCommentDto } from './dto/create-comment.dto'; + +@Injectable() +export class CommentService { + constructor(private readonly prisma: PrismaService) {} + + async create(postId: number, createCommentDto: CreateCommentDto): Promise { + const { userId } = createCommentDto; + + const post = await this.prisma.post.findUnique({ where: { id: postId } }); + if (!post) { + throw new NotFoundException('Post not found'); + } + + const user = await this.prisma.user.findUnique({ where: { id: userId } }); + if (!user) { + throw new NotFoundException('User not found'); + } + + return this.prisma.comment.create({ + data: { + text: createCommentDto.text, + user: { connect: { id: userId } }, + post: { connect: { id: postId } }, + }, + }); + } + + async remove(id: number): Promise { + const comment = await this.prisma.comment.findUnique({ where: { id } }); + if (!comment) { + throw new NotFoundException('Comment not found'); + } + + return this.prisma.comment.delete({ where: { id } }); + } + + async findAll(postId: number): Promise<{ comments: Comment[] }> { + const comments = await this.prisma.comment.findMany({ where: { postId } }); + return { comments }; + } +} diff --git a/apps/backend/src/comment/dto/create-comment.dto.ts b/apps/backend/src/comment/dto/create-comment.dto.ts new file mode 100644 index 0000000..d11f972 --- /dev/null +++ b/apps/backend/src/comment/dto/create-comment.dto.ts @@ -0,0 +1,5 @@ +export class CreateCommentDto { + text: string; + userId: number; + postId: number; +} diff --git a/apps/backend/src/comment/entities/comment.entity.ts b/apps/backend/src/comment/entities/comment.entity.ts new file mode 100644 index 0000000..9a0cf54 --- /dev/null +++ b/apps/backend/src/comment/entities/comment.entity.ts @@ -0,0 +1,22 @@ +import { IsDate, IsNotEmpty, IsNumber, IsPositive, IsString } from 'class-validator'; + +export class Comment { + @IsNumber() + @IsPositive() + id: number; + + @IsString() + @IsNotEmpty() + text: string; + + @IsDate() + date: Date; + + @IsNumber() + @IsPositive() + postId: number; + + @IsNumber() + @IsPositive() + userId: number; +}