Skip to content

Commit

Permalink
fix(): create separate files for command and queries
Browse files Browse the repository at this point in the history
  • Loading branch information
NarHakobyan committed Jan 2, 2025
1 parent cd09baa commit b16dead
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 69 deletions.
48 changes: 1 addition & 47 deletions src/modules/post/commands/create-post.command.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,10 @@
import type { ICommand, ICommandHandler } from '@nestjs/cqrs';
import { CommandHandler } from '@nestjs/cqrs';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import type { ICommand } from '@nestjs/cqrs';

import type { CreatePostDto } from '../dtos/create-post.dto.ts';
import { PostEntity } from '../post.entity.ts';
import { PostTranslationEntity } from '../post-translation.entity.ts';

export class CreatePostCommand implements ICommand {
constructor(
public readonly userId: Uuid,
public readonly createPostDto: CreatePostDto,
) {}
}

@CommandHandler(CreatePostCommand)
export class CreatePostHandler
implements ICommandHandler<CreatePostCommand, PostEntity>
{
constructor(
@InjectRepository(PostEntity)
private postRepository: Repository<PostEntity>,
@InjectRepository(PostTranslationEntity)
private postTranslationRepository: Repository<PostTranslationEntity>,
) {}

async execute(command: CreatePostCommand) {
const { userId, createPostDto } = command;
const postEntity = this.postRepository.create({ userId });
const translations: PostTranslationEntity[] = [];

await this.postRepository.save(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,
languageCode,
title: createTranslationDto.text,
description: createPostDto.description.find(
(desc) => desc.languageCode === languageCode,
)!.text,
});

translations.push(translationEntity);
}

await this.postTranslationRepository.save(translations);

postEntity.translations = translations;

return postEntity;
}
}
49 changes: 49 additions & 0 deletions src/modules/post/commands/create-post.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { ICommandHandler } from '@nestjs/cqrs';
import { CommandHandler } from '@nestjs/cqrs';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

import { PostEntity } from '../post.entity.ts';
import { PostTranslationEntity } from '../post-translation.entity.ts';
import { CreatePostCommand } from './create-post.command.ts';

@CommandHandler(CreatePostCommand)
export class CreatePostHandler
implements ICommandHandler<CreatePostCommand, PostEntity>
{
constructor(
@InjectRepository(PostEntity)
private postRepository: Repository<PostEntity>,
@InjectRepository(PostTranslationEntity)
private postTranslationRepository: Repository<PostTranslationEntity>,
) {}

async execute(command: CreatePostCommand) {
const { userId, createPostDto } = command;
const postEntity = this.postRepository.create({ userId });
const translations: PostTranslationEntity[] = [];

await this.postRepository.save(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,
languageCode,
title: createTranslationDto.text,
description: createPostDto.description.find(
(desc) => desc.languageCode === languageCode,
)!.text,
});

translations.push(translationEntity);
}

await this.postTranslationRepository.save(translations);

postEntity.translations = translations;

return postEntity;
}
}
4 changes: 2 additions & 2 deletions src/modules/post/post.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { CreatePostHandler } from './commands/create-post.command.ts';
import { CreatePostHandler } from './commands/create-post.handler.ts';
import { PostController } from './post.controller.ts';
import { PostEntity } from './post.entity.ts';
import { PostService } from './post.service.ts';
import { PostTranslationEntity } from './post-translation.entity.ts';
import { GetPostHandler } from './queries/get-post.query.ts';
import { GetPostHandler } from './queries/get-post.handler.ts';

const handlers = [CreatePostHandler, GetPostHandler];

Expand Down
21 changes: 21 additions & 0 deletions src/modules/post/queries/get-post.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { IQueryHandler } from '@nestjs/cqrs';
import { QueryHandler } from '@nestjs/cqrs';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

import { PostEntity } from '../post.entity.ts';
import { GetPostQuery } from './get-post.query.ts';

@QueryHandler(GetPostQuery)
export class GetPostHandler implements IQueryHandler<GetPostQuery> {
constructor(
@InjectRepository(PostEntity)
private postRepository: Repository<PostEntity>,
) {}

async execute(query: GetPostQuery) {
return this.postRepository.findBy({
userId: query.userId as never,
});
}
}
21 changes: 1 addition & 20 deletions src/modules/post/queries/get-post.query.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
import type { ICommand, IQueryHandler } from '@nestjs/cqrs';
import { QueryHandler } from '@nestjs/cqrs';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

import { PostEntity } from '../post.entity.ts';
import type { ICommand } from '@nestjs/cqrs';

export class GetPostQuery implements ICommand {
constructor(public readonly userId: Uuid) {}
}

@QueryHandler(GetPostQuery)
export class GetPostHandler implements IQueryHandler<GetPostQuery> {
constructor(
@InjectRepository(PostEntity)
private postRepository: Repository<PostEntity>,
) {}

async execute(query: GetPostQuery) {
return this.postRepository.findBy({
userId: query.userId as never,
});
}
}

0 comments on commit b16dead

Please sign in to comment.