Skip to content

Commit 6c6d4f3

Browse files
Affiche tous les Clients dans le formulaire Projet (#455)
1 parent 047d2dd commit 6c6d4f3

File tree

6 files changed

+50
-11
lines changed

6 files changed

+50
-11
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { IQuery } from 'src/Application/IQuery';
22

33
export class GetCustomersQuery implements IQuery {
4-
constructor(public readonly page: number) {}
4+
constructor(public readonly page: number | null) {}
55
}

src/Application/Customer/Query/GetCustomersQueryHandler.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,38 @@ describe('GetCustomersQueryHandler', () => {
4444

4545
verify(customerRepository.findCustomers(1)).once();
4646
});
47+
48+
it('testGetAllCustomers', async () => {
49+
const customerRepository = mock(CustomerRepository);
50+
51+
const customer1 = mock(Customer);
52+
when(customer1.getId()).thenReturn('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2');
53+
when(customer1.getName()).thenReturn('Customer 1');
54+
55+
const customer2 = mock(Customer);
56+
when(customer2.getId()).thenReturn('d54f15d6-1a1d-47e8-8672-9f46018f9960');
57+
when(customer2.getName()).thenReturn('Customer 2');
58+
59+
when(customerRepository.findCustomers(null)).thenResolve([
60+
[instance(customer2), instance(customer1)],
61+
2
62+
]);
63+
64+
const queryHandler = new GetCustomersQueryHandler(
65+
instance(customerRepository)
66+
);
67+
68+
const expectedResult = new Pagination<CustomerView>(
69+
[
70+
new CustomerView('d54f15d6-1a1d-47e8-8672-9f46018f9960', 'Customer 2'),
71+
new CustomerView('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2', 'Customer 1')
72+
],
73+
2
74+
);
75+
76+
expect(
77+
await queryHandler.execute(new GetCustomersQuery(null))
78+
).toMatchObject(expectedResult);
79+
verify(customerRepository.findCustomers(null)).once();
80+
});
4781
});

src/Domain/Customer/Repository/ICustomerRepository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ export interface ICustomerRepository {
44
save(customer: Customer): Promise<Customer>;
55
findOneByName(name: string): Promise<Customer | undefined>;
66
findOneById(id: string): Promise<Customer | undefined>;
7-
findCustomers(page: number): Promise<[Customer[], number]>;
7+
findCustomers(page: number | null): Promise<[Customer[], number]>;
88
}

src/Infrastructure/Customer/Repository/CustomerRepository.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@ export class CustomerRepository implements ICustomerRepository {
3131
.getOne();
3232
}
3333

34-
public findCustomers(page: number): Promise<[Customer[], number]> {
35-
return this.repository
34+
public findCustomers(page: number | null): Promise<[Customer[], number]> {
35+
let query = this.repository
3636
.createQueryBuilder('customer')
3737
.select(['customer.id', 'customer.name'])
38-
.orderBy('customer.name', 'ASC')
39-
.limit(MAX_ITEMS_PER_PAGE)
40-
.offset((page - 1) * MAX_ITEMS_PER_PAGE)
41-
.getManyAndCount();
38+
.orderBy('customer.name', 'ASC');
39+
40+
if (typeof page === 'number') {
41+
query = query
42+
.limit(MAX_ITEMS_PER_PAGE)
43+
.offset((page - 1) * MAX_ITEMS_PER_PAGE);
44+
}
45+
46+
return query.getManyAndCount();
4247
}
4348
}

src/Infrastructure/Project/Controller/AddProjectController.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class AddProjectController {
3838
@Render('pages/projects/add.njk')
3939
public async get() {
4040
const customers: Pagination<CustomerView> = await this.queryBus.execute(
41-
new GetCustomersQuery(1)
41+
new GetCustomersQuery(null)
4242
);
4343

4444
return {
@@ -48,7 +48,7 @@ export class AddProjectController {
4848
}
4949

5050
@Post()
51-
public async poqr(@Body() projectDto: ProjectDTO, @Res() res: Response) {
51+
public async post(@Body() projectDto: ProjectDTO, @Res() res: Response) {
5252
const { name, customerId, active } = projectDto;
5353

5454
try {

src/Infrastructure/Project/Controller/EditProjectController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class EditProjectController {
4545
);
4646

4747
const customers: Pagination<CustomerView> = await this.queryBus.execute(
48-
new GetCustomersQuery(1)
48+
new GetCustomersQuery(null)
4949
);
5050

5151
return {

0 commit comments

Comments
 (0)