Files
DiunaBI/Frontend/src/app/models/layer.model.ts

124 lines
4.5 KiB
TypeScript
Raw Normal View History

2022-12-11 23:40:16 +01:00
import { Base } from './base.model';
import { UntypedFormBuilder, Validators, UntypedFormGroup } from '@angular/forms';
import { HttpClient } from '@angular/common/http';
import { environment } from 'src/environments/environment';
import { map } from 'rxjs';
2023-01-06 11:10:58 +01:00
import { Record } from 'src/app/models/record.model';
2022-12-11 23:40:16 +01:00
2023-01-06 11:10:58 +01:00
export class Layer extends Base {
2023-01-06 11:55:01 +01:00
// eslint-disable-next-line @typescript-eslint/ban-types
2022-12-19 18:36:57 +01:00
number?: Number;
2022-12-22 15:12:19 +01:00
source?: string;
2022-12-11 23:40:16 +01:00
name?: string;
2023-01-06 11:10:58 +01:00
records: Record[] = [];
2022-12-27 22:22:21 +01:00
created?: string;
2022-12-11 23:40:16 +01:00
2023-01-06 11:10:58 +01:00
constructor(data: Partial<Layer> = {}) {
2022-12-11 23:40:16 +01:00
super();
Object.assign(this, data);
}
2023-01-06 11:55:01 +01:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2022-12-22 15:12:19 +01:00
override deserialize(input: any): this {
2022-12-11 23:40:16 +01:00
Object.assign(this, Object.assign(this, super.deserialize(input)));
2023-01-06 11:10:58 +01:00
if (this.records) { this.records = this.records.map(x => new Record().deserialize(x)); }
2022-12-11 23:40:16 +01:00
return this;
}
2022-12-21 18:35:26 +01:00
override serialize() {
this.number = 0; // will be overrided in backend
return Object.assign({}, this);
}
2022-12-11 23:40:16 +01:00
static getForm(fb: UntypedFormBuilder) {
return fb.group({
id: [null],
name: ['', Validators.required],
2022-12-19 18:36:57 +01:00
source: ['', Validators.required],
2022-12-21 22:15:17 +01:00
sheetId: '1G_Hu8DTP-PSPNXTaVYhc_ppnTQi6HWoA4oXSSdUmM9E',
2022-12-11 23:40:16 +01:00
createdAt: '',
modifiedAt: '',
createdBy: '',
modifiedBy: '',
2022-12-27 22:22:21 +01:00
modified: '',
created: ''
2022-12-22 15:12:19 +01:00
});
2022-12-11 23:40:16 +01:00
}
fillForm(form: UntypedFormGroup) {
form.patchValue(this);
form.patchValue({
createdBy: this.createdBy?.userName,
modifiedBy: this.modifiedBy?.userName
});
}
loadForm(form: UntypedFormGroup) {
2023-01-06 11:55:01 +01:00
for (const field of Object.keys(form.controls)) {
2023-01-06 11:10:58 +01:00
this[field as keyof Layer] = form.controls[field].value;
2022-12-11 23:40:16 +01:00
}
this.createdBy = undefined;
this.modifiedBy = undefined;
}
2022-12-21 18:35:26 +01:00
//API Actions
2023-01-06 11:10:58 +01:00
static add(input: Layer, _http: HttpClient): Promise<string> {
2022-12-11 23:40:16 +01:00
return new Promise((resolve, reject) => {
2023-01-06 11:10:58 +01:00
_http.post<string>(`${environment.api.url}/layers`, { ...input.serialize(), }).subscribe({
2022-12-11 23:40:16 +01:00
next: (data) => resolve(data),
2022-12-22 15:12:19 +01:00
error: (e) => reject(e)
}
2022-12-11 23:40:16 +01:00
);
});
}
2023-01-06 11:55:01 +01:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2022-12-11 23:40:16 +01:00
static getList(_http: HttpClient): any {
return new Promise((resolve, reject) => {
2023-01-06 11:10:58 +01:00
_http.get<Layer[]>(`${environment.api.url}/layers`)
.pipe(map(data => data.map(x => new Layer().deserialize(x))))
2022-12-22 15:12:19 +01:00
.subscribe({
next: (data) => resolve(data),
error: (e) => reject(e)
})
2022-12-11 23:40:16 +01:00
});
}
2023-01-06 11:10:58 +01:00
static getById(id: string, _http: HttpClient): Promise<Layer> {
2022-12-11 23:40:16 +01:00
return new Promise((resolve, reject) => {
2023-01-06 11:10:58 +01:00
_http.get<Layer>(`${environment.api.url}/layers/${id}`).pipe(map(x => new Layer().deserialize(x))).subscribe({
2022-12-11 23:40:16 +01:00
next: (data) => resolve(data),
error: (e) => reject(e)
})
});
}
2023-01-06 11:55:01 +01:00
static parseFile(file: File, _http: HttpClient): Promise<Layer[]> {
2022-12-19 18:36:57 +01:00
const formData = new FormData();
formData.append(file.name, file);
return new Promise((resolve, reject) => {
2023-01-06 11:10:58 +01:00
_http.post<Layer[]>(`${environment.api.url}/layers/parseFile`, formData,
).pipe(map(data => data.map(x => new Layer().deserialize(x))))
2022-12-22 15:12:19 +01:00
.subscribe({
next: (data) => {
resolve(data);
},
error: (e) => reject(e)
})
2022-12-19 18:36:57 +01:00
})
}
2023-01-06 11:10:58 +01:00
static parseGoogleSheet(sheetId: string, _http: HttpClient): Promise<Layer> {
2022-12-21 22:15:17 +01:00
return new Promise((resolve, reject) => {
2023-01-06 11:10:58 +01:00
_http.get<Layer>(`${environment.api.url}/layers/parseGoogleSheet/${sheetId}`,
).pipe(map(data => new Layer().deserialize(data)))
2022-12-22 15:12:19 +01:00
.subscribe({
next: (data) => {
resolve(data);
},
error: (e) => reject(e)
})
})
}
static exportToGoogleSheet(id: string, _http: HttpClient): Promise<boolean> {
return new Promise((resolve, reject) => {
2023-01-06 11:10:58 +01:00
_http.get<boolean>(`${environment.api.url}/layers/exportToGoogleSheet/${id}`,
2022-12-22 15:12:19 +01:00
).subscribe({
2022-12-21 22:15:17 +01:00
next: (data) => {
resolve(data);
},
error: (e) => reject(e)
})
})
}
2022-12-11 23:40:16 +01:00
}