Working csv import

ref #22
This commit is contained in:
Nicolai Ort 2020-12-17 18:36:51 +01:00
parent 0d8fbf1eca
commit 03b7e346ab
2 changed files with 31 additions and 5 deletions

View File

@ -30,6 +30,7 @@
"consola": "^2.15.0",
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
"csvtojson": "^2.0.10",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
@ -47,6 +48,7 @@
},
"devDependencies": {
"@types/cors": "^2.8.8",
"@types/csvtojson": "^1.1.5",
"@types/express": "^4.17.9",
"@types/jest": "^26.0.16",
"@types/jsonwebtoken": "^8.5.0",

View File

@ -1,3 +1,4 @@
import csv from 'csvtojson';
import { Body, ContentType, Controller, Param, Post, QueryParam, Req, UseBefore } from 'routing-controllers';
import { OpenAPI } from 'routing-controllers-openapi';
import { RunnerGroupNeededError } from '../errors/RunnerErrors';
@ -44,12 +45,35 @@ export class ImportController {
return await this.postJSON(importRunners, id)
}
@Post('/runners/import/csv')
@ContentType("text/csv")
@Post('/import/csv')
@UseBefore(RawBodyMiddleware)
@OpenAPI({ description: "Create new runners from csv" })
postCSV(@Req() request: any) {
console.log(request.rawBody.toString())
throw new Error("Not implemented yet.");
async postCSV(@Req() request: any, @QueryParam("group") groupID: number) {
let csvParse = await csv({ delimiter: [",", ";"], trim: true }).fromString(request.rawBody.toString());
let importRunners: ImportRunner[] = new Array<ImportRunner>();
for await (let runner of csvParse) {
let newImportRunner = new ImportRunner();
newImportRunner.firstname = runner.firstname;
newImportRunner.middlename = runner.middlename;
newImportRunner.lastname = runner.lastname;
if (runner.class === undefined) { newImportRunner.team = runner.team; }
else { newImportRunner.class = runner.class; }
importRunners.push(newImportRunner);
}
return await this.postJSON(importRunners, groupID);
}
@Post('/organisations/:id/import/csv')
@UseBefore(RawBodyMiddleware)
@OpenAPI({ description: "Create new runners from csv" })
async postOrgsCSV(@Req() request: any, @Param("id") id: number) {
return await this.postCSV(request, id);
}
@Post('/teams/:id/import/csv')
@UseBefore(RawBodyMiddleware)
@OpenAPI({ description: "Create new runners from csv" })
async postTeamsCSV(@Req() request: any, @Param("id") id: number) {
return await this.postCSV(request, id);
}
}