-
Notifications
You must be signed in to change notification settings - Fork 469
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(): create separate files for command and queries
- Loading branch information
1 parent
cd09baa
commit b16dead
Showing
5 changed files
with
74 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
} |