Skip to content

Commit

Permalink
Comment form it's working now
Browse files Browse the repository at this point in the history
  • Loading branch information
Frdrcpeter007 committed Dec 14, 2023
1 parent e63cbf6 commit 87d6ea3
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { OperationModule } from './modules/operation-saving/operation.module';
import { WaitingModule } from './modules/waiting-list/waiting.module';
import { ContactModule } from './modules/contact/contact.module';
import { BlogModule } from './modules/blog/blog.module';
import { CommentModule } from './modules/comment/comment.module';

@Module({
imports: [
Expand All @@ -41,6 +42,7 @@ import { BlogModule } from './modules/blog/blog.module';
CachingModule,
MailModule,
WaitingModule,
CommentModule,
ContactModule,
AuthModule,
SessionModule,
Expand Down Expand Up @@ -70,4 +72,4 @@ import { BlogModule } from './modules/blog/blog.module';
{ provide: APP_GUARD, useClass: RolesGuard },
],
})
export class AppModule {}
export class AppModule { }
6 changes: 5 additions & 1 deletion src/modules/blog/blog.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ export class BlogService {
}

async getBlogDetailsWithComments(slug: string): Promise<any> {
const blog = await this.blogRepository.findOne({ where: { slug }, relations: ['comments'] });
const blog = await this.blogRepository.createQueryBuilder('blog')
.leftJoinAndSelect('blog.comments', 'comments')
.where('blog.slug = :slug', { slug })
.orderBy('comments.createdAt', 'DESC')
.getOne();

const last = await this.blogRepository.find({
where: { slug: Not(slug) },
Expand Down
7 changes: 3 additions & 4 deletions src/modules/blog/entities/blog.entity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Comment } from 'src/modules/comment/entities/comment.entity';
import { BaseEntity } from '../../../db/base-entity';
import { Entity, Column, ManyToMany, JoinTable } from 'typeorm';
import { Entity, Column, ManyToMany, JoinTable, OneToMany } from 'typeorm';

@Entity()
export class Blog extends BaseEntity {
Expand All @@ -23,8 +23,7 @@ export class Blog extends BaseEntity {
@Column('text', { array: true, default: '{}' })
tags?: string[];

@ManyToMany(() => Comment, comment => comment.blog)
@JoinTable()
comments?: Comment[];
@OneToMany(() => Comment, comment => comment.blog)
comments: Comment[];

}
5 changes: 3 additions & 2 deletions src/modules/comment/comment.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ 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';
import { CommentService } from './comment.service';

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

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

@Module({
imports: [
TypeOrmModule.forFeature([Comment]),
TypeOrmModule.forFeature([Comment, Blog])
],
controllers: [CommentController],
providers: [CommentService],
Expand Down
1 change: 1 addition & 0 deletions src/modules/comment/comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class CommentService {
constructor(
@InjectRepository(Comment)
private commentRepository: Repository<Comment>,
@InjectRepository(Blog)
private blogRepository: Repository<Blog>,

) { }
Expand Down
4 changes: 0 additions & 4 deletions src/modules/comment/dto/comment.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ export class CreateCommentDto {
@IsString()
comment: string;

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

@IsNotEmpty()
@IsString()
blog: string;
Expand Down
3 changes: 1 addition & 2 deletions src/modules/comment/entities/comment.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export class Comment extends BaseEntity {
@Column({ nullable: false })
comment: string;

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

0 comments on commit 87d6ea3

Please sign in to comment.