From 2487cbea30bf3fca03f1369441e08eef43bd0cce Mon Sep 17 00:00:00 2001 From: Narek Date: Mon, 1 Apr 2024 17:20:31 +0400 Subject: [PATCH] fix(): fix post get all --- .../migrations/Migration20240331182341.ts | 4 +-- .../post/commands/create-post.command.ts | 6 ++-- src/modules/post/post-translation.entity.ts | 4 +-- src/modules/post/post.controller.ts | 27 ++++++++++------- src/modules/post/post.entity.ts | 1 + src/modules/post/post.service.ts | 29 ++++++++++--------- 6 files changed, 40 insertions(+), 31 deletions(-) diff --git a/src/database/migrations/Migration20240331182341.ts b/src/database/migrations/Migration20240331182341.ts index 37f24ee..a2dba30 100644 --- a/src/database/migrations/Migration20240331182341.ts +++ b/src/database/migrations/Migration20240331182341.ts @@ -9,7 +9,7 @@ export class Migration20240331182341 extends Migration { "id" uuid not null default uuid_generate_v4(), "created_at" timestamp not null default now(), "updated_at" timestamp not null default now(), - "user_id" uuid null, + "user_id" uuid not null, constraint "posts_pkey" primary key ("id") );`); @@ -22,7 +22,7 @@ export class Migration20240331182341 extends Migration { "language_code" text check ("language_code" in ('en_US', 'ru_RU')) not null, "title" varchar(255) not null, "description" varchar(255) not null, - "post_id" uuid null, + "post_id" uuid not null, constraint "post_translations_pkey" primary key ("id") );`); diff --git a/src/modules/post/commands/create-post.command.ts b/src/modules/post/commands/create-post.command.ts index d19d4cb..d8001d0 100644 --- a/src/modules/post/commands/create-post.command.ts +++ b/src/modules/post/commands/create-post.command.ts @@ -29,16 +29,16 @@ export class CreatePostHandler async execute(command: CreatePostCommand) { const { userId, createPostDto } = command; - const postEntity = this.postRepository.create({ userId }); + const postEntity = this.postRepository.create({ user: userId }); const translations: PostTranslationEntity[] = []; - await this.postRepository.insert(postEntity); + await this.postRepository.persistAndFlush(postEntity); // FIXME: Create generic function for translation creation for (const createTranslationDto of createPostDto.title) { const languageCode = createTranslationDto.languageCode; const translationEntity = this.postTranslationRepository.create({ - postId: postEntity.id, + post: postEntity.id, languageCode, title: createTranslationDto.text, description: find(createPostDto.description, { diff --git a/src/modules/post/post-translation.entity.ts b/src/modules/post/post-translation.entity.ts index 530ce03..789cff1 100644 --- a/src/modules/post/post-translation.entity.ts +++ b/src/modules/post/post-translation.entity.ts @@ -14,16 +14,16 @@ export class PostTranslationEntity extends AbstractTranslationEntity PostEntity, // This tells MikroORM the entity to use for the relation - fieldName: 'post_id', // This tells MikroORM the column name to use, which should match the one defined in @Property above joinColumn: 'post_id', // Explicitly define the join column name for clarity, though this is optional if fieldName is already specified columnType: 'uuid', // This specifies the column type, ensuring it matches the type defined in @Property deleteRule: 'cascade', // Use onDelete instead of deleteRule updateRule: 'cascade', // Use onUpdate instead of updateRule + nullable: false, }) post?: PostEntity; // This does not directly create a new column but links to the existing 'post_id' column } diff --git a/src/modules/post/post.controller.ts b/src/modules/post/post.controller.ts index 5c364e8..6d35eed 100644 --- a/src/modules/post/post.controller.ts +++ b/src/modules/post/post.controller.ts @@ -7,6 +7,7 @@ import { HttpStatus, Post, Put, + Query, } from '@nestjs/common'; import { ApiAcceptedResponse, @@ -15,14 +16,18 @@ import { ApiTags, } from '@nestjs/swagger'; +import { PageDto } from '../../common/dto/page.dto.ts'; import { RoleType } from '../../constants'; +import { ApiPageOkResponse } from '../../decorators/api-page-ok-response.decorator.ts'; +import { AuthUser } from '../../decorators/auth-user.decorator.ts'; +import { Auth, UUIDParam } from '../../decorators/http.decorators.ts'; +import { UseLanguageInterceptor } from '../../interceptors/language-interceptor.service.ts'; import { UserEntity } from '../user/user.entity'; import { CreatePostDto } from './dtos/create-post.dto'; import { PostDto } from './dtos/post.dto'; +import { PostPageOptionsDto } from './dtos/post-page-options.dto.ts'; import { UpdatePostDto } from './dtos/update-post.dto'; import { PostService } from './post.service'; -import { Auth, UUIDParam } from '../../decorators/http.decorators.ts'; -import { AuthUser } from '../../decorators/auth-user.decorator.ts'; @Controller('posts') @ApiTags('posts') @@ -45,15 +50,15 @@ export class PostController { return postEntity.toDto(); } - // @Get() - // @Auth([RoleType.USER]) - // @UseLanguageInterceptor() - // @ApiPageOkResponse({ type: PostDto }) - // async getPosts( - // @Query() postsPageOptionsDto: PostPageOptionsDto, - // ): Promise> { - // return this.postService.getAllPost(postsPageOptionsDto); - // } + @Get() + @Auth([RoleType.USER]) + @UseLanguageInterceptor() + @ApiPageOkResponse({ type: PostDto }) + async getPosts( + @Query() postsPageOptionsDto: PostPageOptionsDto, + ): Promise> { + return this.postService.getAllPost(postsPageOptionsDto); + } @Get(':id') @Auth([]) diff --git a/src/modules/post/post.entity.ts b/src/modules/post/post.entity.ts index 0c0ab22..2803491 100644 --- a/src/modules/post/post.entity.ts +++ b/src/modules/post/post.entity.ts @@ -23,6 +23,7 @@ export class PostEntity extends AbstractEntity { joinColumn: 'user_id', deleteRule: 'cascade', updateRule: 'cascade', + nullable: false, }) user?: UserEntity; diff --git a/src/modules/post/post.service.ts b/src/modules/post/post.service.ts index a6828aa..a7580cf 100644 --- a/src/modules/post/post.service.ts +++ b/src/modules/post/post.service.ts @@ -3,9 +3,12 @@ import { EntityRepository } from '@mikro-orm/postgresql'; import { Injectable } from '@nestjs/common'; import { CommandBus } from '@nestjs/cqrs'; +import { PageDto } from '../../common/dto/page.dto.ts'; import { CreatePostCommand } from './commands/create-post.command'; -import { type CreatePostDto } from './dtos/create-post.dto'; -import { type UpdatePostDto } from './dtos/update-post.dto'; +import type { CreatePostDto } from './dtos/create-post.dto'; +import { PostDto } from './dtos/post.dto.ts'; +import { PostPageOptionsDto } from './dtos/post-page-options.dto.ts'; +import type { UpdatePostDto } from './dtos/update-post.dto'; import { PostNotFoundException } from './exceptions/post-not-found.exception'; import { PostEntity } from './post.entity'; @@ -24,17 +27,17 @@ export class PostService { ); } - // async getAllPost( - // postPageOptionsDto: PostPageOptionsDto, - // ): Promise> { - // const queryBuilder = this.postRepository - // .createQueryBuilder('post') - // .leftJoinAndSelect('post.translations', 'postTranslation'); - // const [items, pageMetaDto] = - // await queryBuilder.paginate(postPageOptionsDto); - // - // return items.toPageDto(pageMetaDto); - // } + async getAllPost( + postPageOptionsDto: PostPageOptionsDto, + ): Promise> { + const queryBuilder = this.postRepository + .createQueryBuilder('post') + .leftJoinAndSelect('post.translations', 'postTranslation'); + const [items, pageMetaDto] = + await queryBuilder.paginate(postPageOptionsDto); + + return items.toPageDto(pageMetaDto); + } async getSinglePost(id: Uuid): Promise { const queryBuilder = this.postRepository