-
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
20 changed files
with
351 additions
and
14 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
5 changes: 5 additions & 0 deletions
5
api/src/Application/School/Query/Shooting/CountShootingsBySchoolQuery.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 CountShootingsBySchoolQuery implements IQuery { | ||
constructor(public readonly schoolId: string) {} | ||
} |
28 changes: 28 additions & 0 deletions
28
api/src/Application/School/Query/Shooting/CountShootingsBySchoolQueryHandler.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,28 @@ | ||
import { mock, instance, when, verify } from 'ts-mockito'; | ||
import { ShootingRepository } from 'src/Infrastructure/School/Repository/ShootingRepository'; | ||
import { CountShootingsBySchoolQueryHandler } from './CountShootingsBySchoolQueryHandler'; | ||
import { CountShootingsBySchoolQuery } from './CountShootingsBySchoolQuery'; | ||
|
||
describe('CountSchoolsQueryHandler', () => { | ||
it('testCountShootings', async () => { | ||
const shootingRepository = mock(ShootingRepository); | ||
when( | ||
shootingRepository.countBySchool('5eb3173b-97ab-4bbc-b31c-878d4bfafbc1') | ||
).thenResolve(2); | ||
|
||
const queryHandler = new CountShootingsBySchoolQueryHandler( | ||
instance(shootingRepository) | ||
); | ||
|
||
expect( | ||
await queryHandler.execute( | ||
new CountShootingsBySchoolQuery('5eb3173b-97ab-4bbc-b31c-878d4bfafbc1') | ||
) | ||
).toBe(2); | ||
verify( | ||
shootingRepository.countBySchool( | ||
'5eb3173b-97ab-4bbc-b31c-878d4bfafbc1' | ||
) | ||
).once(); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
api/src/Application/School/Query/Shooting/CountShootingsBySchoolQueryHandler.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,16 @@ | ||
import { QueryHandler } from '@nestjs/cqrs'; | ||
import { Inject } from '@nestjs/common'; | ||
import { CountShootingsBySchoolQuery } from './CountShootingsBySchoolQuery'; | ||
import { IShootingRepository } from 'src/Domain/School/Repository/IShootingRepository'; | ||
|
||
@QueryHandler(CountShootingsBySchoolQuery) | ||
export class CountShootingsBySchoolQueryHandler { | ||
constructor( | ||
@Inject('IShootingRepository') | ||
private readonly shootingRepository: IShootingRepository | ||
) {} | ||
|
||
public execute({ schoolId }: CountShootingsBySchoolQuery): Promise<number> { | ||
return this.shootingRepository.countBySchool(schoolId); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
api/src/Application/School/Query/Shooting/GetShootingsBySchoolQuery.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 GetShootingsBySchoolQuery implements IQuery { | ||
constructor(public readonly schoolId: string) {} | ||
} |
55 changes: 55 additions & 0 deletions
55
api/src/Application/School/Query/Shooting/GetShootingsBySchoolQueryHandler.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 } from 'ts-mockito'; | ||
import { Shooting, ShootingStatus } from 'src/Domain/School/Shooting.entity'; | ||
import { ShootingView } from '../../View/ShootingView'; | ||
import { GetShootingsBySchoolQuery } from './GetShootingsBySchoolQuery'; | ||
import { GetShootingsBySchoolQueryHandler } from './GetShootingsBySchoolQueryHandler'; | ||
import { ShootingRepository } from 'src/Infrastructure/School/Repository/ShootingRepository'; | ||
import { School } from 'src/Domain/School/School.entity'; | ||
|
||
describe('GetShootingBySchoolQueryHandler', () => { | ||
it('testGetShootings', async () => { | ||
const shootingRepository = mock(ShootingRepository); | ||
|
||
const school = mock(School); | ||
const shooting = mock(Shooting); | ||
when(shooting.getId()).thenReturn( | ||
'4de2ffc4-e835-44c8-95b7-17c171c09873' | ||
); | ||
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.getSchool()).thenReturn(instance(school)); | ||
when(shooting.getStatus()).thenReturn(ShootingStatus.DISABLED); | ||
|
||
when( | ||
shootingRepository.findBySchool( | ||
'5eb3173b-97ab-4bbc-b31c-878d4bfafbc1' | ||
) | ||
).thenResolve([instance(shooting)]); | ||
|
||
const queryHandler = new GetShootingsBySchoolQueryHandler( | ||
instance(shootingRepository) | ||
); | ||
|
||
const expectedResult = [ | ||
new ShootingView( | ||
'4de2ffc4-e835-44c8-95b7-17c171c09873', | ||
'Prise de vue fin année', | ||
ShootingStatus.DISABLED, | ||
new Date('2021-04-18'), | ||
new Date('2021-09-01') | ||
) | ||
]; | ||
|
||
expect( | ||
await queryHandler.execute( | ||
new GetShootingsBySchoolQuery('5eb3173b-97ab-4bbc-b31c-878d4bfafbc1') | ||
) | ||
).toMatchObject(expectedResult); | ||
verify( | ||
shootingRepository.findBySchool( | ||
'5eb3173b-97ab-4bbc-b31c-878d4bfafbc1' | ||
) | ||
).once(); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
api/src/Application/School/Query/Shooting/GetShootingsBySchoolQueryHandler.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 { QueryHandler } from '@nestjs/cqrs'; | ||
import { Inject } from '@nestjs/common'; | ||
import { GetShootingsBySchoolQuery } from './GetShootingsBySchoolQuery'; | ||
import { IShootingRepository } from 'src/Domain/School/Repository/IShootingRepository'; | ||
import { ShootingView } from '../../View/ShootingView'; | ||
|
||
@QueryHandler(GetShootingsBySchoolQuery) | ||
export class GetShootingsBySchoolQueryHandler { | ||
constructor( | ||
@Inject('IShootingRepository') | ||
private readonly shootingRepository: IShootingRepository | ||
) {} | ||
|
||
public async execute({ schoolId }: GetShootingsBySchoolQuery): Promise<ShootingView[]> { | ||
const shootingViews: ShootingView[] = []; | ||
const shootings = await this.shootingRepository.findBySchool( | ||
schoolId | ||
); | ||
|
||
for (const shooting of shootings) { | ||
shootingViews.push( | ||
new ShootingView( | ||
shooting.getId(), | ||
shooting.getName(), | ||
shooting.getStatus(), | ||
shooting.getShootingDate(), | ||
shooting.getClosingDate(), | ||
) | ||
); | ||
} | ||
|
||
return shootingViews; | ||
} | ||
} |
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,11 @@ | ||
import { ShootingStatus } from 'src/Domain/School/Shooting.entity'; | ||
|
||
export class ShootingView { | ||
constructor( | ||
public readonly id: string, | ||
public readonly name: string, | ||
public readonly status: ShootingStatus, | ||
public readonly shootingDate: Date, | ||
public readonly closingDate: Date, | ||
) {} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
api/src/Infrastructure/School/Action/Shooting/CountSchoolShootingsAction.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,33 @@ | ||
import { Controller, Inject, UseGuards, Get, Param, NotFoundException } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger'; | ||
import { IQueryBus } from 'src/Application/IQueryBus'; | ||
import { CountShootingsBySchoolQuery } from 'src/Application/School/Query/Shooting/CountShootingsBySchoolQuery'; | ||
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') | ||
@ApiTags('School shooting') | ||
@ApiBearerAuth() | ||
@UseGuards(AuthGuard('bearer'), RolesGuard) | ||
export class CountSchoolShootingsAction { | ||
constructor( | ||
@Inject('IQueryBus') | ||
private readonly queryBus: IQueryBus | ||
) {} | ||
|
||
@Get(':id/count-shootings') | ||
@Roles(UserRole.PHOTOGRAPHER) | ||
@ApiOperation({ summary: 'Count school shootings' }) | ||
public async index(@Param() { id }: IdDTO) { | ||
try { | ||
const total = await this.queryBus.execute(new CountShootingsBySchoolQuery(id)); | ||
|
||
return { total }; | ||
} catch (e) { | ||
throw new NotFoundException(e.message); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
api/src/Infrastructure/School/Action/Shooting/GetSchoolShootingsAction.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,32 @@ | ||
import { Controller, Inject, UseGuards, Get, Param, NotFoundException } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger'; | ||
import { IQueryBus } from 'src/Application/IQueryBus'; | ||
import { IdDTO } from 'src/Infrastructure/Common/DTO/IdDTO'; | ||
import { Roles } from 'src/Infrastructure/User/Decorator/Roles'; | ||
import { RolesGuard } from 'src/Infrastructure/User/Security/RolesGuard'; | ||
import { UserRole } from 'src/Domain/User/User.entity'; | ||
import { ShootingView } from 'src/Application/School/View/ShootingView'; | ||
import { GetShootingsBySchoolQuery } from 'src/Application/School/Query/Shooting/GetShootingsBySchoolQuery'; | ||
|
||
@Controller('schools') | ||
@ApiTags('School shooting') | ||
@ApiBearerAuth() | ||
@UseGuards(AuthGuard('bearer'), RolesGuard) | ||
export class GetSchoolShootingsAction { | ||
constructor( | ||
@Inject('IQueryBus') | ||
private readonly queryBus: IQueryBus | ||
) {} | ||
|
||
@Get(':id/shootings') | ||
@Roles(UserRole.PHOTOGRAPHER) | ||
@ApiOperation({ summary: 'Get school shootings' }) | ||
public async index(@Param() dto: IdDTO): Promise<ShootingView[]> { | ||
try { | ||
return await this.queryBus.execute(new GetShootingsBySchoolQuery(dto.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
Oops, something went wrong.