1
|
import {Injectable} from '@angular/core';
|
2
|
import {HttpClient, HttpParams} from '@angular/common/http';
|
3
|
import {BasicService} from './basic.service';
|
4
|
import {catchError} from 'rxjs/operators';
|
5
|
import {Languages} from '../../enums/common.enum';
|
6
|
import {MatSnackBar} from '@angular/material';
|
7
|
|
8
|
@Injectable({
|
9
|
providedIn: 'root'
|
10
|
})
|
11
|
export class FileService extends BasicService {
|
12
|
|
13
|
constructor(protected http: HttpClient, protected snackBar: MatSnackBar) {
|
14
|
super(http, snackBar);
|
15
|
}
|
16
|
|
17
|
/**
|
18
|
* Uploads xlsx file which will be parsed on the server
|
19
|
* @param file xlsx file to be parsed
|
20
|
*/
|
21
|
uploadXlsFile(file: FileList) {
|
22
|
return this.makeImportXlsxAPiCall(file, null);
|
23
|
}
|
24
|
|
25
|
/**
|
26
|
* Uploads xlsx file which will be parsed on the server
|
27
|
* @param language specify error message language
|
28
|
* @param file xlsx file to be parsed
|
29
|
*/
|
30
|
uploadXlsFileWithLanguage(file: FileList, language: Languages) {
|
31
|
return this.makeImportXlsxAPiCall(file, language);
|
32
|
}
|
33
|
|
34
|
/**
|
35
|
* Exports a pdf file from the imported xlsx file
|
36
|
*/
|
37
|
getExportedPdf() {
|
38
|
return this.makeExportPdfApiCall(null);
|
39
|
}
|
40
|
|
41
|
/**
|
42
|
* Exports a pdf file from the imported xlsx file
|
43
|
* @param language specify error message language
|
44
|
*/
|
45
|
getExportedPdfWithLanguage(language: Languages) {
|
46
|
return this.makeExportPdfApiCall(language);
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* Testovaci endpoint pro export PDF souboru
|
51
|
* GET /export/pdf? [lang=<CZ,EN>]
|
52
|
* @param language specify error message language
|
53
|
*/
|
54
|
private makeExportPdfApiCall(language: Languages) {
|
55
|
const httpParams: HttpParams = this.createParams({lang: language});
|
56
|
|
57
|
return this.http.get(this.baseUrl + '/api/export/pdf', {responseType: 'blob', params: httpParams})
|
58
|
.pipe(
|
59
|
catchError(err => this.handleError(err))
|
60
|
);
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* Testovaci endpoint pro import XLS souboru
|
65
|
* POST /import/xls?[lang=<CZ,EN>]&file=<binarni_soubor>
|
66
|
* @param file xlsx file to import and parse
|
67
|
* @param language specify error message langauge
|
68
|
*/
|
69
|
private makeImportXlsxAPiCall(file: FileList, language: Languages) {
|
70
|
const fileCount: number = file.length;
|
71
|
const formData = new FormData();
|
72
|
|
73
|
const httpParams: HttpParams = this.createParams({lang: language});
|
74
|
const options = {params: httpParams};
|
75
|
|
76
|
if (fileCount > 0) {
|
77
|
|
78
|
formData.append('file', file.item(0));
|
79
|
return this.http.post(this.baseUrl + '/api/import/xls', formData, options);
|
80
|
}
|
81
|
}
|
82
|
}
|