Skip to content

Commit

Permalink
feat: domain & usecases implementation / project folder structure
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavoTrizotti committed Jun 25, 2024
1 parent 3a7f7e6 commit ac0b870
Show file tree
Hide file tree
Showing 125 changed files with 3,731 additions and 142 deletions.
11 changes: 6 additions & 5 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "@rocketseat/eslint-config/node",
"rules": {
"no-useless-constructor": "off"
}
}
"extends": "@rocketseat/eslint-config/node",
"rules": {
"no-useless-constructor": "off",
"no-new": "off"
}
}
1 change: 1 addition & 0 deletions nest-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"entryFile": "infra/main",
"compilerOptions": {
"deleteOutDir": true
}
Expand Down
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"test:e2e": "vitest run --config ./vitest.config.e2e.ts"
},
"dependencies": {
"@faker-js/faker": "^8.4.1",
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.2.2",
"@nestjs/core": "^10.0.0",
Expand All @@ -32,6 +33,7 @@
"bcryptjs": "^2.4.3",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"dayjs": "^1.11.11",
"dotenv": "^16.4.5",
"eslint": "^8.57.0",
"passport": "^0.7.0",
Expand Down
10 changes: 0 additions & 10 deletions src/auth/current-user-decorator.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/auth/jwt-auth.guard.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/controllers/test.controller.ts

This file was deleted.

23 changes: 23 additions & 0 deletions src/core/either.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Either, left, right } from '@/core/either'

function doSomeThing(shouldSuccess: boolean): Either<string, number> {
if (shouldSuccess) {
return right(10)
} else {
return left('error')
}
}

test('success result', () => {
const result = doSomeThing(true)

expect(result.isRight()).toBe(true)
expect(result.isLeft()).toBe(false)
})

test('error result', () => {
const result = doSomeThing(false)

expect(result.isLeft()).toBe(true)
expect(result.isRight()).toBe(false)
})
43 changes: 43 additions & 0 deletions src/core/either.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Error
export class Left<L, R> {
readonly value: L

constructor(value: L) {
this.value = value
}

isRight(): this is Right<L, R> {
return false
}

isLeft(): this is Left<L, R> {
return true
}
}

// Success
export class Right<L, R> {
readonly value: R

constructor(value: R) {
this.value = value
}

isRight(): this is Right<L, R> {
return true
}

isLeft(): this is Left<L, R> {
return false
}
}

export type Either<L, R> = Left<L, R> | Right<L, R>

export const left = <L, R>(value: L): Either<L, R> => {
return new Left(value)
}

export const right = <L, R>(value: R): Either<L, R> => {
return new Right(value)
}
20 changes: 20 additions & 0 deletions src/core/entities/aggregate-root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { DomainEvents } from '@/core/events/domain-events'
import { DomainEvent } from '../events/domain-event'
import { Entity } from './entity'

export abstract class AggregateRoot<Props> extends Entity<Props> {
private _domainEvents: DomainEvent[] = []

get domainEvents(): DomainEvent[] {
return this._domainEvents
}

protected addDomainEvent(domainEvent: DomainEvent): void {
this._domainEvents.push(domainEvent)
DomainEvents.markAggregateForDispatch(this)
}

public clearEvents() {
this._domainEvents = []
}
}
27 changes: 27 additions & 0 deletions src/core/entities/entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { UniqueEntityID } from './unique-entity-id'

export abstract class Entity<Props> {
private _id: UniqueEntityID
protected props: Props

get id() {
return this._id
}

protected constructor(props: Props, id?: UniqueEntityID) {
this.props = props
this._id = id ?? new UniqueEntityID()
}

public equals(entity: Entity<unknown>) {
if (entity === this) {
return true
}

if (entity.id === this._id) {
return true
}

return false
}
}
21 changes: 21 additions & 0 deletions src/core/entities/unique-entity-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { randomUUID } from 'node:crypto'

export class UniqueEntityID {
private value: string

toString() {
return this.value
}

toValue() {
return this.value
}

constructor(value?: string) {
this.value = value ?? randomUUID()
}

public equals(id: UniqueEntityID) {
return id.toValue() === this.value
}
}
66 changes: 66 additions & 0 deletions src/core/entities/watched-list.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { WatchedList } from '@/core/entities/watched-list'

class NumberWatchedList extends WatchedList<number> {
compareItems(a: number, b: number): boolean {
return a === b
}
}

describe('watched list', () => {
it('should be able to create a watched list with initial items', () => {
const list = new NumberWatchedList([1, 2, 3])

expect(list.currentItems).toHaveLength(3)
})

it('should be able to add new items to the list', () => {
const list = new NumberWatchedList([1, 2, 3])

list.add(4)

expect(list.currentItems).toHaveLength(4)
expect(list.getNewItems()).toEqual([4])
})

it('should be able to remove items from the list', () => {
const list = new NumberWatchedList([1, 2, 3])

list.remove(2)

expect(list.currentItems).toHaveLength(2)
expect(list.getRemovedItems()).toEqual([2])
})

it('should be able to add an item even if it was removed before', () => {
const list = new NumberWatchedList([1, 2, 3])

list.remove(2)
list.add(2)

expect(list.currentItems).toHaveLength(3)

expect(list.getRemovedItems()).toEqual([])
expect(list.getNewItems()).toEqual([])
})

it('should be able to remove an item even if it was added before', () => {
const list = new NumberWatchedList([1, 2, 3])

list.add(4)
list.remove(4)

expect(list.currentItems).toHaveLength(3)

expect(list.getRemovedItems()).toEqual([])
expect(list.getNewItems()).toEqual([])
})

it('should be able to update watched list items', () => {
const list = new NumberWatchedList([1, 2, 3])

list.update([1, 3, 5])

expect(list.getRemovedItems()).toEqual([2])
expect(list.getNewItems()).toEqual([5])
})
})
Loading

0 comments on commit ac0b870

Please sign in to comment.