Skip to content

Commit

Permalink
Add comment modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Frdrcpeter007 committed Dec 6, 2023
1 parent a1ffd59 commit e63cbf6
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 4 deletions.
2 changes: 0 additions & 2 deletions src/modules/blog/blog.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Not, Repository } from 'typeorm';
import { _404 } from '../../common/constants/errors';
import { MailService } from '../mail/mail.service';
import { Blog } from './entities/blog.entity';
import { CreateBlogDto } from './dto/blog.dto';
import Slugify from 'slugify';
import slugify from 'slugify';

@Injectable()
Expand Down
22 changes: 22 additions & 0 deletions src/modules/comment/comment.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
Body,
Controller,
Post
} from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { _403, _404, _409 } from '../../common/constants/errors';
import { Public } from 'src/common/decorators/public.decorator';
import { CreateCommentDto } from './dto/comment.dto';

@ApiTags('Comments')
@Controller('comments')
export class CommentController {
constructor(private readonly commentService: any) { }

@Post()
@Public()
@ApiOperation({ summary: 'Save comment a blog' })
async add(@Body() blogDto: CreateCommentDto) {
return await this.commentService.comment(blogDto);
}
}
15 changes: 15 additions & 0 deletions src/modules/comment/comment.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Comment } from './entities/comment.entity';
import { CommentController } from './comment.controller';
import { CommentService } from './comment.service';

@Module({
imports: [
TypeOrmModule.forFeature([Comment]),
],
controllers: [CommentController],
providers: [CommentService],
exports: [CommentService],
})
export class CommentModule { }
32 changes: 32 additions & 0 deletions src/modules/comment/comment.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Not, Repository } from 'typeorm';
import { _404 } from '../../common/constants/errors';
import { Comment } from './entities/comment.entity';
import { CreateCommentDto } from './dto/comment.dto';
import { Blog } from '../blog/entities/blog.entity';

@Injectable()
export class CommentService {
constructor(
@InjectRepository(Comment)
private commentRepository: Repository<Comment>,
private blogRepository: Repository<Blog>,

) { }

async add(comment: CreateCommentDto): Promise<Comment> {

const blog = await this.blogRepository.findOne({ where: { slug: comment.blog } });

const newComment = this.commentRepository.create({
fullname: comment.fullname,
email: comment.email,
comment: comment.comment,
blog: blog
});

return this.commentRepository.save(newComment);

}
}
28 changes: 28 additions & 0 deletions src/modules/comment/dto/comment.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
IsArray,
IsNotEmpty,
IsString
} from 'class-validator';

export class CreateCommentDto {

@IsNotEmpty()
@IsString()
fullname: string;

@IsString()
email: string;

@IsNotEmpty()
@IsString()
comment: string;

@IsNotEmpty()
@IsString()
content: string;

@IsNotEmpty()
@IsString()
blog: string;

}
5 changes: 3 additions & 2 deletions src/modules/comment/entities/comment.entity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Blog } from 'src/modules/blog/entities/blog.entity';
import { BaseEntity } from '../../../db/base-entity';
import { Entity, Column, ManyToOne } from 'typeorm';
import { Entity, Column, ManyToOne, JoinColumn } from 'typeorm';

@Entity()
export class Comment extends BaseEntity {
Expand All @@ -14,6 +14,7 @@ export class Comment extends BaseEntity {
@Column({ nullable: false })
comment: string;

@ManyToOne(() => Blog, blog => blog.comments)
@ManyToOne(() => Blog, (blog) => blog.comments)
@JoinColumn({ name: 'idBlog' })
blog: Blog;
}

0 comments on commit e63cbf6

Please sign in to comment.