-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSubjectAverageDto.ts
123 lines (102 loc) · 3.76 KB
/
SubjectAverageDto.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { IsArray, IsInstance, IsNumber, IsOptional, IsString, validateSync, ValidationError } from 'class-validator';
import AverageWithTimeDto from './SubjectAverage/AverageWithTimeDto';
import SubjectDescriptor from './SubjectDescriptor';
export interface SubjectAverageFields {
Atlag?: number;
AtlagAlakulasaIdoFuggvenyeben: Array<AverageWithTimeDto>;
SortIndex?: number;
Tantargy: SubjectDescriptor;
SulyozottOsztalyzatOsszege?: number;
SulyozottOsztalyzatSzama?: number;
Uid: string;
}
export default class SubjectAverageDto implements Partial<SubjectAverageFields> {
@IsOptional()
@IsNumber()
private readonly average?: number;
@IsArray()
@IsInstance(AverageWithTimeDto, { each: true })
private readonly averagesInTime: Array<AverageWithTimeDto> = [];
@IsOptional()
@IsNumber()
private readonly sortIndex?: number;
@IsInstance(SubjectDescriptor)
private readonly subject?: SubjectDescriptor;
@IsOptional()
@IsNumber()
private readonly sumOfWeightedEvaluations?: number;
@IsOptional()
@IsNumber()
private readonly sumOfWeights?: number;
@IsString()
private readonly uid?: string;
constructor(input: any) {
if (typeof input === 'object' && input !== null) {
this.average = typeof input['Atlag'] === 'number' ? input['Atlag'] : undefined;
this.averagesInTime = Array.isArray(input['AtlagAlakulasaIdoFuggvenyeben']) ?
input['AtlagAlakulasaIdoFuggvenyeben'].map((item: any) => new AverageWithTimeDto(item)) : [];
this.sortIndex = typeof input['SortIndex'] === 'number' ? input['SortIndex'] : undefined;
this.subject = typeof input['Tantargy'] === 'object' ? new SubjectDescriptor(input['Tantargy']) : undefined;
this.sumOfWeightedEvaluations = typeof input['SulyozottOsztalyzatOsszege'] === 'number' ? input['SulyozottOsztalyzatOsszege'] :
undefined;
this.sumOfWeights = typeof input['SulyozottOsztalyzatSzama'] === 'number' ? input['SulyozottOsztalyzatSzama'] : 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 Atlag(): number | undefined {
return this.average;
}
public get AtlagAlakulasaIdoFuggvenyeben(): Array<AverageWithTimeDto> {
return this.averagesInTime;
}
public get SortIndex(): number | undefined {
return this.sortIndex;
}
public get Tantargy(): SubjectDescriptor | undefined {
return this.subject;
}
public get SulyozottOsztalyzatOsszege(): number | undefined {
return this.sumOfWeightedEvaluations;
}
public get SulyozottOsztalyzatSzama(): number | undefined {
return this.sumOfWeights;
}
public get Uid(): string | undefined {
return this.uid;
}
public get json(): SubjectAverageFields {
return {
Atlag: this.average,
AtlagAlakulasaIdoFuggvenyeben: this.averagesInTime.map((item) => item.json),
SortIndex: this.sortIndex,
SulyozottOsztalyzatOsszege: this.sumOfWeightedEvaluations,
SulyozottOsztalyzatSzama: this.sumOfWeights,
Tantargy: this.subject?.json,
Uid: this.uid,
} as SubjectAverageFields;
}
private validationErrorResponse(errors: Array<ValidationError>): object {
const validFields: Partial<SubjectAverageFields> = {
Atlag: this.average,
AtlagAlakulasaIdoFuggvenyeben: this.averagesInTime,
SortIndex: this.sortIndex,
SulyozottOsztalyzatOsszege: this.sumOfWeightedEvaluations,
SulyozottOsztalyzatSzama: this.sumOfWeights,
Tantargy: this.subject,
Uid: this.uid,
};
const errorMessages: Array<string> = [];
for (const error of errors) {
validFields[error.property as keyof SubjectAverageFields] = undefined;
errorMessages.push(...Object.values(error.constraints || {}));
}
return {
valid: validFields,
errors: errorMessages,
};
}
}