Skip to content

Commit

Permalink
feat(api): scenarios: cost-sufrace marxan data
Browse files Browse the repository at this point in the history
  • Loading branch information
kgajowy committed Jun 11, 2021
1 parent a5d5836 commit 027ba86
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { CostSurfaceViewService } from './cost-surface-view.service';

@Module({
providers: [CostSurfaceViewService],
exports: [CostSurfaceViewService],
})
export class CostSurfaceViewModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Injectable } from '@nestjs/common';
import * as stream from 'stream';

@Injectable()
export class CostSurfaceViewService {
read(scenarioId: string, stream: stream.Writable): void {
stream.write('id,cost,status');

// typeorm query runner/builder -> stream

stream.end();
}
}
21 changes: 21 additions & 0 deletions api/apps/api/src/modules/scenarios/scenarios.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import {
Controller,
Delete,
Get,
Header,
Param,
ParseUUIDPipe,
Patch,
Post,
Req,
Res,
UploadedFile,
UseGuards,
UseInterceptors,
Expand Down Expand Up @@ -47,6 +49,7 @@ import { FileInterceptor } from '@nestjs/platform-express';
import { ScenariosService } from './scenarios.service';
import { ScenarioSerializer } from './dto/scenario.serializer';
import { ScenarioFeatureSerializer } from './dto/scenario-feature.serializer';
import * as express from 'express';

@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
Expand Down Expand Up @@ -170,4 +173,22 @@ export class ScenariosController {
await this.service.getFeatures(id),
);
}

@Header('Content-Type', 'text/csv')
@ApiOkResponse({
schema: {
type: 'string',
},
})
@ApiOperation({
description: `Uploaded cost surface data`,
})
@Get(`:id/marxan/dat/pu.dat`)
async getScenarioCostSurface(
@Param('id', ParseUUIDPipe) id: string,
@Res() res: express.Response,
): Promise<void> {
await this.service.getCostSurfaceCsv(id, res);
return;
}
}
2 changes: 2 additions & 0 deletions api/apps/api/src/modules/scenarios/scenarios.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { CostSurfaceModule } from './cost-surface/cost-surface.module';
import { ScenariosService } from './scenarios.service';
import { ScenarioSerializer } from './dto/scenario.serializer';
import { ScenarioFeatureSerializer } from './dto/scenario-feature.serializer';
import { CostSurfaceViewModule } from './cost-surface-readmodel/cost-surface-view.module';

@Module({
imports: [
Expand All @@ -29,6 +30,7 @@ import { ScenarioFeatureSerializer } from './dto/scenario-feature.serializer';
AnalysisModule,
CostSurfaceModule,
HttpModule,
CostSurfaceViewModule,
],
providers: [
ScenariosService,
Expand Down
11 changes: 11 additions & 0 deletions api/apps/api/src/modules/scenarios/scenarios.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HttpService, Injectable } from '@nestjs/common';
import { FetchSpecification } from 'nestjs-base-service';
import * as stream from 'stream';

import { AppInfoDTO } from '@marxan-api/dto/info.dto';
import { AppConfig } from '@marxan-api/utils/config.utils';
Expand All @@ -13,6 +14,7 @@ import { ScenariosCrudService } from './scenarios-crud.service';
import { CreateScenarioDTO } from './dto/create.scenario.dto';
import { UpdateScenarioDTO } from './dto/update.scenario.dto';
import { UpdateScenarioPlanningUnitLockStatusDto } from './dto/update-scenario-planning-unit-lock-status.dto';
import { CostSurfaceViewService } from '@marxan-api/modules/scenarios/cost-surface-readmodel/cost-surface-view.service';

@Injectable()
export class ScenariosService {
Expand All @@ -26,6 +28,7 @@ export class ScenariosService {
private readonly updatePlanningUnits: AdjustPlanningUnits,
private readonly costSurface: CostSurfaceFacade,
private readonly httpService: HttpService,
private readonly costSurfaceView: CostSurfaceViewService,
) {}

async findAllPaginated(fetchSpecification: FetchSpecification) {
Expand Down Expand Up @@ -103,6 +106,14 @@ export class ScenariosService {
return geoJson;
}

async getCostSurfaceCsv(
scenarioId: string,
stream: stream.Writable,
): Promise<void> {
await this.assertScenario(scenarioId);
this.costSurfaceView.read(scenarioId, stream);
}

private async assertScenario(scenarioId: string) {
await this.crudService.getById(scenarioId);
}
Expand Down

0 comments on commit 027ba86

Please sign in to comment.