Added import export service and component in webapp.

This commit is contained in:
Iga Bartosz Collins 2023-01-31 13:29:56 +01:00
parent dc77a777c5
commit 1020e37005
7 changed files with 120 additions and 0 deletions

View File

@ -2,3 +2,5 @@ Osoby:
<app-person-list></app-person-list>
Grupy:
<app-group-list></app-group-list>
Import i eksport bazy danych:
<app-import-export></app-import-export>

View File

@ -0,0 +1,2 @@
<button type="button" mat-button (click)="export()">Eksportuj</button>
<input type="file" (change)="importDB($event)">

View File

@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ImportExportComponent } from './import-export.component';
describe('ImportExportComponent', () => {
let component: ImportExportComponent;
let fixture: ComponentFixture<ImportExportComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ ImportExportComponent ]
})
.compileComponents();
fixture = TestBed.createComponent(ImportExportComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@ -0,0 +1,45 @@
import {Component, OnInit} from '@angular/core';
import {ImportExportService} from "../../service/import-export.service";
import {Subject, switchMap} from "rxjs";
@Component({
selector: 'app-import-export',
templateUrl: './import-export.component.html',
styleUrls: ['./import-export.component.sass']
})
export class ImportExportComponent implements OnInit {
xmlSubject: Subject<string> = new Subject();
constructor(private importExportService: ImportExportService) {
}
ngOnInit(): void {
this.xmlSubject.pipe(
switchMap(xmlData => this.importExportService.uploadFile(xmlData))
).subscribe()
}
export() {
this.importExportService.downloadFile().subscribe((data: Blob) => {
const downloadURL = window.URL.createObjectURL(data);
const link = document.createElement('a');
link.href = downloadURL;
link.download = "export.xml";
link.click();
})
}
importDB(event: any) {
if (event.target?.value) {
const file = event.target.files[0];
const type = file.type;
const reader = new FileReader();
reader.onload = (evt) => {
const xmlData: string = (evt as any).target.result;
this.xmlSubject.next(xmlData);
};
reader.readAsText(file);
}
}
}

View File

@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { ImportExportService } from './import-export.service';
describe('ImportExportService', () => {
let service: ImportExportService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ImportExportService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@ -0,0 +1,32 @@
import { Injectable } from '@angular/core';
import {HttpClient, HttpHeaders} from "@angular/common/http";
@Injectable({
providedIn: 'root'
})
export class ImportExportService {
private apiUrl = 'api';
constructor(private http: HttpClient) {}
downloadFile() {
return this.http.get(`${this.apiUrl}/exportDatabase`, {
headers: new HttpHeaders({
'Content-Type': 'application/xml',
Accept: 'application/xml'
}),
observe: 'body',
responseType: "blob"
});
}
uploadFile(body: string) {
return this.http.post(`${this.apiUrl}/importDatabase`, body, {
headers: new HttpHeaders({
'Content-Type': 'application/xml',
Accept: 'application/xml'
})
});
}
}