-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClassAverageDto.ts
98 lines (80 loc) · 2.85 KB
/
ClassAverageDto.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { IsInstance, IsNumber, IsOptional, IsString, validateSync, ValidationError } from 'class-validator';
import SubjectDescriptor from './SubjectDescriptor';
export interface ClassAverageFields {
TanuloAtlag?: number;
OsztalyCsoportAtlag?: number;
OsztalyCsoportAtlagtolValoElteres?: number;
Tantargy: SubjectDescriptor;
Uid: string;
}
export default class ClassAverageDto implements Partial<ClassAverageFields> {
@IsOptional()
@IsNumber()
private readonly average?: number;
@IsOptional()
@IsNumber()
private readonly classAverageNumber?: number;
@IsOptional()
@IsNumber()
private readonly differenceFromClassAverage?: number;
@IsInstance(SubjectDescriptor)
private readonly subject?: SubjectDescriptor;
@IsString()
private readonly uid?: string;
constructor(input: any) {
if (typeof input === 'object' && input !== null) {
this.average = typeof input['TanuloAtlag'] === 'number' ? input['TanuloAtlag'] : undefined;
this.classAverageNumber = typeof input['OsztalyCsoportAtlag'] === 'number' ? input['OsztalyCsoportAtlag'] : undefined;
this.differenceFromClassAverage = typeof input['OsztalyCsoportAtlagtolValoElteres'] === 'number' ?
input['OsztalyCsoportAtlagtolValoElteres'] : undefined;
this.subject = typeof input['Tantargy'] === 'object' ? new SubjectDescriptor(input['Tantargy']) : undefined;
this.uid = typeof input['Uid'] === 'string' ? input['Uid'].trim() : undefined;
}
const errors = validateSync(this, { skipMissingProperties: true });
if (errors.length > 0) {
throw this.validationErrorResponse(errors);
}
}
public get TanuloAtlag(): number | undefined {
return this.average;
}
public get OsztalyCsoportAtlag(): number | undefined {
return this.classAverageNumber;
}
public get OsztalyCsoportAtlagtolValoElteres(): number | undefined {
return this.differenceFromClassAverage;
}
public get Tantargy(): SubjectDescriptor | undefined {
return this.subject;
}
public get Uid(): string | undefined {
return this.uid;
}
public get json(): ClassAverageFields {
return {
OsztalyCsoportAtlag: this.classAverageNumber,
OsztalyCsoportAtlagtolValoElteres: this.differenceFromClassAverage,
Tantargy: this.subject?.json,
TanuloAtlag: this.average,
Uid: this.uid,
} as ClassAverageFields;
}
private validationErrorResponse(errors: Array<ValidationError>): object {
const validFields: Partial<ClassAverageFields> = {
TanuloAtlag: this.average,
OsztalyCsoportAtlag: this.classAverageNumber,
OsztalyCsoportAtlagtolValoElteres: this.differenceFromClassAverage,
Tantargy: this.subject,
Uid: this.uid,
};
const errorMessages: Array<string> = [];
for (const error of errors) {
validFields[error.property as keyof ClassAverageFields] = undefined;
errorMessages.push(...Object.values(error.constraints || {}));
}
return {
valid: validFields,
errors: errorMessages,
};
}
}