Moved Create Runner to it's own file
This commit is contained in:
parent
6d81fc1309
commit
9e3ee433d2
|
@ -1,73 +1,11 @@
|
|||
import {
|
||||
JsonController,
|
||||
Param,
|
||||
Body,
|
||||
Get,
|
||||
Post,
|
||||
Put,
|
||||
Delete,
|
||||
NotFoundError,
|
||||
OnUndefined,
|
||||
NotAcceptableError,
|
||||
Authorized
|
||||
} from 'routing-controllers';
|
||||
import { JsonController, Param, Body, Get, Post, Put, Delete, OnUndefined } from 'routing-controllers';
|
||||
import { getConnectionManager, Repository } from 'typeorm';
|
||||
import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
|
||||
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
||||
import { Runner } from '../models/Runner';
|
||||
import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsPositive, IsString } from 'class-validator';
|
||||
import { RunnerGroupNeededError, RunnerGroupNotFoundError, RunnerIdsNotMatchingError, RunnerNotFoundError, RunnerOnlyOneGroupAllowedError } from '../errors/RunnerErrors';
|
||||
import { RunnerGroup } from '../models/RunnerGroup';
|
||||
import { RunnerOrganisation } from '../models/RunnerOrganisation';
|
||||
import { RunnerTeam } from '../models/RunnerTeam';
|
||||
import { CreateRunner } from '../models/CreateRunner';
|
||||
|
||||
class CreateRunner {
|
||||
/**
|
||||
* The runners's first name.
|
||||
*/
|
||||
@IsNotEmpty()
|
||||
@IsString()
|
||||
firstname: string;
|
||||
|
||||
/**
|
||||
* The runners's middle name.
|
||||
* Optional
|
||||
*/
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
middlename?: string;
|
||||
|
||||
/**
|
||||
* The runers's last name.
|
||||
*/
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
lastname: string;
|
||||
|
||||
/**
|
||||
* The runner's phone number.
|
||||
* Optional
|
||||
*/
|
||||
@IsOptional()
|
||||
@IsPhoneNumber('DE')
|
||||
phone?: string;
|
||||
|
||||
/**
|
||||
* The runner's email address.
|
||||
* Optional
|
||||
*/
|
||||
@IsOptional()
|
||||
@IsEmail()
|
||||
email?: string;
|
||||
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
teamId?: number;
|
||||
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
orgId?: number;
|
||||
}
|
||||
|
||||
@JsonController('/runners')
|
||||
//@Authorized('RUNNERS:read')
|
||||
|
@ -99,37 +37,20 @@ export class RunnerController {
|
|||
|
||||
@Post()
|
||||
@ResponseSchema(Runner)
|
||||
@ResponseSchema(RunnerOnlyOneGroupAllowedError)
|
||||
@ResponseSchema(RunnerGroupNeededError)
|
||||
@ResponseSchema(RunnerGroupNotFoundError)
|
||||
@OpenAPI({ description: 'Create a new runner object (id will be generated automagicly).' })
|
||||
async post(
|
||||
@Body({ validate: true })
|
||||
createRunner: CreateRunner
|
||||
) {
|
||||
let group: RunnerGroup;
|
||||
|
||||
if(createRunner.teamId && createRunner.orgId){
|
||||
throw new RunnerOnlyOneGroupAllowedError();
|
||||
}
|
||||
if(!createRunner.teamId && !createRunner.orgId){
|
||||
throw new RunnerGroupNeededError();
|
||||
async post(@Body({ validate: true }) createRunner: CreateRunner) {
|
||||
let runner;
|
||||
try {
|
||||
runner = await createRunner.toRunner();
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
if(createRunner.teamId){
|
||||
group = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ id: createRunner.teamId });
|
||||
}
|
||||
if(createRunner.orgId){
|
||||
group = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: createRunner.orgId });
|
||||
}
|
||||
if(!group){
|
||||
throw new RunnerGroupNotFoundError();
|
||||
}
|
||||
|
||||
delete createRunner.teamId;
|
||||
delete createRunner.orgId;
|
||||
let runner = <Runner>createRunner;
|
||||
runner.group=group;
|
||||
console.log(runner)
|
||||
|
||||
return this.runnerRepository.save(runner);
|
||||
return runner;
|
||||
//return this.runnerRepository.save(runner);
|
||||
}
|
||||
|
||||
@Put('/:id')
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsPositive, IsString } from 'class-validator';
|
||||
import { Runner } from '../models/Runner';
|
||||
import { getConnectionManager, Repository } from 'typeorm';
|
||||
import { group } from 'console';
|
||||
import { RunnerOnlyOneGroupAllowedError, RunnerGroupNeededError, RunnerGroupNotFoundError } from '../errors/RunnerErrors';
|
||||
import { RunnerOrganisation } from './RunnerOrganisation';
|
||||
import { RunnerTeam } from './RunnerTeam';
|
||||
import { RunnerGroup } from './RunnerGroup';
|
||||
import { Address } from 'cluster';
|
||||
|
||||
export class CreateRunner {
|
||||
@IsString()
|
||||
firstname: string;
|
||||
@IsString()
|
||||
middlename?: string;
|
||||
@IsString()
|
||||
lastname: string;
|
||||
@IsString()
|
||||
phone?: string;
|
||||
@IsString()
|
||||
email?: string;
|
||||
@IsInt()
|
||||
teamId?: number
|
||||
@IsInt()
|
||||
orgId?: number
|
||||
|
||||
public async toRunner(): Promise<Runner> {
|
||||
let newRunner: Runner = new Runner();
|
||||
|
||||
if (this.teamId !== undefined && this.orgId !== undefined) {
|
||||
throw new RunnerOnlyOneGroupAllowedError();
|
||||
}
|
||||
if (this.teamId === undefined && this.orgId === undefined) {
|
||||
throw new RunnerGroupNeededError();
|
||||
}
|
||||
|
||||
if (this.teamId) {
|
||||
newRunner.group = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ id: this.teamId });
|
||||
}
|
||||
if (this.orgId) {
|
||||
newRunner.group = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.orgId });
|
||||
}
|
||||
if (!newRunner.group) {
|
||||
throw new RunnerGroupNotFoundError();
|
||||
}
|
||||
|
||||
newRunner.firstname = this.firstname;
|
||||
newRunner.middlename = this.middlename;
|
||||
newRunner.lastname = this.lastname;
|
||||
newRunner.phone = this.phone;
|
||||
newRunner.email = this.email;
|
||||
|
||||
console.log(newRunner)
|
||||
return newRunner;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue