-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: find last acceded file when uploading contents
- Loading branch information
1 parent
b43a5b6
commit fe23037
Showing
11 changed files
with
113 additions
and
4 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
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
24 changes: 24 additions & 0 deletions
24
src/context/virtual-drive/contents/domain/events/ContentsAccededDomainEvent.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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { DomainEvent } from '../../../../shared/domain/DomainEvent'; | ||
|
||
export class ContentsAccededDomainEvent extends DomainEvent { | ||
static readonly EVENT_NAME = 'contents.acceded'; | ||
|
||
readonly path: string; | ||
|
||
constructor({ aggregateId, path }: { aggregateId: string; path: string }) { | ||
super({ | ||
eventName: ContentsAccededDomainEvent.EVENT_NAME, | ||
aggregateId, | ||
}); | ||
|
||
this.path = path; | ||
} | ||
|
||
toPrimitives() { | ||
return { | ||
aggregateId: this.aggregateId, | ||
eventId: this.eventId, | ||
path: this.path, | ||
}; | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
src/context/virtual-drive/files/application/FileToOverrideProvider.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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Optional } from '../../../../shared/types/Optional'; | ||
import { ContentsAccededDomainEvent } from '../../contents/domain/events/ContentsAccededDomainEvent'; | ||
import { EventRepository } from '../../shared/domain/EventRepository'; | ||
import { File } from '../domain/File'; | ||
import { FileRepository } from '../domain/FileRepository'; | ||
import { FileNotFoundError } from '../domain/errors/FileNotFoundError'; | ||
|
||
export class FileToOverrideProvider { | ||
constructor( | ||
private readonly eventRepository: EventRepository, | ||
private readonly repository: FileRepository | ||
) {} | ||
|
||
async run(): Promise<Optional<File>> { | ||
const eventOptional = | ||
await this.eventRepository.last<ContentsAccededDomainEvent>( | ||
ContentsAccededDomainEvent.EVENT_NAME | ||
); | ||
|
||
if (!eventOptional.isPresent()) { | ||
return Optional.empty(); | ||
} | ||
|
||
const event = eventOptional.get(); | ||
|
||
const file = await this.repository.searchByContentsId(event.aggregateId); | ||
|
||
if (!file) { | ||
throw new FileNotFoundError(event.aggregateId); | ||
} | ||
|
||
return Optional.of(file); | ||
} | ||
} |
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,10 @@ | ||
import { Optional } from '../../../../shared/types/Optional'; | ||
import { DomainEvent } from '../../../shared/domain/DomainEvent'; | ||
|
||
export interface EventRepository { | ||
store(event: DomainEvent): Promise<void>; | ||
search(aggregateId: string): Promise<Array<DomainEvent>>; | ||
last<Event extends DomainEvent>( | ||
event: Event['eventName'] | ||
): Promise<Optional<Event>>; | ||
} |
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
9 changes: 9 additions & 0 deletions
9
tests/context/virtual-drive/shared/__mock__/EventRepositoryMock.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,14 +1,23 @@ | ||
import { DomainEvent } from '../../../../../src/context/shared/domain/DomainEvent'; | ||
import { EventRepository } from '../../../../../src/context/virtual-drive/shared/domain/EventRepository'; | ||
import { Optional } from '../../../../../src/shared/types/Optional'; | ||
|
||
export class EventRepositoryMock implements EventRepository { | ||
public readonly storeMock = jest.fn(); | ||
public readonly searchMock = jest.fn(); | ||
public readonly lastMock = jest.fn(); | ||
|
||
store(event: DomainEvent): Promise<void> { | ||
return this.storeMock(event); | ||
} | ||
|
||
search(aggregateId: string): Promise<DomainEvent[]> { | ||
return this.searchMock(aggregateId); | ||
} | ||
|
||
last<Event extends DomainEvent>( | ||
event: Event['eventName'] | ||
): Promise<Optional<Event>> { | ||
return this.lastMock(event); | ||
} | ||
} |