Moved Create Runner to it's own file
This commit is contained in:
		@@ -1,73 +1,11 @@
 | 
				
			|||||||
import {
 | 
					import { JsonController, Param, Body, Get, Post, Put, Delete, OnUndefined } from 'routing-controllers';
 | 
				
			||||||
	JsonController,
 | 
					 | 
				
			||||||
	Param,
 | 
					 | 
				
			||||||
	Body,
 | 
					 | 
				
			||||||
	Get,
 | 
					 | 
				
			||||||
	Post,
 | 
					 | 
				
			||||||
	Put,
 | 
					 | 
				
			||||||
	Delete,
 | 
					 | 
				
			||||||
	NotFoundError,
 | 
					 | 
				
			||||||
	OnUndefined,
 | 
					 | 
				
			||||||
	NotAcceptableError,
 | 
					 | 
				
			||||||
	Authorized
 | 
					 | 
				
			||||||
} from 'routing-controllers';
 | 
					 | 
				
			||||||
import { getConnectionManager, Repository } from 'typeorm';
 | 
					import { getConnectionManager, Repository } from 'typeorm';
 | 
				
			||||||
import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
 | 
					import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
 | 
				
			||||||
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
 | 
					import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
 | 
				
			||||||
import { Runner } from '../models/Runner';
 | 
					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 { RunnerGroupNeededError, RunnerGroupNotFoundError, RunnerIdsNotMatchingError, RunnerNotFoundError, RunnerOnlyOneGroupAllowedError } from '../errors/RunnerErrors';
 | 
				
			||||||
import { RunnerGroup } from '../models/RunnerGroup';
 | 
					import { CreateRunner } from '../models/CreateRunner';
 | 
				
			||||||
import { RunnerOrganisation } from '../models/RunnerOrganisation';
 | 
					 | 
				
			||||||
import { RunnerTeam } from '../models/RunnerTeam';
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
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')
 | 
					@JsonController('/runners')
 | 
				
			||||||
//@Authorized('RUNNERS:read')
 | 
					//@Authorized('RUNNERS:read')
 | 
				
			||||||
@@ -99,37 +37,20 @@ export class RunnerController {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	@Post()
 | 
						@Post()
 | 
				
			||||||
	@ResponseSchema(Runner)
 | 
						@ResponseSchema(Runner)
 | 
				
			||||||
 | 
						@ResponseSchema(RunnerOnlyOneGroupAllowedError)
 | 
				
			||||||
 | 
						@ResponseSchema(RunnerGroupNeededError)
 | 
				
			||||||
 | 
						@ResponseSchema(RunnerGroupNotFoundError)
 | 
				
			||||||
	@OpenAPI({ description: 'Create a new runner object (id will be generated automagicly).' })
 | 
						@OpenAPI({ description: 'Create a new runner object (id will be generated automagicly).' })
 | 
				
			||||||
	async post(
 | 
						async post(@Body({ validate: true }) createRunner: CreateRunner) {
 | 
				
			||||||
		@Body({ validate: true })
 | 
							let runner;
 | 
				
			||||||
		createRunner: CreateRunner
 | 
							try {
 | 
				
			||||||
	) {
 | 
								runner = await createRunner.toRunner();
 | 
				
			||||||
		let group: RunnerGroup;
 | 
							} catch (error) {
 | 
				
			||||||
 | 
								return error;
 | 
				
			||||||
		if(createRunner.teamId && createRunner.orgId){
 | 
					 | 
				
			||||||
			throw new RunnerOnlyOneGroupAllowedError();
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		if(!createRunner.teamId && !createRunner.orgId){
 | 
					 | 
				
			||||||
			throw new RunnerGroupNeededError();
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if(createRunner.teamId){
 | 
							return runner;
 | 
				
			||||||
			group = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ id: createRunner.teamId });
 | 
							//return this.runnerRepository.save(runner);
 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		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);
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Put('/:id')
 | 
						@Put('/:id')
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										56
									
								
								src/models/CreateRunner.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								src/models/CreateRunner.ts
									
									
									
									
									
										Normal file
									
								
							@@ -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;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user