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 {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>) =>
@ -29,23 +28,24 @@ export function retryWithInterval<T>(): (source: Observable<T>) => Observable<T>
);
}
/*
@Injectable({
providedIn: 'root'
})
*/
export enum AvailableVersion {
v1
}
@Injectable()
export default abstract class ApiService implements SetRequestBuilderAfterBuild {
export default abstract class ApiService {
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,7 +56,8 @@ export default abstract class ApiService implements SetRequestBuilderAfterBuild
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());
}
});
@ -71,59 +72,50 @@ export default abstract class ApiService implements SetRequestBuilderAfterBuild
return test;
}
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()),
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(
retryWithInterval<Type>(),
catchError(error => {
if (!this.request.silenceMode)
this.handleError(error);
this.request = RequestBuilder.getStandardRequestData();
throw error;
})
);
}
public createRequestBuilder() {
this.request = RequestBuilder.getStandardRequestData();
return new RequestBuilder(this);
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 get<Type>(endpoint: string = ''): Observable<Type> {
if (endpoint)
this.request.endpoint = endpoint;
return this.makeHttpRequest<Type>('get');
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 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');
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;
})
);
}
private handleError(error: HttpErrorResponse): void {
// todo: change to Retry-After condition
if (error.error.toString().includes("setup")) {
this.router.navigate(['/setup/']).then();
this.router.navigate(['/setup/']);
return;
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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