Skip to content

Commit b16dead

Browse files
committed
fix(): create separate files for command and queries
1 parent cd09baa commit b16dead

File tree

5 files changed

+74
-69
lines changed

5 files changed

+74
-69
lines changed
Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,10 @@
1-
import type { ICommand, ICommandHandler } from '@nestjs/cqrs';
2-
import { CommandHandler } from '@nestjs/cqrs';
3-
import { InjectRepository } from '@nestjs/typeorm';
4-
import { Repository } from 'typeorm';
1+
import type { ICommand } from '@nestjs/cqrs';
52

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

105
export class CreatePostCommand implements ICommand {
116
constructor(
127
public readonly userId: Uuid,
138
public readonly createPostDto: CreatePostDto,
149
) {}
1510
}
16-
17-
@CommandHandler(CreatePostCommand)
18-
export class CreatePostHandler
19-
implements ICommandHandler<CreatePostCommand, PostEntity>
20-
{
21-
constructor(
22-
@InjectRepository(PostEntity)
23-
private postRepository: Repository<PostEntity>,
24-
@InjectRepository(PostTranslationEntity)
25-
private postTranslationRepository: Repository<PostTranslationEntity>,
26-
) {}
27-
28-
async execute(command: CreatePostCommand) {
29-
const { userId, createPostDto } = command;
30-
const postEntity = this.postRepository.create({ userId });
31-
const translations: PostTranslationEntity[] = [];
32-
33-
await this.postRepository.save(postEntity);
34-
35-
// FIXME: Create generic function for translation creation
36-
for (const createTranslationDto of createPostDto.title) {
37-
const languageCode = createTranslationDto.languageCode;
38-
const translationEntity = this.postTranslationRepository.create({
39-
postId: postEntity.id,
40-
languageCode,
41-
title: createTranslationDto.text,
42-
description: createPostDto.description.find(
43-
(desc) => desc.languageCode === languageCode,
44-
)!.text,
45-
});
46-
47-
translations.push(translationEntity);
48-
}
49-
50-
await this.postTranslationRepository.save(translations);
51-
52-
postEntity.translations = translations;
53-
54-
return postEntity;
55-
}
56-
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import type { ICommandHandler } from '@nestjs/cqrs';
2+
import { CommandHandler } from '@nestjs/cqrs';
3+
import { InjectRepository } from '@nestjs/typeorm';
4+
import { Repository } from 'typeorm';
5+
6+
import { PostEntity } from '../post.entity.ts';
7+
import { PostTranslationEntity } from '../post-translation.entity.ts';
8+
import { CreatePostCommand } from './create-post.command.ts';
9+
10+
@CommandHandler(CreatePostCommand)
11+
export class CreatePostHandler
12+
implements ICommandHandler<CreatePostCommand, PostEntity>
13+
{
14+
constructor(
15+
@InjectRepository(PostEntity)
16+
private postRepository: Repository<PostEntity>,
17+
@InjectRepository(PostTranslationEntity)
18+
private postTranslationRepository: Repository<PostTranslationEntity>,
19+
) {}
20+
21+
async execute(command: CreatePostCommand) {
22+
const { userId, createPostDto } = command;
23+
const postEntity = this.postRepository.create({ userId });
24+
const translations: PostTranslationEntity[] = [];
25+
26+
await this.postRepository.save(postEntity);
27+
28+
// FIXME: Create generic function for translation creation
29+
for (const createTranslationDto of createPostDto.title) {
30+
const languageCode = createTranslationDto.languageCode;
31+
const translationEntity = this.postTranslationRepository.create({
32+
postId: postEntity.id,
33+
languageCode,
34+
title: createTranslationDto.text,
35+
description: createPostDto.description.find(
36+
(desc) => desc.languageCode === languageCode,
37+
)!.text,
38+
});
39+
40+
translations.push(translationEntity);
41+
}
42+
43+
await this.postTranslationRepository.save(translations);
44+
45+
postEntity.translations = translations;
46+
47+
return postEntity;
48+
}
49+
}

src/modules/post/post.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Module } from '@nestjs/common';
22
import { TypeOrmModule } from '@nestjs/typeorm';
33

4-
import { CreatePostHandler } from './commands/create-post.command.ts';
4+
import { CreatePostHandler } from './commands/create-post.handler.ts';
55
import { PostController } from './post.controller.ts';
66
import { PostEntity } from './post.entity.ts';
77
import { PostService } from './post.service.ts';
88
import { PostTranslationEntity } from './post-translation.entity.ts';
9-
import { GetPostHandler } from './queries/get-post.query.ts';
9+
import { GetPostHandler } from './queries/get-post.handler.ts';
1010

1111
const handlers = [CreatePostHandler, GetPostHandler];
1212

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { IQueryHandler } from '@nestjs/cqrs';
2+
import { QueryHandler } from '@nestjs/cqrs';
3+
import { InjectRepository } from '@nestjs/typeorm';
4+
import { Repository } from 'typeorm';
5+
6+
import { PostEntity } from '../post.entity.ts';
7+
import { GetPostQuery } from './get-post.query.ts';
8+
9+
@QueryHandler(GetPostQuery)
10+
export class GetPostHandler implements IQueryHandler<GetPostQuery> {
11+
constructor(
12+
@InjectRepository(PostEntity)
13+
private postRepository: Repository<PostEntity>,
14+
) {}
15+
16+
async execute(query: GetPostQuery) {
17+
return this.postRepository.findBy({
18+
userId: query.userId as never,
19+
});
20+
}
21+
}
Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,5 @@
1-
import type { ICommand, IQueryHandler } from '@nestjs/cqrs';
2-
import { QueryHandler } from '@nestjs/cqrs';
3-
import { InjectRepository } from '@nestjs/typeorm';
4-
import { Repository } from 'typeorm';
5-
6-
import { PostEntity } from '../post.entity.ts';
1+
import type { ICommand } from '@nestjs/cqrs';
72

83
export class GetPostQuery implements ICommand {
94
constructor(public readonly userId: Uuid) {}
105
}
11-
12-
@QueryHandler(GetPostQuery)
13-
export class GetPostHandler implements IQueryHandler<GetPostQuery> {
14-
constructor(
15-
@InjectRepository(PostEntity)
16-
private postRepository: Repository<PostEntity>,
17-
) {}
18-
19-
async execute(query: GetPostQuery) {
20-
return this.postRepository.findBy({
21-
userId: query.userId as never,
22-
});
23-
}
24-
}

0 commit comments

Comments
 (0)