Skip to content

Commit

Permalink
feat: add post like and unlike
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-Frost committed May 6, 2024
1 parent f0c7691 commit 58ae698
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/posts/posts.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Controller,
FileTypeValidator,
Get,
Param,
ParseFilePipe,
Post,
UploadedFile,
Expand Down Expand Up @@ -50,4 +51,14 @@ export class PostsController {
findAll() {
return this.postsService.findAll();
}

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

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

@Module({
imports: [MikroOrmModule.forFeature([Post])],
imports: [MikroOrmModule.forFeature([Post, User])],
controllers: [PostsController],
providers: [PostsService],
})
Expand Down
29 changes: 29 additions & 0 deletions src/posts/posts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { InjectRepository } from '@mikro-orm/nestjs';
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 { CreatePostDto } from './dto/create-post.dto';
import { Post } from './entities/post.entity';

Expand All @@ -11,6 +12,8 @@ export class PostsService {
constructor(
@InjectRepository(Post)
private readonly postRepository: EntityRepository<Post>,
@InjectRepository(User)
private readonly userRepository: EntityRepository<User>,
private readonly em: EntityManager,
) {}

Expand All @@ -31,4 +34,30 @@ export class PostsService {
findAll() {
return this.postRepository.findAll({ populate: ['author'] });
}

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

if (!post.likedBy.contains(user)) post.likedBy.add(user);

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

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

if (post.likedBy.contains(user)) post.likedBy.remove(user);

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

0 comments on commit 58ae698

Please sign in to comment.