Skip to content

Commit

Permalink
fix(): fix post get all
Browse files Browse the repository at this point in the history
  • Loading branch information
NarHakobyan committed Apr 1, 2024
1 parent 23893e9 commit 2487cbe
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/database/migrations/Migration20240331182341.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
);`);

Expand All @@ -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")
);`);

Expand Down
6 changes: 3 additions & 3 deletions src/modules/post/commands/create-post.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/post/post-translation.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ export class PostTranslationEntity extends AbstractTranslationEntity<PostTransla
@Property()
description!: string;

@Property({ type: 'uuid', fieldName: 'post_id', persist: false })
@Property({ type: 'uuid', persist: false })
postId!: string; // Ensure this is your foreign key column, linked via the ManyToOne relation below

@ManyToOne({
entity: () => 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
}
27 changes: 16 additions & 11 deletions src/modules/post/post.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
HttpStatus,
Post,
Put,
Query,
} from '@nestjs/common';
import {
ApiAcceptedResponse,
Expand All @@ -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')
Expand All @@ -45,15 +50,15 @@ export class PostController {
return postEntity.toDto();
}

// @Get()
// @Auth([RoleType.USER])
// @UseLanguageInterceptor()
// @ApiPageOkResponse({ type: PostDto })
// async getPosts(
// @Query() postsPageOptionsDto: PostPageOptionsDto,
// ): Promise<PageDto<PostDto>> {
// return this.postService.getAllPost(postsPageOptionsDto);
// }
@Get()
@Auth([RoleType.USER])
@UseLanguageInterceptor()
@ApiPageOkResponse({ type: PostDto })
async getPosts(
@Query() postsPageOptionsDto: PostPageOptionsDto,
): Promise<PageDto<PostDto>> {
return this.postService.getAllPost(postsPageOptionsDto);
}

@Get(':id')
@Auth([])
Expand Down
1 change: 1 addition & 0 deletions src/modules/post/post.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class PostEntity extends AbstractEntity<PostDto> {
joinColumn: 'user_id',
deleteRule: 'cascade',
updateRule: 'cascade',
nullable: false,
})
user?: UserEntity;

Expand Down
29 changes: 16 additions & 13 deletions src/modules/post/post.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -24,17 +27,17 @@ export class PostService {
);
}

// async getAllPost(
// postPageOptionsDto: PostPageOptionsDto,
// ): Promise<PageDto<PostDto>> {
// 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<PageDto<PostDto>> {
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<PostEntity> {
const queryBuilder = this.postRepository
Expand Down

0 comments on commit 2487cbe

Please sign in to comment.