Skip to content

Commit

Permalink
refactor(api): make shapefile decorator more customizable, rename pla…
Browse files Browse the repository at this point in the history
…nning area dto
  • Loading branch information
Dyostiq committed Jul 7, 2021
1 parent 150a489 commit bd30b05
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
21 changes: 16 additions & 5 deletions api/apps/api/src/decorators/shapefile.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,27 @@ import {
ApiConsumes,
ApiOkResponse,
ApiOperation,
ApiResponseMetadata,
} from '@nestjs/swagger';

import { ShapefileGeoJSONResponseDTO } from '@marxan-api/modules/scenarios/dto/shapefile.geojson.response.dto';

export function ApiConsumesShapefile(withGeoJsonResponse = true) {
export function ApiConsumesShapefile(
options: {
withGeoJsonResponse?: boolean;
type?: ApiResponseMetadata['type'];
description?: ApiResponseMetadata['description'];
} = {},
) {
options = Object.assign({}, options, {
withGeoJsonResponse: true,
type: ShapefileGeoJSONResponseDTO,
description: 'Upload Zip file containing .shp, .dbj, .prj and .shx files',
});
return applyDecorators(
...[
ApiOperation({
description:
'Upload Zip file containing .shp, .dbj, .prj and .shx files',
description: options.description,
}),
ApiConsumes('multipart/form-data'),
ApiBody({
Expand All @@ -30,8 +41,8 @@ export function ApiConsumesShapefile(withGeoJsonResponse = true) {
},
},
}),
withGeoJsonResponse
? ApiOkResponse({ type: ShapefileGeoJSONResponseDTO })
options.withGeoJsonResponse
? ApiOkResponse({ type: options.type })
: undefined,
].filter(isDefined),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ApiProperty } from '@nestjs/swagger';
import { GeoJSON } from 'geojson';
import { ShapefileGeoJSONResponseDTO } from '../../scenarios/dto/shapefile.geojson.response.dto';

export class PlanningAreaCreatedDto {
export class PlanningAreaResponseDto extends ShapefileGeoJSONResponseDTO {
@ApiProperty({
description: 'An ID of the created planning area',
})
Expand Down
12 changes: 6 additions & 6 deletions api/apps/api/src/modules/projects/projects.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import { GeoFeatureSerializer } from './dto/geo-feature.serializer';
import { ProjectSerializer } from './dto/project.serializer';
import { ProjectJobsStatusDto } from './dto/project-jobs-status.dto';
import { JobStatusSerializer } from './dto/job-status.serializer';
import { PlanningAreaCreatedDto } from './dto/planning-area-created.dto';
import { PlanningAreaResponseDto } from './dto/planning-area-response.dto';
import { isLeft } from 'fp-ts/Either';

@UseGuards(JwtAuthGuard)
Expand Down Expand Up @@ -191,7 +191,7 @@ export class ProjectsController {
return this.jobsStatusSerizalizer.serialize(projectId, scenarios);
}

@ApiConsumesShapefile(false)
@ApiConsumesShapefile({ withGeoJsonResponse: false })
@ApiOperation({
description: 'Upload shapefile for project-specific protected areas',
})
Expand All @@ -209,16 +209,16 @@ export class ProjectsController {
return;
}

@ApiOkResponse({ type: PlanningAreaCreatedDto })
@ApiOperation({
@ApiConsumesShapefile({
withGeoJsonResponse: true,
type: PlanningAreaResponseDto,
description: 'Upload shapefile with project planning-area',
})
@ApiConsumesShapefile(false)
@UseInterceptors(FileInterceptor('file', uploadOptions))
@Post('planning-area/shapefile')
async shapefileWithProjectPlanningArea(
@UploadedFile() file: Express.Multer.File,
): Promise<PlanningAreaCreatedDto> {
): Promise<PlanningAreaResponseDto> {
const result = await this.projectsService.savePlanningAreaFromShapefile(
file,
);
Expand Down
2 changes: 1 addition & 1 deletion api/apps/api/src/modules/scenarios/scenarios.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export class ScenariosController {
);
}

@ApiConsumesShapefile(false)
@ApiConsumesShapefile({ withGeoJsonResponse: false })
@ApiNoContentResponse()
@Post(`:id/cost-surface/shapefile`)
async processCostSurfaceShapefile(
Expand Down

0 comments on commit bd30b05

Please sign in to comment.