58
src/models/creation/CreateRunner.ts
Normal file
58
src/models/creation/CreateRunner.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsPositive, IsString } from 'class-validator';
|
||||
import { Runner } from '../entities/Runner';
|
||||
import { getConnectionManager, Repository } from 'typeorm';
|
||||
import { group } from 'console';
|
||||
import { RunnerOnlyOneGroupAllowedError, RunnerGroupNeededError, RunnerGroupNotFoundError } from '../../errors/RunnerErrors';
|
||||
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
|
||||
import { RunnerTeam } from '../entities/RunnerTeam';
|
||||
import { RunnerGroup } from '../entities/RunnerGroup';
|
||||
import { Address } from 'cluster';
|
||||
|
||||
export class CreateRunner {
|
||||
@IsString()
|
||||
firstname: string;
|
||||
@IsString()
|
||||
middlename?: string;
|
||||
@IsString()
|
||||
lastname: string;
|
||||
@IsString()
|
||||
phone?: string;
|
||||
@IsString()
|
||||
email?: string;
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
teamId?: number
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
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;
|
||||
}
|
||||
}
|
||||
15
src/models/creation/CreateRunnerOrganisation.ts
Normal file
15
src/models/creation/CreateRunnerOrganisation.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { IsString } from 'class-validator';
|
||||
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
|
||||
|
||||
export class CreateRunnerOrganisation {
|
||||
@IsString()
|
||||
name: string;
|
||||
|
||||
public async toRunnerOrganisation(): Promise<RunnerOrganisation> {
|
||||
let newRunnerOrganisation: RunnerOrganisation = new RunnerOrganisation();
|
||||
|
||||
newRunnerOrganisation.name = this.name;
|
||||
|
||||
return newRunnerOrganisation;
|
||||
}
|
||||
}
|
||||
30
src/models/creation/CreateTrack.ts
Normal file
30
src/models/creation/CreateTrack.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { IsInt, IsNotEmpty, IsPositive, IsString } from 'class-validator';
|
||||
import { Track } from '../entities/Track';
|
||||
|
||||
export class CreateTrack {
|
||||
/**
|
||||
* The track's name.
|
||||
*/
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* The track's distance in meters (must be greater 0).
|
||||
*/
|
||||
@IsInt()
|
||||
@IsPositive()
|
||||
distance: number;
|
||||
|
||||
/**
|
||||
* Converts a Track object based on this.
|
||||
*/
|
||||
public toTrack(): Track {
|
||||
let newTrack: Track = new Track();
|
||||
|
||||
newTrack.name = this.name;
|
||||
newTrack.distance = this.distance;
|
||||
|
||||
return newTrack;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user