Skip to content
Open
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
23 changes: 23 additions & 0 deletions apps/backend/src/comment/comment.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Body, Controller, Delete, 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()
export class CommentController {
constructor(private readonly commentService: CommentService) {}

@Post('post/:postId/comments')
@UseGuards(AuthGuard('jwt'))
create(@Param('postId', ParseIntPipe) postId: number, @Body() createCommentDto: CreateCommentDto): Promise<Comment> {
return this.commentService.create(postId, createCommentDto);
}

@Delete('comments/:id')
@UseGuards(AuthGuard('jwt'))
remove(@Param('id', ParseIntPipe) id: number): Promise<Comment> {
return this.commentService.remove(id);
}
}
9 changes: 9 additions & 0 deletions apps/backend/src/comment/comment.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
40 changes: 40 additions & 0 deletions apps/backend/src/comment/comment.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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<Comment> {
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<Comment> {
const comment = await this.prisma.comment.findUnique({ where: { id } });
if (!comment) {
throw new NotFoundException('Comment not found');
}

return this.prisma.comment.delete({ where: { id } });
}
}
5 changes: 5 additions & 0 deletions apps/backend/src/comment/dto/create-comment.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class CreateCommentDto {
text: string;
userId: number;
postId: number;
}
22 changes: 22 additions & 0 deletions apps/backend/src/comment/entities/comment.entity.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Loading