-
-
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
31 changed files
with
263 additions
and
178 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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { AnyResult, Fail, NoResult, Ok, Result } from '@lib/core' | ||
import { Command, AnyCommand } from './Command' | ||
|
||
|
||
export class ProcessorResult<TCommandResult extends AnyResult> { | ||
constructor( | ||
public readonly processorResult: Result<boolean, string>, | ||
public readonly commandResult: TCommandResult = NoResult as TCommandResult, | ||
) {} | ||
|
||
/** | ||
* Indicates if the command is executed. | ||
* @returns True if the command is executed, otherwise false. | ||
* @note This property is true if the command is executed successfully or failed. | ||
*/ | ||
get isCommandExecuted(): boolean { | ||
return this.processorResult.isSuccess | ||
} | ||
|
||
/** | ||
* Indicates if the command is executed successfully. | ||
* @returns True if the command is executed successfully, otherwise false. | ||
*/ | ||
get isCommandSucceeded(): boolean { | ||
return this.commandResult.isSuccess | ||
} | ||
|
||
/** | ||
* Gets the result of the executed command. | ||
* @returns {TCommandResult['value']} Returns the result of the executed command. | ||
*/ | ||
get value() : TCommandResult['value'] | TCommandResult['error'] { | ||
return this.commandResult.value | ||
} | ||
} | ||
|
||
/** | ||
* Processor executes commands. | ||
*/ | ||
export class Processor<TContext> { | ||
private stack: AnyCommand[] = [] | ||
|
||
/** | ||
* Initialize a new instance of the Processor class. | ||
* @param context Context of the processor. | ||
*/ | ||
constructor( | ||
public readonly context: TContext | ||
) { } | ||
|
||
/** | ||
* Excecute the command. | ||
* @param command Command to process. | ||
* @returns {ProcessorResult<TResult>} Returns the result execution. | ||
*/ | ||
execute<TResult extends AnyResult>( | ||
command: Command<TContext, TResult> | ||
): ProcessorResult<TResult> { | ||
if (this.stack.includes(command)) { | ||
return new ProcessorResult(Fail('Command is already executed.')) | ||
} | ||
this.stack.push(command) | ||
const commandResult = command.execute(this.context) | ||
return new ProcessorResult<TResult>(Ok(), commandResult) | ||
} | ||
|
||
/** | ||
* Revert the last executed command. | ||
*/ | ||
revert<TResult extends AnyResult>(): ProcessorResult<TResult> { | ||
const command = this.stack.pop() | ||
if (!command) { | ||
return new ProcessorResult(Fail('No command to revert.')) | ||
} | ||
|
||
const commandResult = command.revert(this.context) | ||
return new ProcessorResult<TResult>(Ok(), commandResult) | ||
} | ||
} |
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,2 +1,2 @@ | ||
export { ICommand } from './command' | ||
export { Processor } from './processor' | ||
export * from './Command' | ||
export * from './Processor' |
This file was deleted.
Oops, something went wrong.
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 +1 @@ | ||
export * from './result' | ||
export * from './Result' |
4 changes: 2 additions & 2 deletions
4
lib/domain/models/aggregate.ts → lib/domain/models/Aggregate.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
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,12 @@ | ||
import { Result } from '@lib/core' | ||
|
||
/** | ||
* Builds a new instance of the specified type or returns a failure result. | ||
*/ | ||
export abstract class Builder<TModel, TError> { | ||
/** | ||
* Builds a new instance of the specified type or returns a failure result. | ||
* @returns A new instance of the specified type or a failure result. | ||
*/ | ||
public abstract build(): Result<TModel, TError> | ||
} |
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
File renamed without changes.
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,18 @@ | ||
import { Identity } from './Identity' | ||
|
||
|
||
/** | ||
* Identifiable is an interface for all objects that have an identity. | ||
*/ | ||
export interface Identifiable< | ||
TIdentity extends Identity<unknown, unknown> | ||
> { | ||
get id(): TIdentity | ||
} | ||
|
||
/** | ||
* Equalable is an interface for all objects that can be compared for equality. | ||
*/ | ||
export interface Equalable<T> { | ||
equals(other: T): boolean | ||
} |
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 was deleted.
Oops, something went wrong.
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,6 +1,6 @@ | ||
export * from './aggregate' | ||
export * from './builder' | ||
export * from './entity' | ||
export * from './identity' | ||
export * from './interfaces' | ||
export * from './value' | ||
export * from './Aggregate' | ||
export * from './Builder' | ||
export * from './Entity' | ||
export * from './Identity' | ||
export * from './Interfaces' | ||
export * from './Value' |
This file was deleted.
Oops, something went wrong.
6 changes: 3 additions & 3 deletions
6
lib/persistence/inmemory-repository.ts → lib/persistence/InMemoryRepository.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
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,3 +1,3 @@ | ||
export * from './query' | ||
export * from './repository' | ||
export * from './inmemory-repository' | ||
export * from './Query' | ||
export * from './Repository' | ||
export * from './InMemoryRepository' |
Oops, something went wrong.