Skip to content

Commit

Permalink
refactor: renames branches and collections to files and filetrees
Browse files Browse the repository at this point in the history
  • Loading branch information
samhatoum committed May 14, 2020
1 parent a872f35 commit f617b94
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 114 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ jobs:
with:
coverageLocations: |
${{github.workspace}}/coverage/jest/lcov.info:lcov
${{github.workspace}}/coverage/cucumber/lcov.info:lcov
debug: true
- name: Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# - name: Release
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
# run: npx semantic-release
28 changes: 0 additions & 28 deletions src/domain/branch/Branch.spec.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/domain/branch/BranchCommandHandlers.ts

This file was deleted.

28 changes: 28 additions & 0 deletions src/domain/file/File.spec.ts
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)
}))
12 changes: 6 additions & 6 deletions src/domain/branch/Branch.ts → src/domain/file/File.ts
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))
}
}
19 changes: 19 additions & 0 deletions src/domain/file/FileCommandHandlers.ts
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)
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {Command} from '../../es-cqrs/Command'

export class CreateBranch extends Command {
export class CreateFile extends Command {
constructor(public readonly name: string) {
super(-1)
}
}

export class RenameBranch extends Command {
export class RenameFile extends Command {
constructor(
public readonly aggregateId: string,
public readonly name: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Event} from '../../es-cqrs/Event'

export class BranchCreated extends Event {
export class FileCreated extends Event {
constructor(
public readonly aggregateId: string,
public readonly name: string
Expand All @@ -9,7 +9,7 @@ export class BranchCreated extends Event {
}
}

export class BranchRenamed extends Event {
export class FileRenamed extends Event {
constructor(
public readonly aggregateId: string,
public readonly name: string
Expand Down
14 changes: 8 additions & 6 deletions src/es-cqrs/GivenWhenThen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,31 @@ import {MessageBus} from './MessageBus'
import {EventStore} from './EventStore'
import {Repository} from './Repository'

export function GivenWhenThen<T>(aggregate: T, dirname: string): Function {
console.log(dirname)
export function GivenWhenThen(aggregatePath: string): Function {
return (cb: Function): Function => {
const dir = aggregatePath.substring(0, aggregatePath.lastIndexOf('/'))
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function req(moduleName, extract = false): any {
if (extract)
// eslint-disable-next-line global-require,import/no-dynamic-require
return require(`${dirname}/${moduleName}`)[`${moduleName}`]
return require(`${dir}/${moduleName}`)[`${moduleName}`]
// eslint-disable-next-line global-require,import/no-dynamic-require
return require(`${dirname}/${moduleName}`)
return require(`${dir}/${moduleName}`)
}

// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
const aggregateName = aggregate.name
const aggregateName = aggregatePath.substring(
aggregatePath.lastIndexOf('/') + 1
)

const Aggregate = req(aggregateName, true)
const AggregateCommandHandlers = req(
`${aggregateName}CommandHandlers`,
true
)
// eslint-disable-next-line @typescript-eslint/no-var-requires,global-require,import/no-dynamic-require
const AggregateCommands = require(`${dirname}/${aggregateName}Commands`)
const AggregateCommands = require(`${dir}/${aggregateName}Commands`)

const messageBus = new MessageBus()
const eventStore = new EventStore(messageBus)
Expand Down
30 changes: 0 additions & 30 deletions src/projections/Collection.spec.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/projections/Collection.ts

This file was deleted.

34 changes: 34 additions & 0 deletions src/projections/FileTree.spec.ts
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')
}))
17 changes: 17 additions & 0 deletions src/projections/FileTree.ts
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
})
}
}

0 comments on commit f617b94

Please sign in to comment.