-
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
8 changed files
with
161 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ICommand } from 'src/Application/ICommand'; | ||
|
||
export class RemoveEventCommand implements ICommand { | ||
constructor(public readonly id: string) {} | ||
} |
55 changes: 55 additions & 0 deletions
55
api/src/Application/Calendar/Command/RemoveEventCommandHandler.spec.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,55 @@ | ||
import { mock, instance, when, verify, anything } from 'ts-mockito'; | ||
import { RemoveEventCommandHandler } from './RemoveEventCommandHandler'; | ||
import { RemoveEventCommand } from './RemoveEventCommand'; | ||
import { EventRepository } from 'src/Infrastructure/Calendar/Repository/EventRepository'; | ||
import { Event } from 'src/Domain/Calendar/Event.entity'; | ||
import { EventNotFoundException } from 'src/Domain/Calendar/Exception/EventNotFoundException'; | ||
|
||
describe('RemoveEventCommandHandler', () => { | ||
let eventRepository: EventRepository; | ||
let removedEvent: Event; | ||
let handler: RemoveEventCommandHandler; | ||
|
||
const command = new RemoveEventCommand('17efcbee-bd2f-410e-9e99-51684b592bad'); | ||
|
||
beforeEach(() => { | ||
eventRepository = mock(EventRepository); | ||
removedEvent = mock(Event); | ||
|
||
handler = new RemoveEventCommandHandler( | ||
instance(eventRepository) | ||
); | ||
}); | ||
|
||
it('testEventRemovedSuccessfully', async () => { | ||
when(eventRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')) | ||
.thenResolve(instance(removedEvent)); | ||
when(removedEvent.getId()).thenReturn( | ||
'17efcbee-bd2f-410e-9e99-51684b592bad' | ||
); | ||
when( | ||
eventRepository.save(instance(removedEvent)) | ||
).thenResolve(instance(removedEvent)); | ||
|
||
await handler.execute(command); | ||
|
||
verify( | ||
eventRepository.remove(instance(removedEvent)) | ||
).once(); | ||
verify(eventRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')).once(); | ||
}); | ||
|
||
it('testEventNotFound', async () => { | ||
when(eventRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')) | ||
.thenResolve(null); | ||
|
||
try { | ||
expect(await handler.execute(command)).toBeUndefined(); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(EventNotFoundException); | ||
expect(e.message).toBe('calendar.errors.event_not_found'); | ||
verify(eventRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')).once(); | ||
verify(eventRepository.remove(anything())).never(); | ||
} | ||
}); | ||
}); |
23 changes: 23 additions & 0 deletions
23
api/src/Application/Calendar/Command/RemoveEventCommandHandler.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,23 @@ | ||
import { Inject } from '@nestjs/common'; | ||
import { CommandHandler } from '@nestjs/cqrs'; | ||
import { EventNotFoundException } from 'src/Domain/Calendar/Exception/EventNotFoundException'; | ||
import { IEventRepository } from 'src/Domain/Calendar/Repository/IEventRepository'; | ||
import { RemoveEventCommand } from './RemoveEventCommand'; | ||
|
||
@CommandHandler(RemoveEventCommand) | ||
export class RemoveEventCommandHandler { | ||
constructor( | ||
@Inject('IEventRepository') | ||
private readonly eventRepository: IEventRepository, | ||
) {} | ||
|
||
public async execute({ id }: RemoveEventCommand): Promise<void> { | ||
const event = await this.eventRepository.findOneById(id); | ||
|
||
if (!event) { | ||
throw new EventNotFoundException(); | ||
} | ||
|
||
await this.eventRepository.remove(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
38 changes: 38 additions & 0 deletions
38
api/src/Infrastructure/Calendar/Action/RemoveEventAction.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,38 @@ | ||
import { | ||
Controller, | ||
Inject, | ||
BadRequestException, | ||
UseGuards, | ||
Param, | ||
Delete | ||
} from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger'; | ||
import { RemoveEventCommand } from 'src/Application/Calendar/Command/RemoveEventCommand'; | ||
import { ICommandBus } from 'src/Application/ICommandBus'; | ||
import { UserRole } from 'src/Domain/User/User.entity'; | ||
import { IdDTO } from 'src/Infrastructure/Common/DTO/IdDTO'; | ||
import { Roles } from 'src/Infrastructure/User/Decorator/Roles'; | ||
import { RolesGuard } from 'src/Infrastructure/User/Security/RolesGuard'; | ||
|
||
@Controller('events') | ||
@ApiTags('Calendar') | ||
@ApiBearerAuth() | ||
@UseGuards(AuthGuard('bearer'), RolesGuard) | ||
export class RemoveEventAction { | ||
constructor( | ||
@Inject('ICommandBus') | ||
private readonly commandBus: ICommandBus | ||
) {} | ||
|
||
@Delete(':id') | ||
@Roles(UserRole.PHOTOGRAPHER) | ||
@ApiOperation({ summary: 'Remove event' }) | ||
public async index(@Param() { id }: IdDTO) { | ||
try { | ||
await this.commandBus.execute(new RemoveEventCommand(id)); | ||
} catch (e) { | ||
throw new BadRequestException(e.message); | ||
} | ||
} | ||
} |
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