frontend-toegevoegd
This commit is contained in:
63
agiledancers-fe/src/app/core/school-api.ts
Normal file
63
agiledancers-fe/src/app/core/school-api.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { inject, Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { forkJoin, Observable } from 'rxjs';
|
||||
import {
|
||||
DanceGroup,
|
||||
Dancer,
|
||||
DancerRequest,
|
||||
GroupRequest,
|
||||
ResourceKind,
|
||||
School,
|
||||
SchoolData,
|
||||
SchoolRequest,
|
||||
Show,
|
||||
ShowRequest,
|
||||
} from './api-models';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class SchoolApi {
|
||||
private readonly http = inject(HttpClient);
|
||||
private readonly base = '/api/dance-schools';
|
||||
|
||||
schools(): Observable<School[]> {
|
||||
return this.http.get<School[]>(this.base);
|
||||
}
|
||||
schoolData(schoolId: number): Observable<SchoolData> {
|
||||
const base = `${this.base}/${schoolId}`;
|
||||
return forkJoin({
|
||||
school: this.http.get<School>(base),
|
||||
dancers: this.http.get<Dancer[]>(`${base}/dancers`),
|
||||
groups: this.http.get<DanceGroup[]>(`${base}/dance-groups`),
|
||||
shows: this.http.get<Show[]>(`${base}/shows`),
|
||||
});
|
||||
}
|
||||
saveSchool(body: SchoolRequest, id?: number): Observable<School> {
|
||||
return id === undefined
|
||||
? this.http.post<School>(this.base, body)
|
||||
: this.http.put<School>(`${this.base}/${id}`, body);
|
||||
}
|
||||
saveDancer(schoolId: number, body: DancerRequest, id?: number): Observable<Dancer> {
|
||||
return this.save(`${this.base}/${schoolId}/dancers`, body, id);
|
||||
}
|
||||
saveGroup(schoolId: number, body: GroupRequest, id?: number): Observable<DanceGroup> {
|
||||
return this.save(`${this.base}/${schoolId}/dance-groups`, body, id);
|
||||
}
|
||||
saveShow(schoolId: number, body: ShowRequest, id?: number): Observable<Show> {
|
||||
return this.save(`${this.base}/${schoolId}/shows`, body, id);
|
||||
}
|
||||
delete(kind: ResourceKind, schoolId: number, id: number): Observable<void> {
|
||||
const collection = { school: '', dancer: '/dancers', group: '/dance-groups', show: '/shows' }[
|
||||
kind
|
||||
];
|
||||
const url =
|
||||
kind === 'school' ? `${this.base}/${id}` : `${this.base}/${schoolId}${collection}/${id}`;
|
||||
return this.http.delete<void>(url);
|
||||
}
|
||||
membership(schoolId: number, groupId: number, dancerId: number, add: boolean): Observable<void> {
|
||||
const url = `${this.base}/${schoolId}/dance-groups/${groupId}/dancers/${dancerId}`;
|
||||
return add ? this.http.put<void>(url, null) : this.http.delete<void>(url);
|
||||
}
|
||||
private save<T>(url: string, body: unknown, id?: number): Observable<T> {
|
||||
return id === undefined ? this.http.post<T>(url, body) : this.http.put<T>(`${url}/${id}`, body);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user