168 lines
4.3 KiB
TypeScript
168 lines
4.3 KiB
TypeScript
import { HttpClient } from '@angular/common/http';
|
|
import { inject, Injectable } from '@angular/core';
|
|
import { BehaviorSubject, Observable } from 'rxjs';
|
|
import { IUser } from '../model/interface/user.interface';
|
|
import { IKey } from '../model/interface/key.interface';
|
|
import { ICylinder } from '../model/interface/cylinder.interface';
|
|
import { HotToastService } from '@ngxpert/hot-toast';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class ApiService {
|
|
private http: HttpClient = inject(HttpClient);
|
|
private toast: HotToastService = inject(HotToastService);
|
|
|
|
public keys: BehaviorSubject<IKey[]> = new BehaviorSubject<IKey[]>([]);
|
|
public cylinders: BehaviorSubject<ICylinder[]> = new BehaviorSubject<ICylinder[]>([]);
|
|
|
|
|
|
|
|
constructor() { }
|
|
|
|
|
|
getAllUsers(): Observable<IUser[]> {
|
|
return this.http.get<IUser[]>('/api/user');
|
|
}
|
|
|
|
saveUser(user: IUser) {
|
|
return this.http.post('/api/user', user);
|
|
}
|
|
|
|
getRoles(): Observable<{id: string, name: string}[]> {
|
|
return this.http.get<{id: string, name: string}[]>('/api/role');
|
|
}
|
|
|
|
private getKeys(): Observable<IKey[]> {
|
|
return this.http.get<IKey[]>('api/key')
|
|
}
|
|
|
|
refreshKeys(): void{
|
|
this.getKeys().subscribe({
|
|
next: keys => {
|
|
this.keys.next(keys);
|
|
},
|
|
error: () => {
|
|
this.toast.error('Fehler beim Laden der Schlüssel')
|
|
}
|
|
})
|
|
}
|
|
|
|
getLostKeys(): Observable<IKey[]> {
|
|
return this.http.get<IKey[]>('api/key/lost')
|
|
}
|
|
|
|
updateKey(key: IKey): Observable<IKey> {
|
|
return this.http.put<IKey>('api/key', key);
|
|
}
|
|
|
|
createKey(key: any) {
|
|
return this.http.post<IKey>('api/key', key);
|
|
}
|
|
|
|
createSystem(keySystem: any) {
|
|
return this.http.post('api/system', keySystem);
|
|
}
|
|
|
|
getSystems(): Observable<any[]> {
|
|
return this.http.get<any[]>('api/system');
|
|
}
|
|
|
|
getSystemManagers(id: string): Observable<IUser[]> {
|
|
return this.http.get<any[]>(`api/system/${id}/manager`);
|
|
}
|
|
|
|
handoverKey(data: any) {
|
|
return this.http.post(`api/key/${data.key.id}/handover`, data);
|
|
}
|
|
|
|
getHandovers(keyID: string): Observable<any[]> {
|
|
return this.http.get<any[]>(`api/key/${keyID}/handover`);
|
|
}
|
|
|
|
createCustomer(data: { name: string, system: any}) {
|
|
return this.http.post('api/customer', data);
|
|
}
|
|
|
|
getCustomers(): Observable<any[]> {
|
|
return this.http.get<any[]>('api/customer')
|
|
}
|
|
|
|
deleteKey(id: string) {
|
|
return this.http.delete(`api/key/${id}`);
|
|
}
|
|
|
|
getKeyArchive(): Observable<IKey[]> {
|
|
return this.http.get<IKey[]>('api/key/archive');
|
|
}
|
|
|
|
restoreKey(id: string) {
|
|
return this.http.put(`api/key/${id}/restore`, null);
|
|
}
|
|
|
|
private getCylinders(): Observable<ICylinder[]> {
|
|
return this.http.get<ICylinder[]>('api/cylinder');
|
|
}
|
|
|
|
/**
|
|
* Aktualisiert die Zylinder im Behaviour Subject
|
|
* cylinders
|
|
* @returns Promise wenn geladen
|
|
*/
|
|
refreshCylinders(): Promise<void> {
|
|
return new Promise<void>(resolve => {
|
|
this.getCylinders().subscribe({
|
|
next: data => {
|
|
this.cylinders.next(data);
|
|
},
|
|
error: () => {
|
|
this.toast.error('Fehler beim Laden der Zylinder')
|
|
},
|
|
complete: () => resolve()
|
|
})
|
|
})
|
|
}
|
|
|
|
deleteCylinder(cylinder: ICylinder): Observable<any> {
|
|
return this.http.delete(`api/cylinder/${cylinder.id}`)
|
|
}
|
|
|
|
deleteUser(id: string) {
|
|
return this.http.delete(`api/user/${id}`)
|
|
}
|
|
|
|
addManager(systemID: string, email: string): Observable<IUser[]> {
|
|
return this.http.post<IUser[]>(`api/system/${systemID}/manager`, {
|
|
email,
|
|
action: 'add'
|
|
});
|
|
}
|
|
|
|
removeManager(systemID: string, email: string): Observable<IUser[]> {
|
|
return this.http.post<IUser[]>(`api/system/${systemID}/manager`, {
|
|
email,
|
|
action: 'remove'
|
|
});
|
|
}
|
|
|
|
getStats(): Observable<{ keys: number, cylinders: number, systems: number, handedOut: number }> {
|
|
return this.http.get<{ keys: number, cylinders: number, systems: number, handedOut: number }>('api/user/stats');
|
|
}
|
|
|
|
getActivities(): Observable<any[]> {
|
|
return this.http.get<any[]>('api/user/activities');
|
|
}
|
|
|
|
createCylinder(data: any) {
|
|
return this.http.post<ICylinder>('api/cylinder', data);
|
|
}
|
|
|
|
getSettings(): Observable<any> {
|
|
return this.http.get('api/user/settings')
|
|
}
|
|
|
|
updateSettings(settings: any): Observable<any> {
|
|
return this.http.post('api/user/settings', settings)
|
|
}
|
|
}
|