-
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
11 changed files
with
214 additions
and
2 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
api/src/Application/School/Query/Shooting/GetSchootingByIdQuery.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,5 @@ | ||
import { IQuery } from 'src/Application/IQuery'; | ||
|
||
export class GetShootingByIdQuery implements IQuery { | ||
constructor(public readonly id: string) {} | ||
} |
57 changes: 57 additions & 0 deletions
57
api/src/Application/School/Query/Shooting/GetShootingByIdQueryHandler.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,57 @@ | ||
import { mock, instance, when, verify } from 'ts-mockito'; | ||
import { Shooting, ShootingStatus } from 'src/Domain/School/Shooting.entity'; | ||
import { ShootingRepository } from 'src/Infrastructure/School/Repository/ShootingRepository'; | ||
import { ShootingView } from '../../View/ShootingView'; | ||
import { GetShootingByIdQuery } from './GetSchootingByIdQuery'; | ||
import { GetShootingByIdQueryHandler } from './GetShootingByIdQueryHandler'; | ||
import { ShootingNotFoundException } from 'src/Domain/School/Exception/ShootingNotFoundException'; | ||
|
||
describe('GetShootingByIdQueryHandler', () => { | ||
const query = new GetShootingByIdQuery('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2'); | ||
|
||
it('testGetShooting', async () => { | ||
const shootingRepository = mock(ShootingRepository); | ||
const queryHandler = new GetShootingByIdQueryHandler(instance(shootingRepository)); | ||
const expectedResult = new ShootingView( | ||
'eb9e1d9b-dce2-48a9-b64f-f0872f3157d2', | ||
'Prise de vue fin année', | ||
ShootingStatus.DISABLED, | ||
new Date('2021-04-18'), | ||
new Date('2021-09-01') | ||
); | ||
|
||
const shooting = mock(Shooting); | ||
when(shooting.getId()).thenReturn('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2'); | ||
when(shooting.getName()).thenReturn('Prise de vue fin année'); | ||
when(shooting.getClosingDate()).thenReturn(new Date('2021-09-01')); | ||
when(shooting.getShootingDate()).thenReturn(new Date('2021-04-18')); | ||
when(shooting.getStatus()).thenReturn(ShootingStatus.DISABLED); | ||
when( | ||
shootingRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') | ||
).thenResolve(instance(shooting)); | ||
|
||
expect(await queryHandler.execute(query)).toMatchObject(expectedResult); | ||
|
||
verify( | ||
shootingRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') | ||
).once(); | ||
}); | ||
|
||
it('testGetShootingNotFound', async () => { | ||
const shootingRepository = mock(ShootingRepository); | ||
const queryHandler = new GetShootingByIdQueryHandler(instance(shootingRepository)); | ||
when( | ||
shootingRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') | ||
).thenResolve(null); | ||
|
||
try { | ||
expect(await queryHandler.execute(query)).toBeUndefined(); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(ShootingNotFoundException); | ||
expect(e.message).toBe('schools.shootings.errors.not_found'); | ||
verify( | ||
shootingRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') | ||
).once(); | ||
} | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
api/src/Application/School/Query/Shooting/GetShootingByIdQueryHandler.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,30 @@ | ||
import { QueryHandler } from '@nestjs/cqrs'; | ||
import { Inject } from '@nestjs/common'; | ||
import { GetShootingByIdQuery } from './GetSchootingByIdQuery'; | ||
import { IShootingRepository } from 'src/Domain/School/Repository/IShootingRepository'; | ||
import { ShootingView } from '../../View/ShootingView'; | ||
import { ShootingNotFoundException } from 'src/Domain/School/Exception/ShootingNotFoundException'; | ||
|
||
@QueryHandler(GetShootingByIdQuery) | ||
export class GetShootingByIdQueryHandler { | ||
constructor( | ||
@Inject('IShootingRepository') | ||
private readonly shootingRepository: IShootingRepository | ||
) {} | ||
|
||
public async execute(query: GetShootingByIdQuery): Promise<ShootingView> { | ||
const shooting = await this.shootingRepository.findOneById(query.id); | ||
|
||
if (!shooting) { | ||
throw new ShootingNotFoundException(); | ||
} | ||
|
||
return new ShootingView( | ||
shooting.getId(), | ||
shooting.getName(), | ||
shooting.getStatus(), | ||
shooting.getShootingDate(), | ||
shooting.getClosingDate() | ||
); | ||
} | ||
} |
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 @@ | ||
export class ShootingNotFoundException extends Error { | ||
constructor() { | ||
super('schools.shootings.errors.not_found'); | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
api/src/Infrastructure/School/Action/Shooting/GetShootingAction.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,39 @@ | ||
import { | ||
Controller, | ||
Inject, | ||
UseGuards, | ||
Param, | ||
Get, | ||
NotFoundException | ||
} from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger'; | ||
import { IQueryBus } from 'src/Application/IQueryBus'; | ||
import { GetShootingByIdQuery } from 'src/Application/School/Query/Shooting/GetSchootingByIdQuery'; | ||
import { ShootingView } from 'src/Application/School/View/ShootingView'; | ||
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('schools/:schoolId/shootings') | ||
@ApiTags('School shooting') | ||
@ApiBearerAuth() | ||
@UseGuards(AuthGuard('bearer'), RolesGuard) | ||
export class GetShootingAction { | ||
constructor( | ||
@Inject('IQueryBus') | ||
private readonly queryBus: IQueryBus | ||
) {} | ||
|
||
@Get(':id') | ||
@Roles(UserRole.PHOTOGRAPHER) | ||
@ApiOperation({ summary: 'Get shooting' }) | ||
public async index(@Param() { id }: IdDTO): Promise<ShootingView> { | ||
try { | ||
return await this.queryBus.execute(new GetShootingByIdQuery(id)); | ||
} catch (e) { | ||
throw new NotFoundException(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
52 changes: 52 additions & 0 deletions
52
client/src/routes/admin/schools/[id]/shootings/[shootingId]/index.svelte
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,52 @@ | ||
<script context="module"> | ||
export const preload = ({ params: { id, shootingId } }) => { | ||
return { id, shootingId }; | ||
}; | ||
</script> | ||
|
||
<script> | ||
import { _ } from 'svelte-i18n'; | ||
import { onMount } from 'svelte'; | ||
import { get } from 'utils/axios'; | ||
import Breadcrumb from 'components/Breadcrumb.svelte'; | ||
import { errorNormalizer } from 'normalizer/errors'; | ||
import ServerErrors from 'components/ServerErrors.svelte'; | ||
import H4Title from 'components/H4Title.svelte'; | ||
export let id; | ||
export let shootingId; | ||
let title; | ||
let school; | ||
let shooting; | ||
let errors = []; | ||
onMount(async () => { | ||
try { | ||
const [ schoolResponse, shootingResponse ] = await Promise.all([ | ||
get(`schools/${id}`), | ||
get(`schools/${id}/shootings/${shootingId}`) | ||
]); | ||
school = schoolResponse.data; | ||
shooting = shootingResponse.data; | ||
title = shooting.name; | ||
} catch (e) { | ||
errors = errorNormalizer(e); | ||
} | ||
}); | ||
</script> | ||
|
||
<svelte:head> | ||
<title>{title} - {$_('app')}</title> | ||
</svelte:head> | ||
|
||
<Breadcrumb items={[ | ||
{ title: $_('schools.breadcrumb'), path: '/admin/schools' }, | ||
{ title: school?.name, path: `/admin/schools/${id}` }, | ||
{ title: $_('schools.shootings.title'), path: `/admin/schools/${id}/shootings` }, | ||
{ title } | ||
]} /> | ||
<div class="inline-flex items-center"> | ||
<H4Title {title} /> | ||
</div> | ||
<ServerErrors {errors} /> |
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