Skip to content

Commit

Permalink
Merge pull request #8 from nailtonvital/novas-consultas-de-relacionam…
Browse files Browse the repository at this point in the history
…entos-de-endpoints

adjusts comments and likes
  • Loading branch information
nailtonvital committed May 31, 2024
2 parents 33532c0 + b2a11cd commit e0ed908
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/posts/dto/likePost.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString, IsNumber, IsDate, IsOptional } from 'class-validator';

export class LikePostDto {
@ApiProperty()
@IsNumber()
idPost: number;

@ApiProperty()
@IsNumber()
idUser: number;
}
5 changes: 3 additions & 2 deletions src/posts/posts.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/commo
import { PostsService } from './posts.service';
import { CreatePostDto } from './dto/create-post.dto';
import { UpdatePostDto } from './dto/update-post.dto';
import { LikePostDto } from './dto/likePost.dto';

@Controller('posts')
export class PostsController {
Expand Down Expand Up @@ -53,7 +54,7 @@ export class PostsController {
}

@Post('post/addLike')
async addLike(@Body() body) {
async addLike(@Body() body: LikePostDto) {
const { idPost, idUser } = body;
try {
return await this.postsService.addLike(idPost, idUser);
Expand All @@ -63,7 +64,7 @@ export class PostsController {
}

@Post('post/removeLike')
async removeLike(@Body() body) {
async removeLike(@Body() body: LikePostDto) {
const { idPost, idUser } = body;
try {
return await this.postsService.removeLike(idPost, idUser);
Expand Down
12 changes: 8 additions & 4 deletions src/posts/posts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,14 @@ export class PostsService {

async findOne(id: number) {
try {
return await this.postRepository.findOne({
where: { idPost: id },
relations: ['interests', 'user', 'comments', 'likes']
});
return await this.postRepository.createQueryBuilder('post')
.where('post.idPost = :id', { id })
.leftJoinAndSelect('post.interests', 'interests')
.leftJoinAndSelect('post.user', 'user')
.leftJoinAndSelect('post.comments', 'comments')
.leftJoinAndSelect('post.likes', 'likes')
.loadRelationCountAndMap('post.commentsCount', 'post.comments')
.getOne();
} catch (error) {
throw error;
}
Expand Down
14 changes: 10 additions & 4 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,16 @@ export class UsersService {

async findOneById(id: number) {
try {
return await this.userRepository.findOne({
where: { idUser: id },
relations: ['interests', 'posts'],
});
return await this.userRepository.createQueryBuilder('user')
.where('user.idUser = :id', { id: id })
.leftJoinAndSelect('user.interests', 'interests')
.leftJoinAndSelect('user.posts', 'posts')
.leftJoinAndSelect('posts.likes', 'likes')
.leftJoinAndSelect('posts.comments', 'comments')
.leftJoinAndSelect('posts.interests', 'post_interests')
.loadRelationCountAndMap('user.postsCount', 'user.posts')
.loadRelationCountAndMap('posts.commentsCount', 'posts.comments')
.getOne();
} catch (error) {
throw error;
}
Expand Down

0 comments on commit e0ed908

Please sign in to comment.