forked from xolvio/typescript-event-sourcing
-
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.
refactor: renames branches and collections to files and filetrees
- Loading branch information
Showing
13 changed files
with
119 additions
and
114 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {GivenWhenThen} from '../../es-cqrs/GivenWhenThen' | ||
import {guid} from '../../es-cqrs/Guid' | ||
import {File} from './File' | ||
import {RenameFile} from './FileCommands' | ||
import {FileCreated, FileRenamed} from './FileEvents' | ||
import {MissingParameterError} from '../../es-cqrs/Errors' | ||
|
||
const GWT = GivenWhenThen(`${__dirname}/File`) | ||
|
||
test('Rename file', () => | ||
GWT((Given, When, Then) => { | ||
const fileId = guid() | ||
|
||
Given(new FileCreated(fileId, 'foo')) | ||
When(new RenameFile(fileId, 'bar', 1)) | ||
Then(new FileRenamed(fileId, 'bar')) | ||
})) | ||
|
||
test('Rename file fail', () => | ||
GWT((Given, When, Then) => { | ||
const fileId = guid() | ||
|
||
Given(new FileCreated(fileId, 'foo')) | ||
When(new RenameFile(fileId, '', 1)) | ||
expect((): void => { | ||
Then(new FileRenamed(fileId, 'bar')) | ||
}).toThrow(MissingParameterError) | ||
})) |
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,27 +1,27 @@ | ||
import {AggregateRoot} from '../../es-cqrs/AggregateRoot' | ||
import {BranchCreated, BranchRenamed} from './BranchEvents' | ||
import {FileCreated, FileRenamed} from './FileEvents' | ||
import {MissingParameterError} from '../../es-cqrs/Errors' | ||
|
||
export class Branch extends AggregateRoot { | ||
export class File extends AggregateRoot { | ||
private name: string | ||
|
||
constructor(guid: string, name: string) { | ||
super() | ||
this.applyChange(new BranchCreated(guid, name)) | ||
this.applyChange(new FileCreated(guid, name)) | ||
} | ||
|
||
applyBranchCreated(event: BranchCreated): void { | ||
applyFileCreated(event: FileCreated): void { | ||
this._id = event.aggregateId | ||
this.name = event.name | ||
} | ||
|
||
applyBranchRenamed(event: BranchRenamed): void { | ||
applyFileRenamed(event: FileRenamed): void { | ||
this._id = event.aggregateId | ||
this.name = event.name | ||
} | ||
|
||
rename(name: string): void { | ||
if (!name) throw new MissingParameterError('Must provide a name') | ||
this.applyChange(new BranchRenamed(this._id, name)) | ||
this.applyChange(new FileRenamed(this._id, name)) | ||
} | ||
} |
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,19 @@ | ||
import {IRepository} from '../../es-cqrs/Repository' | ||
import {File} from './File' | ||
import {CreateFile, RenameFile} from './FileCommands' | ||
import {guid} from '../../es-cqrs/Guid' | ||
|
||
export class FileCommandHandlers { | ||
constructor(private _repository: IRepository<File>) {} | ||
|
||
handleCreateFile(command: CreateFile): void { | ||
const file = new File(guid(), command.name) | ||
this._repository.save(file, -1) | ||
} | ||
|
||
handleRenameFile(command: RenameFile): void { | ||
const file = this._repository.getById(command.aggregateId) | ||
file.rename(command.name) | ||
this._repository.save(file, command.expectedAggregateVersion) | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
src/domain/branch/BranchCommands.ts → src/domain/file/FileCommands.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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {GivenWhenThen} from '../es-cqrs/GivenWhenThen' | ||
import {FileCreated, FileRenamed} from '../domain/file/FileEvents' | ||
import {FileTree} from './FileTree' | ||
|
||
const GWT = GivenWhenThen(`${__dirname}/../domain/file/File`) | ||
|
||
test('FileTree Projection', () => | ||
GWT((Given, When, Then, messageBus) => { | ||
const fileTree = new FileTree(messageBus) | ||
|
||
Given( | ||
new FileCreated('1234', 'foo'), | ||
new FileCreated('2345', 'bar'), | ||
new FileCreated('3456', 'baz') | ||
) | ||
|
||
expect(fileTree.files['1234'].name).toEqual('foo') | ||
expect(fileTree.files['2345'].name).toEqual('bar') | ||
expect(fileTree.files['3456'].name).toEqual('baz') | ||
})) | ||
|
||
test('FileTree Projection', () => | ||
GWT((Given, When, Then, messageBus) => { | ||
const fileTree = new FileTree(messageBus) | ||
|
||
Given( | ||
new FileCreated('1234', 'foo'), | ||
new FileCreated('5678', 'bar'), | ||
new FileRenamed('5678', 'baz') | ||
) | ||
|
||
expect(fileTree.files['1234'].name).toEqual('foo') | ||
expect(fileTree.files['5678'].name).toEqual('baz') | ||
})) |
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,17 @@ | ||
import {IMessageBus} from '../es-cqrs/IMessageBus' | ||
import {FileCreated, FileRenamed} from '../domain/file/FileEvents' | ||
|
||
export class FileTree { | ||
public files = {} | ||
|
||
constructor(private messageBus: IMessageBus) { | ||
messageBus.registerEventHandler(FileCreated, (e) => { | ||
const event = e as FileCreated | ||
this.files[event.aggregateId] = event | ||
}) | ||
messageBus.registerEventHandler(FileRenamed, (e) => { | ||
const event = e as FileRenamed | ||
this.files[event.aggregateId].name = event.name | ||
}) | ||
} | ||
} |