Skip to content

Commit

Permalink
feat: add post comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-Frost committed May 6, 2024
1 parent 7939baf commit 84672d8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/posts/dto/comment-post.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IsNotEmpty, IsString } from 'class-validator';

export class CommentPostDto {
@IsString()
@IsNotEmpty()
text: string;
}
15 changes: 13 additions & 2 deletions src/posts/posts.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Body,
Controller,
Delete,
FileTypeValidator,
Get,
Param,
Expand All @@ -14,6 +15,7 @@ import { randomUUID } from 'crypto';
import { diskStorage } from 'multer';
import { extname } from 'path';
import { User, UserPayload } from 'src/auth/decorators/user.decorator';
import { CommentPostDto } from './dto/comment-post.dto';
import { CreatePostDto } from './dto/create-post.dto';
import { PostsService } from './posts.service';

Expand Down Expand Up @@ -52,13 +54,22 @@ export class PostsController {
return this.postsService.findAll();
}

@Post(':id/like')
@Post(':id/likes')
like(@User() user: UserPayload, @Param('id') id: string) {
return this.postsService.like(+id, user.sub);
}

@Post(':id/unlike')
@Delete(':id/likes')
unlike(@User() user: UserPayload, @Param('id') id: string) {
return this.postsService.unlike(+id, user.sub);
}

@Post(':id/comments')
comment(
@User() user: UserPayload,
@Param('id') id: string,
@Body() commentPostDto: CommentPostDto,
) {
return this.postsService.comment(+id, user.sub, commentPostDto);
}
}
3 changes: 2 additions & 1 deletion src/posts/posts.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { Module } from '@nestjs/common';
import { User } from 'src/users/entities/user.entity';
import { PostComment } from './entities/post-comment.entity';
import { Post } from './entities/post.entity';
import { PostsController } from './posts.controller';
import { PostsService } from './posts.service';

@Module({
imports: [MikroOrmModule.forFeature([Post, User])],
imports: [MikroOrmModule.forFeature([Post, PostComment, User])],
controllers: [PostsController],
providers: [PostsService],
})
Expand Down
28 changes: 25 additions & 3 deletions src/posts/posts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { EntityManager } from '@mikro-orm/postgresql';
import { Injectable } from '@nestjs/common';
import { UserPayload } from 'src/auth/decorators/user.decorator';
import { User } from 'src/users/entities/user.entity';
import { CommentPostDto } from './dto/comment-post.dto';
import { CreatePostDto } from './dto/create-post.dto';
import { PostComment } from './entities/post-comment.entity';
import { Post } from './entities/post.entity';

@Injectable()
Expand All @@ -14,6 +16,8 @@ export class PostsService {
private readonly postRepository: EntityRepository<Post>,
@InjectRepository(User)
private readonly userRepository: EntityRepository<User>,
@InjectRepository(PostComment)
private readonly postCommentRepository: EntityRepository<PostComment>,
private readonly em: EntityManager,
) {}

Expand All @@ -33,14 +37,14 @@ export class PostsService {

findAll() {
return this.postRepository.findAll({
populate: ['author', 'likedBy', 'comments'],
populate: ['author', 'likedBy', 'comments', 'comments.author'],
});
}

async like(id: number, userId: number) {
const post = await this.postRepository.findOneOrFail(
{ id },
{ populate: ['author', 'likedBy', 'comments'] },
{ populate: ['author', 'likedBy', 'comments', 'comments.author'] },
);
const user = await this.userRepository.findOneOrFail({ id: userId });

Expand All @@ -53,7 +57,7 @@ export class PostsService {
async unlike(id: number, userId: number) {
const post = await this.postRepository.findOneOrFail(
{ id },
{ populate: ['author', 'likedBy', 'comments'] },
{ populate: ['author', 'likedBy', 'comments', 'comments.author'] },
);
const user = await this.userRepository.findOneOrFail({ id: userId });

Expand All @@ -62,4 +66,22 @@ export class PostsService {
await this.em.flush();
return post;
}

async comment(id: number, userId: number, comment: CommentPostDto) {
const post = await this.postRepository.findOneOrFail(
{ id },
{ populate: ['author', 'likedBy', 'comments', 'comments.author'] },
);
const user = await this.userRepository.findOneOrFail({ id: userId });

const postComment = this.postCommentRepository.create({
...comment,
post,
author: user,
});
post.comments.add(postComment);

await this.em.flush();
return post;
}
}

0 comments on commit 84672d8

Please sign in to comment.