import { Injectable, Inject } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { AppConstants } from './helpers/constants/app.constants'; import { IAppConstants } from './helpers/constants/app-constants.interface'; import { Observable } from 'rxjs'; import { SearchFilterModel } from './models/search-filter.model'; import { UrlSearchFilter } from './models/url-search-filter.model'; import { GlobalDataService } from './services/global-data.service'; @Injectable({ providedIn: 'root' }) export class ArticleDiscoveryService { private baseUrl: string; // #region Constructor constructor( private http: HttpClient, private globalData: GlobalDataService, @Inject(AppConstants) private constants: IAppConstants ) { } // #endregion Constructor // #region Private Methods private get articleSearchApiUrl(): string { return `${this.globalData.BaseUrl}/${this.constants.ARTICLES_API_ROUTE_PREFIX}${this.constants.SEARCH_URL}`; } private get settingsUrl(): string { return `${this.globalData.BaseUrl}/${this.constants.ARTICLES_API_ROUTE_PREFIX}${this.constants.SETTINGS_URL}`; } private get articleFilterApiUrl(): string { return `${this.globalData.BaseUrl}/${this.constants.ARTICLES_API_ROUTE_PREFIX}${this.constants.ARTICLE_FILTER_URL}`; } private get articleMetadataUrl(): string { return `${this.globalData.BaseUrl}/${this.constants.ARTICLES_API_ROUTE_PREFIX}${this.constants.ARTICLE_METADATA_URL}`; } private get articleSchemaUrl(): string { return `${this.globalData.BaseUrl}/${this.constants.ARTICLES_API_ROUTE_PREFIX}${this.constants.ARTICLE_SCHEMA_URL}`; } private articleSubjectsUrl(slug: string): string { return `${this.globalData.BaseUrl}/${this.constants.ARTICLES_API_ROUTE_PREFIX}/${this.constants.SUBJECTS_ROUTE_PREFIX}/${slug}`; } // #endregion Private Methods // #region Protected Methods protected getRequestHeaders(): { headers: HttpHeaders | { [header: string]: string | string[] }; } { const headers = { headers: new HttpHeaders({ 'Content-Type': 'application/json', Accept: 'application/json', 'Cache-Control': 'no-cache', Pragma: 'no-cache' }), observe: 'response' }; return headers; } // #endregion // #region Public Methods public getPageSettings(): Observable { return this.http.get(this.settingsUrl, this.getRequestHeaders()); } public getSearch(filter: SearchFilterModel): Observable { return this.http.post( this.articleSearchApiUrl, filter, this.getRequestHeaders() ); } public getPageFilters(filter: UrlSearchFilter): Observable { return this.http.post( this.articleFilterApiUrl, filter, this.getRequestHeaders() ); } public getMetadata(): Observable { return this.http.get( this.articleMetadataUrl, this.getRequestHeaders() ); } public getArticleSchema(filter: SearchFilterModel): Observable { return this.http.post( this.articleSchemaUrl, filter, this.getRequestHeaders() ); } public getArticleSubjects(slug: string): Observable { return this.http.get( this.articleSubjectsUrl(slug), this.getRequestHeaders() ); } // #endregion Public Methods }