Compare commits

..

No commits in common. "8e738d9b3dee1bf154edf1743c7bd8efef430ada" and "e56644c5382205ff73afd894b3bad14495aa5612" have entirely different histories.

11 changed files with 76 additions and 254 deletions

View File

@ -1,102 +0,0 @@
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;
}
}

View File

@ -1,10 +1,9 @@
import {catchError, mergeMap, Observable, retryWhen, tap, timer} from "rxjs"; import {catchError, mergeMap, Observable, retryWhen, timer} from "rxjs";
import {HttpClient, HttpErrorResponse} from "@angular/common/http"; import {HttpClient, HttpErrorResponse} from "@angular/common/http";
import {NotifyColor, OpenNotifyService} from "@service/open-notify.service"; import {NotifyColor, OpenNotifyService} from "@service/open-notify.service";
import {environment} from "@environment"; import {environment} from "@environment";
import {Router} from "@angular/router"; import {Router} from "@angular/router";
import {Injectable} from "@angular/core"; import {Injectable} from "@angular/core";
import {RequestBuilder, RequestData, SetRequestBuilderAfterBuild} from "@api/RequestBuilder";
export function retryWithInterval<T>(): (source: Observable<T>) => Observable<T> { export function retryWithInterval<T>(): (source: Observable<T>) => Observable<T> {
return (source: Observable<T>) => return (source: Observable<T>) =>
@ -29,23 +28,24 @@ export function retryWithInterval<T>(): (source: Observable<T>) => Observable<T>
); );
} }
/*
@Injectable({
providedIn: 'root'
})
*/
export enum AvailableVersion { export enum AvailableVersion {
v1 v1
} }
@Injectable() @Injectable()
export default abstract class ApiService implements SetRequestBuilderAfterBuild { export default abstract class ApiService {
constructor(private http: HttpClient, private notify: OpenNotifyService, private router: Router) { constructor(private http: HttpClient, private notify: OpenNotifyService, private router: Router) {
} }
private urlApi = environment.apiUrl; private urlApi = environment.apiUrl;
protected abstract basePath: string; protected abstract basePath: string;
protected abstract version: AvailableVersion; 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 { private static addQuery(endpoint: string, queryParams?: Record<string, string | number | boolean | Array<any> | null> | null): string {
const url = new URL(endpoint); const url = new URL(endpoint);
@ -54,9 +54,10 @@ export default abstract class ApiService implements SetRequestBuilderAfterBuild
Object.keys(queryParams).forEach(key => { Object.keys(queryParams).forEach(key => {
const value = queryParams[key]; const value = queryParams[key];
if (value !== null && value !== undefined) { if (value !== null && value !== undefined) {
if (typeof (value) === typeof (Array)) { if (typeof(value) === typeof(Array)) {
(value as Array<any>).forEach(x => url.searchParams.append(key, x.toString())); (value as Array<any>).forEach(x => url.searchParams.append(key, x.toString()));
} else }
else
url.searchParams.append(key, value.toString()); url.searchParams.append(key, value.toString());
} }
}); });
@ -71,59 +72,50 @@ export default abstract class ApiService implements SetRequestBuilderAfterBuild
return test; return test;
} }
private makeHttpRequest<Type>(method: 'get' | 'post' | 'delete' | 'put'): Observable<Type> { public get<Type>(endpoint: string = '', queryParams: Record<string, string | number | boolean | Array<any> | null> | null = null): Observable<Type> {
const doneEndpoint = ApiService.addQuery(ApiService.combineUrls(this.urlApi, AvailableVersion[this.version], this.basePath, this.request.endpoint), this.request.queryParams); return this.http.get<Type>(ApiService.addQuery(ApiService.combineUrls(this.urlApi, AvailableVersion[this.version], this.basePath, endpoint), queryParams), {withCredentials: true}).pipe(
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>(), retryWithInterval<Type>(),
catchError(error => { catchError(error => {
if (!this.request.silenceMode) this.handleError(error);
this.handleError(error);
this.request = RequestBuilder.getStandardRequestData();
throw error; throw error;
}) })
); );
} }
public createRequestBuilder() { public post<Type>(endpoint: string, data: any, queryParams: Record<string, string | number | boolean | Array<any> | null> | null = null): Observable<Type> {
this.request = RequestBuilder.getStandardRequestData(); return this.http.post<Type>(ApiService.addQuery(ApiService.combineUrls(this.urlApi, AvailableVersion[this.version], this.basePath, endpoint), queryParams), data, {withCredentials: true}).pipe(
return new RequestBuilder(this); retryWithInterval<Type>(),
catchError(error => {
this.handleError(error);
throw error;
})
);
} }
public get<Type>(endpoint: string = ''): Observable<Type> { public put<Type>(endpoint: string, data: any, queryParams: Record<string, string | number | boolean | Array<any> | null> | null = null): Observable<Type> {
if (endpoint) return this.http.put<Type>(ApiService.addQuery(ApiService.combineUrls(this.urlApi, AvailableVersion[this.version], this.basePath, endpoint), queryParams), data, {withCredentials: true}).pipe(
this.request.endpoint = endpoint; retryWithInterval<Type>(),
return this.makeHttpRequest<Type>('get'); catchError(error => {
this.handleError(error);
throw error;
})
);
} }
public post<Type>(endpoint: string = ''): Observable<Type> { public delete<Type>(endpoint: string): Observable<Type> {
if (endpoint) return this.http.delete<Type>(ApiService.combineUrls(this.urlApi, AvailableVersion[this.version], this.basePath, endpoint), {withCredentials: true}).pipe(
this.request.endpoint = endpoint; retryWithInterval<Type>(),
return this.makeHttpRequest<Type>('post'); catchError(error => {
} this.handleError(error);
throw error;
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 { private handleError(error: HttpErrorResponse): void {
// todo: change to Retry-After condition // todo: change to Retry-After condition
if (error.error.toString().includes("setup")) { if (error.error.toString().includes("setup")) {
this.router.navigate(['/setup/']).then(); this.router.navigate(['/setup/']);
return; return;
} }

View File

@ -5,8 +5,8 @@ import {CampusDetailsResponse} from "@api/v1/campusDetailsResponse";
@Injectable() @Injectable()
export class CampusService extends ApiService { export class CampusService extends ApiService {
public readonly basePath = 'Campus/'; protected basePath = 'Campus/';
public readonly version = AvailableVersion.v1; protected version = AvailableVersion.v1;
public getCampus() { public getCampus() {
return this.get<CampusBasicInfoResponse[]>(); return this.get<CampusBasicInfoResponse[]>();

View File

@ -4,14 +4,11 @@ import {DisciplineResponse} from "@api/v1/disciplineResponse";
@Injectable() @Injectable()
export class DisciplineService extends ApiService { export class DisciplineService extends ApiService {
public readonly basePath = 'Discipline/'; protected basePath = 'Discipline/';
public readonly version = AvailableVersion.v1; protected version = AvailableVersion.v1;
public getDisciplines(page: number | null = null, pageSize: number | null = null) { public getDisciplines(page: number | null = null, pageSize: number | null = null) {
return this.createRequestBuilder() return this.get<DisciplineResponse[]>('', {page: page, pageSize: pageSize});
.setQueryParams({page: page, pageSize: pageSize})
.build<ApiService>()
.get<DisciplineResponse[]>();
} }
public getById(id: number) { public getById(id: number) {

View File

@ -5,14 +5,11 @@ import {FacultyDetailsResponse} from "@api/v1/facultyDetailsResponse";
@Injectable() @Injectable()
export class FacultyService extends ApiService { export class FacultyService extends ApiService {
public readonly basePath = 'Faculty/'; protected basePath = 'Faculty/';
public readonly version = AvailableVersion.v1; protected version = AvailableVersion.v1;
public getFaculties(page: number | null = null, pageSize: number | null = null) { public getFaculties(page: number | null = null, pageSize: number | null = null) {
return this.createRequestBuilder() return this.get<FacultyResponse[]>('', {page: page, pageSize: pageSize});
.setQueryParams({page: page, pageSize: pageSize})
.build<ApiService>()
.get<FacultyResponse[]>();
} }

View File

@ -5,14 +5,11 @@ import {GroupDetailsResponse} from "@api/v1/groupDetailsResponse";
@Injectable() @Injectable()
export class GroupService extends ApiService { export class GroupService extends ApiService {
public readonly basePath = 'Group/'; protected basePath = 'Group/';
public readonly version = AvailableVersion.v1; protected version = AvailableVersion.v1;
public getGroups(page: number | null = null, pageSize: number | null = null) { public getGroups(page: number | null = null, pageSize: number | null = null) {
return this.createRequestBuilder() return this.get<GroupResponse[]>('', {page: page, pageSize: pageSize});
.setQueryParams({page: page, pageSize: pageSize})
.build<ApiService>()
.get<GroupResponse[]>();
} }
public getById(id: number) { public getById(id: number) {

View File

@ -5,8 +5,8 @@ import {LectureHallDetailsResponse} from "@api/v1/lectureHallDetailsResponse";
@Injectable() @Injectable()
export class LectureHallService extends ApiService { export class LectureHallService extends ApiService {
public readonly basePath = 'LectureHall/'; protected basePath = 'LectureHall/';
public readonly version = AvailableVersion.v1; protected version = AvailableVersion.v1;
public getLectureHalls() { public getLectureHalls() {
return this.get<LectureHallResponse[]>(); return this.get<LectureHallResponse[]>();

View File

@ -4,14 +4,11 @@ import {ProfessorResponse} from "@api/v1/professorResponse";
@Injectable() @Injectable()
export class ProfessorService extends ApiService { export class ProfessorService extends ApiService {
public readonly basePath = 'Professor/'; protected basePath = 'Professor/';
public readonly version = AvailableVersion.v1; protected version = AvailableVersion.v1;
public getProfessors(page: number | null = null, pageSize: number | null = null) { public getProfessors(page: number | null = null, pageSize: number | null = null) {
return this.createRequestBuilder() return this.get<ProfessorResponse[]>('', {page: page, pageSize: pageSize});
.setQueryParams({page: page, pageSize: pageSize})
.build<ApiService>()
.get<ProfessorResponse[]>();
} }
public getById(id: number) { public getById(id: number) {

View File

@ -8,8 +8,8 @@ import {map} from "rxjs";
@Injectable() @Injectable()
export class ScheduleService extends ApiService { export class ScheduleService extends ApiService {
public readonly basePath = 'Schedule/'; protected basePath = 'Schedule/';
public readonly version = AvailableVersion.v1; protected version = AvailableVersion.v1;
public startTerm() { public startTerm() {
return this.get<string>('StartTerm').pipe(map(date => new DateOnly(date))); return this.get<string>('StartTerm').pipe(map(date => new DateOnly(date)));
@ -20,41 +20,22 @@ export class ScheduleService extends ApiService {
} }
public postSchedule(data: ScheduleRequest) { public postSchedule(data: ScheduleRequest) {
return this.createRequestBuilder() return this.post<ScheduleResponse[]>('', data);
.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) { 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.createRequestBuilder() return this.get<ScheduleResponse[]>('GetByGroup/' + id.toString(), {isEven: isEven, disciplines: disciplines, professors: professors, lectureHalls: lectureHalls});
.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) { 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.createRequestBuilder() return this.get<ScheduleResponse[]>('GetByProfessor/' + id.toString(), {isEven: isEven, disciplines: disciplines, groups: groups, lectureHalls: lectureHalls});
.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) { 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.createRequestBuilder() return this.get<ScheduleResponse[]>('GetByLectureHall/' + id.toString(), {isEven: isEven, disciplines: disciplines, groups: groups, professors: professors});
.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) { 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.createRequestBuilder() return this.get<ScheduleResponse[]>('GetByDiscipline/' + id.toString(), {isEven: isEven, groups: groups, professors: professors, lectureHalls: lectureHalls});
.setEndpoint('GetByDiscipline/' + id.toString())
.setQueryParams({isEven: isEven, groups: groups, professors: professors, lectureHalls: lectureHalls})
.build<ApiService>()
.get<ScheduleResponse[]>();
} }
} }

View File

@ -10,88 +10,51 @@ import {DateOnly} from "@model/DateOnly";
@Injectable() @Injectable()
export default class SetupService extends ApiService { export default class SetupService extends ApiService {
public readonly basePath = 'Setup/'; protected basePath = 'Setup/';
public readonly version = AvailableVersion.v1; public readonly version = AvailableVersion.v1;
public checkToken(token: string) { public checkToken(token: string) {
return this.createRequestBuilder() return this.get<boolean>('CheckToken', {token: token});
.setEndpoint('CheckToken')
.setQueryParams({token: token})
.build<ApiService>()
.get<boolean>();
} }
public setPsql(data: DatabaseRequest) { public setPsql(data: DatabaseRequest) {
return this.createRequestBuilder() return this.post<boolean>('SetPsql', data);
.setEndpoint('SetPsql')
.setData(data)
.build<ApiService>()
.post<boolean>();
} }
public setMysql(data: DatabaseRequest) { public setMysql(data: DatabaseRequest) {
return this.createRequestBuilder() return this.post<boolean>('SetMysql', data);
.setEndpoint('SetMysql')
.setData(data)
.build<ApiService>()
.post<boolean>();
} }
public setSqlite(path: string | null = null) { public setSqlite(path: string | null = null) {
return this.createRequestBuilder() return this.post<boolean>('SetSqlite', null, {path: path});
.setEndpoint('SetSqlite')
.setQueryParams({path: path})
.build<ApiService>()
.get<boolean>();
} }
public setRedis(data: CacheRequest) { public setRedis(data: CacheRequest) {
return this.createRequestBuilder() return this.post<boolean>('SetRedis', data);
.setEndpoint('SetRedis')
.setData(data)
.build<ApiService>()
.post<boolean>();
} }
public setMemcached() { public setMemcached() {
return this.post<boolean>('SetMemcached'); return this.post<boolean>('SetMemcached', null);
} }
public createAdmin(data: CreateUserRequest) { public createAdmin(data: CreateUserRequest) {
return this.createRequestBuilder() return this.post<boolean>('CreateAdmin', data);
.setEndpoint('CreateAdmin')
.setData(data)
.build<ApiService>()
.post<boolean>();
} }
public setLogging(data: LoggingRequest | null = null) { public setLogging(data: LoggingRequest | null = null) {
return this.createRequestBuilder() return this.post<boolean>('SetLogging', data);
.setEndpoint('SetLogging')
.setData(data)
.build<ApiService>()
.post<boolean>();
} }
public setEmail(data: EmailRequest | null = null) { public setEmail(data: EmailRequest | null = null) {
return this.createRequestBuilder() return this.post<boolean>('SetEmail', data);
.setEndpoint('SetEmail')
.setData(data)
.build<ApiService>()
.post<boolean>();
} }
public setSchedule(data: ScheduleConfigurationRequest) { public setSchedule(data: ScheduleConfigurationRequest) {
data.startTerm = new DateOnly(data.startTerm).toString(); 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() { public submit() {
return this.post<boolean>('Submit'); return this.post<boolean>('Submit', null);
} }
} }

View File

@ -44,7 +44,7 @@ export class ScheduleComponent {
@ViewChild('tableHeader') childComponent!: TableHeaderComponent; @ViewChild('tableHeader') childComponent!: TableHeaderComponent;
constructor(api: ScheduleService) { constructor(private api: ScheduleService) {
this.calculateCurrentWeek(); this.calculateCurrentWeek();
this.startTerm = new Date(1, 1, 1); this.startTerm = new Date(1, 1, 1);
api.pairPeriod().subscribe(date => { api.pairPeriod().subscribe(date => {