Compare commits
3 Commits
e56644c538
...
8e738d9b3d
Author | SHA1 | Date | |
---|---|---|---|
8e738d9b3d | |||
e36376db3a | |||
aa2eab9a4c |
102
src/api/RequestBuilder.ts
Normal file
102
src/api/RequestBuilder.ts
Normal file
@ -0,0 +1,102 @@
|
||||
import {HttpHeaders} from "@angular/common/http";
|
||||
|
||||
export interface SetRequestBuilderAfterBuild {
|
||||
setRequestBuilder(request: RequestData): void;
|
||||
}
|
||||
|
||||
export interface RequestData {
|
||||
endpoint: string;
|
||||
queryParams: Record<string, string | number | boolean | Array<any> | null> | null;
|
||||
httpHeaders: HttpHeaders;
|
||||
data: any;
|
||||
silenceMode: boolean;
|
||||
}
|
||||
|
||||
export class RequestBuilder {
|
||||
private endpoint: string = '';
|
||||
private queryParams: Record<string, string | number | boolean | Array<any> | null> | null = null;
|
||||
private httpHeaders: HttpHeaders = new HttpHeaders();
|
||||
private data: any = null;
|
||||
private silenceMode: boolean = false;
|
||||
private readonly object: any;
|
||||
|
||||
constructor(obj: any) {
|
||||
this.object = obj;
|
||||
}
|
||||
|
||||
public setEndpoint(endpoint: string): this {
|
||||
this.endpoint = endpoint;
|
||||
return this;
|
||||
}
|
||||
|
||||
public setQueryParams(queryParams: Record<string, string | number | boolean | Array<any> | null>): RequestBuilder {
|
||||
this.queryParams = queryParams;
|
||||
return this;
|
||||
}
|
||||
|
||||
public addHeaders(headers: Record<string, string>): RequestBuilder {
|
||||
Object.keys(headers).forEach(key => {
|
||||
this.httpHeaders = this.httpHeaders.set(key, headers[key]);
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
public setData(data: any): RequestBuilder {
|
||||
this.data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
public setSilenceMode(silence: boolean): RequestBuilder {
|
||||
this.silenceMode = silence;
|
||||
return this;
|
||||
}
|
||||
|
||||
public build<Type>(): Type {
|
||||
(this.object as SetRequestBuilderAfterBuild).setRequestBuilder({
|
||||
endpoint: this.endpoint,
|
||||
queryParams: this.queryParams,
|
||||
httpHeaders: this.httpHeaders,
|
||||
data: this.data,
|
||||
silenceMode: this.silenceMode
|
||||
});
|
||||
return this.object as Type;
|
||||
}
|
||||
|
||||
public getEndpoint(): string {
|
||||
return this.endpoint;
|
||||
}
|
||||
|
||||
public getQueryParams(): Record<string, string | number | boolean | Array<any> | null> | null {
|
||||
return this.queryParams;
|
||||
}
|
||||
|
||||
public getHttpHeaders(): HttpHeaders {
|
||||
return this.httpHeaders;
|
||||
}
|
||||
|
||||
public getData(): any {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
public getSilenceMode(): boolean {
|
||||
return this.silenceMode;
|
||||
}
|
||||
|
||||
public static getStandardRequestData(): RequestData {
|
||||
return {
|
||||
endpoint: '',
|
||||
queryParams: null,
|
||||
httpHeaders: new HttpHeaders(),
|
||||
data: null,
|
||||
silenceMode: false
|
||||
}
|
||||
}
|
||||
|
||||
public reset(): void {
|
||||
this.endpoint = '';
|
||||
this.queryParams = null;
|
||||
this.httpHeaders = new HttpHeaders();
|
||||
this.data = null;
|
||||
this.silenceMode = false;
|
||||
}
|
||||
}
|
@ -1,9 +1,10 @@
|
||||
import {catchError, mergeMap, Observable, retryWhen, timer} from "rxjs";
|
||||
import {catchError, mergeMap, Observable, retryWhen, tap, timer} from "rxjs";
|
||||
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
|
||||
import {NotifyColor, OpenNotifyService} from "@service/open-notify.service";
|
||||
import {environment} from "@environment";
|
||||
import {Router} from "@angular/router";
|
||||
import {Injectable} from "@angular/core";
|
||||
import {RequestBuilder, RequestData, SetRequestBuilderAfterBuild} from "@api/RequestBuilder";
|
||||
|
||||
export function retryWithInterval<T>(): (source: Observable<T>) => Observable<T> {
|
||||
return (source: Observable<T>) =>
|
||||
@ -28,24 +29,23 @@ export function retryWithInterval<T>(): (source: Observable<T>) => Observable<T>
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
*/
|
||||
|
||||
export enum AvailableVersion {
|
||||
v1
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export default abstract class ApiService {
|
||||
export default abstract class ApiService implements SetRequestBuilderAfterBuild {
|
||||
constructor(private http: HttpClient, private notify: OpenNotifyService, private router: Router) {
|
||||
}
|
||||
|
||||
private urlApi = environment.apiUrl;
|
||||
protected abstract basePath: string;
|
||||
protected abstract version: AvailableVersion;
|
||||
private request: RequestData = RequestBuilder.getStandardRequestData();
|
||||
|
||||
public setRequestBuilder(request: RequestData): void {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
private static addQuery(endpoint: string, queryParams?: Record<string, string | number | boolean | Array<any> | null> | null): string {
|
||||
const url = new URL(endpoint);
|
||||
@ -56,8 +56,7 @@ export default abstract class ApiService {
|
||||
if (value !== null && value !== undefined) {
|
||||
if (typeof (value) === typeof (Array)) {
|
||||
(value as Array<any>).forEach(x => url.searchParams.append(key, x.toString()));
|
||||
}
|
||||
else
|
||||
} else
|
||||
url.searchParams.append(key, value.toString());
|
||||
}
|
||||
});
|
||||
@ -72,50 +71,59 @@ export default abstract class ApiService {
|
||||
return test;
|
||||
}
|
||||
|
||||
public get<Type>(endpoint: string = '', queryParams: Record<string, string | number | boolean | Array<any> | null> | null = null): Observable<Type> {
|
||||
return this.http.get<Type>(ApiService.addQuery(ApiService.combineUrls(this.urlApi, AvailableVersion[this.version], this.basePath, endpoint), queryParams), {withCredentials: true}).pipe(
|
||||
private makeHttpRequest<Type>(method: 'get' | 'post' | 'delete' | 'put'): Observable<Type> {
|
||||
const doneEndpoint = ApiService.addQuery(ApiService.combineUrls(this.urlApi, AvailableVersion[this.version], this.basePath, this.request.endpoint), this.request.queryParams);
|
||||
|
||||
return this.http.request<Type>(method, doneEndpoint, {
|
||||
withCredentials: true,
|
||||
headers: this.request.httpHeaders,
|
||||
body: this.request.data
|
||||
}).pipe(
|
||||
tap(_ => this.request = RequestBuilder.getStandardRequestData()),
|
||||
retryWithInterval<Type>(),
|
||||
catchError(error => {
|
||||
if (!this.request.silenceMode)
|
||||
this.handleError(error);
|
||||
|
||||
this.request = RequestBuilder.getStandardRequestData();
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
public post<Type>(endpoint: string, data: any, queryParams: Record<string, string | number | boolean | Array<any> | null> | null = null): Observable<Type> {
|
||||
return this.http.post<Type>(ApiService.addQuery(ApiService.combineUrls(this.urlApi, AvailableVersion[this.version], this.basePath, endpoint), queryParams), data, {withCredentials: true}).pipe(
|
||||
retryWithInterval<Type>(),
|
||||
catchError(error => {
|
||||
this.handleError(error);
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
public createRequestBuilder() {
|
||||
this.request = RequestBuilder.getStandardRequestData();
|
||||
return new RequestBuilder(this);
|
||||
}
|
||||
|
||||
public put<Type>(endpoint: string, data: any, queryParams: Record<string, string | number | boolean | Array<any> | null> | null = null): Observable<Type> {
|
||||
return this.http.put<Type>(ApiService.addQuery(ApiService.combineUrls(this.urlApi, AvailableVersion[this.version], this.basePath, endpoint), queryParams), data, {withCredentials: true}).pipe(
|
||||
retryWithInterval<Type>(),
|
||||
catchError(error => {
|
||||
this.handleError(error);
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
public get<Type>(endpoint: string = ''): Observable<Type> {
|
||||
if (endpoint)
|
||||
this.request.endpoint = endpoint;
|
||||
return this.makeHttpRequest<Type>('get');
|
||||
}
|
||||
|
||||
public delete<Type>(endpoint: string): Observable<Type> {
|
||||
return this.http.delete<Type>(ApiService.combineUrls(this.urlApi, AvailableVersion[this.version], this.basePath, endpoint), {withCredentials: true}).pipe(
|
||||
retryWithInterval<Type>(),
|
||||
catchError(error => {
|
||||
this.handleError(error);
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
public post<Type>(endpoint: string = ''): Observable<Type> {
|
||||
if (endpoint)
|
||||
this.request.endpoint = endpoint;
|
||||
return this.makeHttpRequest<Type>('post');
|
||||
}
|
||||
|
||||
public put<Type>(endpoint: string = ''): Observable<Type> {
|
||||
if (endpoint)
|
||||
this.request.endpoint = endpoint;
|
||||
return this.makeHttpRequest<Type>('put');
|
||||
}
|
||||
|
||||
public delete<Type>(endpoint: string = ''): Observable<Type> {
|
||||
if (endpoint)
|
||||
this.request.endpoint = endpoint;
|
||||
return this.makeHttpRequest<Type>('delete');
|
||||
}
|
||||
|
||||
private handleError(error: HttpErrorResponse): void {
|
||||
// todo: change to Retry-After condition
|
||||
if (error.error.toString().includes("setup")) {
|
||||
this.router.navigate(['/setup/']);
|
||||
this.router.navigate(['/setup/']).then();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -5,8 +5,8 @@ import {CampusDetailsResponse} from "@api/v1/campusDetailsResponse";
|
||||
|
||||
@Injectable()
|
||||
export class CampusService extends ApiService {
|
||||
protected basePath = 'Campus/';
|
||||
protected version = AvailableVersion.v1;
|
||||
public readonly basePath = 'Campus/';
|
||||
public readonly version = AvailableVersion.v1;
|
||||
|
||||
public getCampus() {
|
||||
return this.get<CampusBasicInfoResponse[]>();
|
||||
|
@ -4,11 +4,14 @@ import {DisciplineResponse} from "@api/v1/disciplineResponse";
|
||||
|
||||
@Injectable()
|
||||
export class DisciplineService extends ApiService {
|
||||
protected basePath = 'Discipline/';
|
||||
protected version = AvailableVersion.v1;
|
||||
public readonly basePath = 'Discipline/';
|
||||
public readonly version = AvailableVersion.v1;
|
||||
|
||||
public getDisciplines(page: number | null = null, pageSize: number | null = null) {
|
||||
return this.get<DisciplineResponse[]>('', {page: page, pageSize: pageSize});
|
||||
return this.createRequestBuilder()
|
||||
.setQueryParams({page: page, pageSize: pageSize})
|
||||
.build<ApiService>()
|
||||
.get<DisciplineResponse[]>();
|
||||
}
|
||||
|
||||
public getById(id: number) {
|
||||
|
@ -5,11 +5,14 @@ import {FacultyDetailsResponse} from "@api/v1/facultyDetailsResponse";
|
||||
|
||||
@Injectable()
|
||||
export class FacultyService extends ApiService {
|
||||
protected basePath = 'Faculty/';
|
||||
protected version = AvailableVersion.v1;
|
||||
public readonly basePath = 'Faculty/';
|
||||
public readonly version = AvailableVersion.v1;
|
||||
|
||||
public getFaculties(page: number | null = null, pageSize: number | null = null) {
|
||||
return this.get<FacultyResponse[]>('', {page: page, pageSize: pageSize});
|
||||
return this.createRequestBuilder()
|
||||
.setQueryParams({page: page, pageSize: pageSize})
|
||||
.build<ApiService>()
|
||||
.get<FacultyResponse[]>();
|
||||
|
||||
}
|
||||
|
||||
|
@ -5,11 +5,14 @@ import {GroupDetailsResponse} from "@api/v1/groupDetailsResponse";
|
||||
|
||||
@Injectable()
|
||||
export class GroupService extends ApiService {
|
||||
protected basePath = 'Group/';
|
||||
protected version = AvailableVersion.v1;
|
||||
public readonly basePath = 'Group/';
|
||||
public readonly version = AvailableVersion.v1;
|
||||
|
||||
public getGroups(page: number | null = null, pageSize: number | null = null) {
|
||||
return this.get<GroupResponse[]>('', {page: page, pageSize: pageSize});
|
||||
return this.createRequestBuilder()
|
||||
.setQueryParams({page: page, pageSize: pageSize})
|
||||
.build<ApiService>()
|
||||
.get<GroupResponse[]>();
|
||||
}
|
||||
|
||||
public getById(id: number) {
|
||||
|
@ -5,8 +5,8 @@ import {LectureHallDetailsResponse} from "@api/v1/lectureHallDetailsResponse";
|
||||
|
||||
@Injectable()
|
||||
export class LectureHallService extends ApiService {
|
||||
protected basePath = 'LectureHall/';
|
||||
protected version = AvailableVersion.v1;
|
||||
public readonly basePath = 'LectureHall/';
|
||||
public readonly version = AvailableVersion.v1;
|
||||
|
||||
public getLectureHalls() {
|
||||
return this.get<LectureHallResponse[]>();
|
||||
|
@ -4,11 +4,14 @@ import {ProfessorResponse} from "@api/v1/professorResponse";
|
||||
|
||||
@Injectable()
|
||||
export class ProfessorService extends ApiService {
|
||||
protected basePath = 'Professor/';
|
||||
protected version = AvailableVersion.v1;
|
||||
public readonly basePath = 'Professor/';
|
||||
public readonly version = AvailableVersion.v1;
|
||||
|
||||
public getProfessors(page: number | null = null, pageSize: number | null = null) {
|
||||
return this.get<ProfessorResponse[]>('', {page: page, pageSize: pageSize});
|
||||
return this.createRequestBuilder()
|
||||
.setQueryParams({page: page, pageSize: pageSize})
|
||||
.build<ApiService>()
|
||||
.get<ProfessorResponse[]>();
|
||||
}
|
||||
|
||||
public getById(id: number) {
|
||||
|
@ -8,8 +8,8 @@ import {map} from "rxjs";
|
||||
|
||||
@Injectable()
|
||||
export class ScheduleService extends ApiService {
|
||||
protected basePath = 'Schedule/';
|
||||
protected version = AvailableVersion.v1;
|
||||
public readonly basePath = 'Schedule/';
|
||||
public readonly version = AvailableVersion.v1;
|
||||
|
||||
public startTerm() {
|
||||
return this.get<string>('StartTerm').pipe(map(date => new DateOnly(date)));
|
||||
@ -20,22 +20,41 @@ export class ScheduleService extends ApiService {
|
||||
}
|
||||
|
||||
public postSchedule(data: ScheduleRequest) {
|
||||
return this.post<ScheduleResponse[]>('', data);
|
||||
return this.createRequestBuilder()
|
||||
.setData(data)
|
||||
.build<ApiService>()
|
||||
.post<ScheduleResponse[]>();
|
||||
}
|
||||
|
||||
public getByGroup(id : number, isEven: boolean | null = null, disciplines: Array<number> | null = null, professors: Array<number> | null = null, lectureHalls: Array<number> | null = null) {
|
||||
return this.get<ScheduleResponse[]>('GetByGroup/' + id.toString(), {isEven: isEven, disciplines: disciplines, professors: professors, lectureHalls: lectureHalls});
|
||||
return this.createRequestBuilder()
|
||||
.setEndpoint('GetByGroup/' + id.toString())
|
||||
.setQueryParams({isEven: isEven, disciplines: disciplines, professors: professors, lectureHalls: lectureHalls})
|
||||
.build<ApiService>()
|
||||
.get<ScheduleResponse[]>();
|
||||
}
|
||||
|
||||
public getByProfessor(id : number, isEven: boolean | null = null, disciplines: Array<number> | null = null, groups: Array<number> | null = null, lectureHalls: Array<number> | null = null) {
|
||||
return this.get<ScheduleResponse[]>('GetByProfessor/' + id.toString(), {isEven: isEven, disciplines: disciplines, groups: groups, lectureHalls: lectureHalls});
|
||||
return this.createRequestBuilder()
|
||||
.setEndpoint('GetByProfessor/' + id.toString())
|
||||
.setQueryParams({isEven: isEven, disciplines: disciplines, groups: groups, lectureHalls: lectureHalls})
|
||||
.build<ApiService>()
|
||||
.get<ScheduleResponse[]>();
|
||||
}
|
||||
|
||||
public getByLectureHall(id : number, isEven: boolean | null = null, disciplines: Array<number> | null = null, groups: Array<number> | null = null, professors: Array<number> | null = null) {
|
||||
return this.get<ScheduleResponse[]>('GetByLectureHall/' + id.toString(), {isEven: isEven, disciplines: disciplines, groups: groups, professors: professors});
|
||||
return this.createRequestBuilder()
|
||||
.setEndpoint('GetByLectureHall/' + id.toString())
|
||||
.setQueryParams({isEven: isEven, disciplines: disciplines, groups: groups, professors: professors})
|
||||
.build<ApiService>()
|
||||
.get<ScheduleResponse[]>();
|
||||
}
|
||||
|
||||
public getByDiscipline(id : number, isEven: boolean | null = null, groups: Array<number> | null = null, professors: Array<number> | null = null, lectureHalls: Array<number> | null = null) {
|
||||
return this.get<ScheduleResponse[]>('GetByDiscipline/' + id.toString(), {isEven: isEven, groups: groups, professors: professors, lectureHalls: lectureHalls});
|
||||
return this.createRequestBuilder()
|
||||
.setEndpoint('GetByDiscipline/' + id.toString())
|
||||
.setQueryParams({isEven: isEven, groups: groups, professors: professors, lectureHalls: lectureHalls})
|
||||
.build<ApiService>()
|
||||
.get<ScheduleResponse[]>();
|
||||
}
|
||||
}
|
||||
|
@ -10,51 +10,88 @@ import {DateOnly} from "@model/DateOnly";
|
||||
|
||||
@Injectable()
|
||||
export default class SetupService extends ApiService {
|
||||
protected basePath = 'Setup/';
|
||||
public readonly basePath = 'Setup/';
|
||||
public readonly version = AvailableVersion.v1;
|
||||
|
||||
public checkToken(token: string) {
|
||||
return this.get<boolean>('CheckToken', {token: token});
|
||||
return this.createRequestBuilder()
|
||||
.setEndpoint('CheckToken')
|
||||
.setQueryParams({token: token})
|
||||
.build<ApiService>()
|
||||
.get<boolean>();
|
||||
}
|
||||
|
||||
public setPsql(data: DatabaseRequest) {
|
||||
return this.post<boolean>('SetPsql', data);
|
||||
return this.createRequestBuilder()
|
||||
.setEndpoint('SetPsql')
|
||||
.setData(data)
|
||||
.build<ApiService>()
|
||||
.post<boolean>();
|
||||
}
|
||||
|
||||
public setMysql(data: DatabaseRequest) {
|
||||
return this.post<boolean>('SetMysql', data);
|
||||
return this.createRequestBuilder()
|
||||
.setEndpoint('SetMysql')
|
||||
.setData(data)
|
||||
.build<ApiService>()
|
||||
.post<boolean>();
|
||||
}
|
||||
|
||||
public setSqlite(path: string | null = null) {
|
||||
return this.post<boolean>('SetSqlite', null, {path: path});
|
||||
return this.createRequestBuilder()
|
||||
.setEndpoint('SetSqlite')
|
||||
.setQueryParams({path: path})
|
||||
.build<ApiService>()
|
||||
.get<boolean>();
|
||||
}
|
||||
|
||||
public setRedis(data: CacheRequest) {
|
||||
return this.post<boolean>('SetRedis', data);
|
||||
return this.createRequestBuilder()
|
||||
.setEndpoint('SetRedis')
|
||||
.setData(data)
|
||||
.build<ApiService>()
|
||||
.post<boolean>();
|
||||
}
|
||||
|
||||
public setMemcached() {
|
||||
return this.post<boolean>('SetMemcached', null);
|
||||
return this.post<boolean>('SetMemcached');
|
||||
}
|
||||
|
||||
public createAdmin(data: CreateUserRequest) {
|
||||
return this.post<boolean>('CreateAdmin', data);
|
||||
return this.createRequestBuilder()
|
||||
.setEndpoint('CreateAdmin')
|
||||
.setData(data)
|
||||
.build<ApiService>()
|
||||
.post<boolean>();
|
||||
}
|
||||
|
||||
public setLogging(data: LoggingRequest | null = null) {
|
||||
return this.post<boolean>('SetLogging', data);
|
||||
return this.createRequestBuilder()
|
||||
.setEndpoint('SetLogging')
|
||||
.setData(data)
|
||||
.build<ApiService>()
|
||||
.post<boolean>();
|
||||
}
|
||||
|
||||
public setEmail(data: EmailRequest | null = null) {
|
||||
return this.post<boolean>('SetEmail', data);
|
||||
return this.createRequestBuilder()
|
||||
.setEndpoint('SetEmail')
|
||||
.setData(data)
|
||||
.build<ApiService>()
|
||||
.post<boolean>();
|
||||
}
|
||||
|
||||
public setSchedule(data: ScheduleConfigurationRequest) {
|
||||
data.startTerm = new DateOnly(data.startTerm).toString();
|
||||
return this.post<boolean>('SetSchedule', data);
|
||||
|
||||
return this.createRequestBuilder()
|
||||
.setEndpoint('SetSchedule')
|
||||
.setData(data)
|
||||
.build<ApiService>()
|
||||
.post<boolean>();
|
||||
}
|
||||
|
||||
public submit() {
|
||||
return this.post<boolean>('Submit', null);
|
||||
return this.post<boolean>('Submit');
|
||||
}
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ export class ScheduleComponent {
|
||||
|
||||
@ViewChild('tableHeader') childComponent!: TableHeaderComponent;
|
||||
|
||||
constructor(private api: ScheduleService) {
|
||||
constructor(api: ScheduleService) {
|
||||
this.calculateCurrentWeek();
|
||||
this.startTerm = new Date(1, 1, 1);
|
||||
api.pairPeriod().subscribe(date => {
|
||||
|
Loading…
Reference in New Issue
Block a user