-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
168 additions
and
117 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
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 |
---|---|---|
@@ -1,10 +1,18 @@ | ||
import { AsyncResult } from '@hexancore/common'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { ICommand, IEvent, IQuery } from '@nestjs/cqrs'; | ||
import { | ||
AR, | ||
AnyHCommand, | ||
ExtractHCommandResultValueType, | ||
AnyHQuery, | ||
ExtractHQueryResultValueType, | ||
AnyHEvent | ||
} from '@hexancore/common'; | ||
|
||
|
||
export type HCommandHandleResult<T extends AnyHCommand> = AR<ExtractHCommandResultValueType<T>>; | ||
export type HQueryHandleResult<T extends AnyHQuery> = AR<ExtractHQueryResultValueType<T>>; | ||
|
||
@Injectable() | ||
export abstract class GeneralBus { | ||
public abstract handleCommand<T>(command: ICommand): AsyncResult<T>; | ||
public abstract handleEvent(event: IEvent): AsyncResult<boolean>; | ||
public abstract handleQuery<T>(query: IQuery): AsyncResult<T>; | ||
public abstract handleCommand<T extends AnyHCommand>(command: T): HCommandHandleResult<T>; | ||
public abstract handleEvent(event: AnyHEvent): AR<boolean>; | ||
public abstract handleQuery<T extends AnyHQuery>(query: T): HQueryHandleResult<T>; | ||
} |
13 changes: 6 additions & 7 deletions
13
src/Infrastructure/Http/Controller/AbstractAppController.ts
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,19 +1,18 @@ | ||
import { GeneralBus } from '../../../Application/GeneralBus'; | ||
import { ICommand, IQuery } from '@nestjs/cqrs'; | ||
import { AnyHCommand, AnyHQuery } from '@hexancore/common'; | ||
import { GeneralBus, type HCommandHandleResult, type HQueryHandleResult } from '../../../Application'; | ||
import { Inject } from '@nestjs/common'; | ||
import { type AR } from '@hexancore/common'; | ||
import { AbstractController } from './AbstractController'; | ||
|
||
export abstract class AbstractAppController extends AbstractController { | ||
public constructor(@Inject() private gb: GeneralBus) { | ||
super(); | ||
} | ||
|
||
protected handleCommand<C extends ICommand, R>(command: C): AR<R> { | ||
return this.gb.handleCommand<R>(command); | ||
protected handleCommand<T extends AnyHCommand>(command: T): HCommandHandleResult<T> { | ||
return this.gb.handleCommand(command); | ||
} | ||
|
||
protected handleQuery<Q extends IQuery, R>(query: Q): AR<R> { | ||
return this.gb.handleQuery<R>(query); | ||
protected handleQuery<T extends AnyHQuery>(query: T): HQueryHandleResult<T> { | ||
return this.gb.handleQuery(query); | ||
} | ||
} |
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
6 changes: 4 additions & 2 deletions
6
test/helper/libs/test-lib/src/Book/Application/Book/Command/Create/BookCreateCommand.ts
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,3 +1,5 @@ | ||
export class BookCreateCommand { | ||
public constructor(public readonly title: string) {} | ||
import { HCommand } from "@hexancore/common"; | ||
|
||
export class BookCreateCommand extends HCommand<BookCreateCommand, void> { | ||
public title!: string; | ||
} |
7 changes: 5 additions & 2 deletions
7
test/helper/libs/test-lib/src/Book/Application/Book/Query/GetById/BookGetByIdQuery.ts
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,3 +1,6 @@ | ||
export class BookGetByIdQuery { | ||
public constructor(public readonly title: string) {} | ||
import { HQuery } from "@hexancore/common"; | ||
import type { BookDto } from "../../Dto/BookDto"; | ||
|
||
export class BookGetByIdQuery extends HQuery<BookGetByIdQuery, BookDto> { | ||
public title!: string; | ||
} |
3 changes: 1 addition & 2 deletions
3
test/helper/libs/test-lib/src/Book/Domain/Book/Shared/ValueObject/BookCopyId.ts
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,4 +1,3 @@ | ||
import { UIntValue, ValueObject } from "@hexancore/common"; | ||
import { UIntValue } from "@hexancore/common"; | ||
|
||
@ValueObject('Book') | ||
export class BookCopyId extends UIntValue<BookCopyId> { } |
3 changes: 1 addition & 2 deletions
3
test/helper/libs/test-lib/src/Book/Domain/Book/Shared/ValueObject/BookId.ts
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,4 +1,3 @@ | ||
import { UIntValue, ValueObject } from "@hexancore/common"; | ||
import { UIntValue } from "@hexancore/common"; | ||
|
||
@ValueObject('Book') | ||
export class BookId extends UIntValue<BookId> { } |
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 |
---|---|---|
|
@@ -2,30 +2,45 @@ | |
* @group unit/core | ||
*/ | ||
|
||
import { CommandBus, EventBus, ICommand, IEvent, IQuery, QueryBus } from '@nestjs/cqrs'; | ||
import { CommandBus, EventBus, QueryBus } from '@nestjs/cqrs'; | ||
import { Mocker } from '@hexancore/mocker'; | ||
import { Email, ERR, ERRA, OK, OKA } from '@hexancore/common'; | ||
import { Email, ERR, ERRA, HCommand, HEvent, HQuery, INTERNAL_ERR, OK, OKA, type JsonObjectType } from '@hexancore/common'; | ||
import { GeneralBus } from '@/Application/GeneralBus'; | ||
import { DefaultGeneralBus } from '@/Application/DefaultGeneralBus'; | ||
|
||
class TestCommand implements ICommand { | ||
public readonly email: Email; | ||
public constructor(emailRaw: string) { | ||
this.email = Email.c(emailRaw).v; | ||
class TestCommand extends HCommand<TestCommand, boolean> { | ||
public constructor(public readonly email: Email) { | ||
super(); | ||
} | ||
|
||
public toJSON(): JsonObjectType<TestCommand> { | ||
return { | ||
email: this.email.toJSON(), | ||
}; | ||
} | ||
} | ||
|
||
class TestEvent implements IEvent { | ||
public readonly email: Email; | ||
public constructor(emailRaw: string) { | ||
this.email = Email.c(emailRaw).v; | ||
class TestEvent extends HEvent<TestEvent> { | ||
public constructor(public readonly email: Email) { | ||
super(); | ||
} | ||
|
||
public toJSON(): JsonObjectType<TestEvent> { | ||
return { | ||
email: this.email.toJSON(), | ||
}; | ||
} | ||
} | ||
|
||
class TestQuery implements IQuery { | ||
public readonly email: Email; | ||
public constructor(emailRaw: string) { | ||
this.email = Email.c(emailRaw).v; | ||
class TestQuery extends HQuery<TestQuery, boolean> { | ||
public constructor(public readonly email: Email) { | ||
super(); | ||
} | ||
|
||
public toJSON(): JsonObjectType<TestQuery> { | ||
return { | ||
email: this.email.toJSON(), | ||
}; | ||
} | ||
} | ||
|
||
|
@@ -50,35 +65,52 @@ describe('DefaultGeneralBus', () => { | |
}); | ||
|
||
describe('handleCommand', () => { | ||
const command = new TestCommand('[email protected]'); | ||
const command = new TestCommand(Email.cs('[email protected]')); | ||
|
||
test('when ok', async () => { | ||
const expectedResult = OKA(true); | ||
|
||
commandBus.expects('execute', command).andReturn(expectedResult.p); | ||
|
||
const currentResult = await gb.handleCommand(command); | ||
const current = await gb.handleCommand(command); | ||
|
||
expect(currentResult).toEqual(OK(true)); | ||
expect(current).toEqual(OK(true)); | ||
}); | ||
|
||
test('when error', async () => { | ||
const expectedResult = ERRA({ type: 'test' }); | ||
|
||
commandBus.expects('execute', command).andReturn(expectedResult.p); | ||
|
||
const currentResult = await gb.handleCommand(command); | ||
const current = await gb.handleCommand(command); | ||
|
||
expect(currentResult).toEqual(ERR({ type: 'test' })); | ||
expect(current).toEqual(ERR({ type: 'test' })); | ||
}); | ||
}); | ||
|
||
describe('handleEvent', () => { | ||
const event: TestEvent = new TestEvent(Email.cs('[email protected]')); | ||
|
||
test('when ok', async () => { | ||
eventBus.expects('publish', event).andReturn(Promise.resolve()); | ||
|
||
const current = await gb.handleEvent(event); | ||
|
||
expect(current).toEqual(OK(true)); | ||
}); | ||
|
||
test('when error', async () => { | ||
eventBus.expects('publish', event).andReturnWith(async () => { throw new Error("test"); }); | ||
|
||
const current = await gb.handleEvent(event); | ||
|
||
expect(current).toEqual(INTERNAL_ERR(new Error("test"))); | ||
}); | ||
}); | ||
|
||
describe('handleQuery', () => { | ||
const query = new TestQuery('[email protected]'); | ||
const query = new TestQuery(Email.cs('[email protected]')); | ||
|
||
test('when ok', async () => { | ||
const expectedResult = OKA(true); | ||
|
||
queryBus.expects('execute', query).andReturn(expectedResult.p); | ||
|
||
const currentResult = await gb.handleQuery(query); | ||
|
@@ -88,7 +120,6 @@ describe('DefaultGeneralBus', () => { | |
|
||
test('when error', async () => { | ||
const expectedResult = ERRA({ type: 'test' }); | ||
|
||
queryBus.expects('execute', query).andReturn(expectedResult.p); | ||
|
||
const currentResult = await gb.handleQuery(query); | ||
|
Oops, something went wrong.