Skip to content

Commit

Permalink
EW-561 code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
psachmann committed Oct 16, 2023
1 parent 5a98abf commit 6bd24fc
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 23 deletions.
19 changes: 7 additions & 12 deletions src/modules/person/api/person.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Mapper } from '@automapper/core';
import { getMapperToken } from '@automapper/nestjs';
import { Body, Controller, Get, Inject, Post, Param, HttpException, HttpStatus, Query, Res } from '@nestjs/common';
import { Body, Controller, Get, Inject, Post, Param, HttpException, HttpStatus, Query } from '@nestjs/common';
import {
ApiBadRequestResponse,
ApiCreatedResponse,
Expand All @@ -18,8 +18,7 @@ import { PersonByIdParams } from './person-by-id.param.js';
import { PersonenQueryParam } from './personen-query.param.js';
import { FindPersonDatensatzDTO } from './finde-persondatensatz-dto.js';
import { PersonenDatensatz } from './personendatensatz.js';
import { Paged, setPaginationHeaders } from '../../../shared/paging/index.js';
import { Response } from 'express';
import { Paged, PagedResponse } from '../../../shared/paging/index.js';

@ApiTags('person')
@Controller({ path: 'person' })
Expand Down Expand Up @@ -58,19 +57,15 @@ export class PersonController {
@ApiUnauthorizedResponse({ description: 'Not authorized to get persons.' })
@ApiForbiddenResponse({ description: 'Insufficient permissions to get persons.' })
@ApiInternalServerErrorResponse({ description: 'Internal server error while getting all persons.' })
public async findPersons(
@Query() queryParams: PersonenQueryParam,
@Res() res: Response,
): Promise<PersonenDatensatz[]> {
const persondatensatzDTO: FindPersonDatensatzDTO = this.mapper.map(
public async findPersons(@Query() queryParams: PersonenQueryParam): Promise<PagedResponse<PersonenDatensatz>> {
const personDatensatzDTO: FindPersonDatensatzDTO = this.mapper.map(
queryParams,
PersonenQueryParam,
FindPersonDatensatzDTO,
);
const persons: Paged<PersonenDatensatz> = await this.uc.findAll(persondatensatzDTO);
const persons: Paged<PersonenDatensatz> = await this.uc.findAll(personDatensatzDTO);
const response: PagedResponse<PersonenDatensatz> = new PagedResponse(persons);

setPaginationHeaders(res, persons);

return persons.items;
return response;
}
}
9 changes: 7 additions & 2 deletions src/shared/paging/global-pagination-headers.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Observable, map } from 'rxjs';
import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common';
import { setPaginationHeaders } from './helpers.js';
import { PagedResponse } from './paged.response.js';
import { Response } from 'express';

Expand All @@ -11,7 +10,7 @@ export class GlobalPaginationHeadersInterceptor implements NestInterceptor {
if (value instanceof PagedResponse) {
const response: Response = context.switchToHttp().getResponse();

setPaginationHeaders(response, value);
GlobalPaginationHeadersInterceptor.setPaginationHeaders(response, value);

return value.items as unknown[];
}
Expand All @@ -20,4 +19,10 @@ export class GlobalPaginationHeadersInterceptor implements NestInterceptor {
}),
);
}

private static setPaginationHeaders<T>(response: Response, payload: PagedResponse<T>): void {
response.setHeader('Pagination-Total', payload.total);
response.setHeader('Pagination-Offset', payload.offset);
response.setHeader('Pagination-Limit', payload.limit);
}
}
8 changes: 0 additions & 8 deletions src/shared/paging/helpers.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/shared/paging/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './global-pagination-headers.interceptor.js';
export * from './helpers.js';
export * from './paged.js';
export * from './paged.query.params.js';
export * from './paged.response.js';

0 comments on commit 6bd24fc

Please sign in to comment.