adp-look-up-service-test v0.0.6
ADP Lookup Service
Overview
The ADP Lookup Service is a module designed for handling lookup data related to vehicle license services. It provides functionality to fetch and manage various lookup data sets like external sources, local sources, plate colors, plate kinds, and plate sources. The service interacts with the backend API to retrieve data and stores it in session storage for efficient and optimized performance.
Features
- Fetches lookup data such as:
- External Ticket Sources (TIC_EXT_SOURCES)
- Local Ticket Sources (TRF_EMIRATES)
- Vehicle Plate Sources (VEH_PLATE_SOURCES)
- Vehicle Plate Colors (VEH_PLATE_COLORS)
- Vehicle Plate Kinds (VEH_PLATE_KINDS)
- Netionalities (TRF_NATIONALITIES)
- Caches the lookup data in session storage for performance optimization.
- Allows for refreshing of cached data when needed.
- Provides observables for real-time updates to components consuming the lookup data.
Installation
To use this service in your Angular project, follow these steps:
- Clone the repository or copy the service files into your Angular project's
src/app
directory. - Make sure you have the necessary dependencies installed:
rxjs
(forBehaviorSubject
and observables)@angular/core
- Import and configure the
ADPLookUpService
in your Angular module.
import { ADPLookUpService } from './services/look-up-service';
@NgModule({
declarations: [...],
imports: [...],
providers: [ADPLookUpService],
bootstrap: [...]
})
export class AppModule {}
Usage
Injecting the Service
To use the lookup service in your components or services, inject the ADPLookUpService
in the constructor.
import { Component, OnInit } from '@angular/core';
import { ADPLookUpService } from './services/look-up-service';
import { LookupData } from './api-models/get-look-up-data.model';
@Component({
selector: 'app-lookup-component',
templateUrl: './lookup-component.component.html',
styleUrls: ['./lookup-component.component.css']
})
export class LookupComponent implements OnInit {
extSources: LookupData[] = [];
constructor(private lookupService: ADPLookUpService) {}
ngOnInit(): void {
// Example of subscribing to get external sources data
this.lookupService.GetExtSourcesArrayFromLookUpData().subscribe((data) => {
this.extSources = data;
});
}
}
Setting the Base URL
Before making requests to the backend API, ensure you set the base URL for the lookup service.
this.lookupService.setBaseUrl('https://your-api-url.com');
Refreshing and Fetching Data
You can refresh the lookup data by passing hardRefresh: true
to force fetching new data from the API.
this.lookupService.GetSourcesArrayFromLookUpData(true).subscribe((data) => {
console.log(data); // Handle new lookup data
});
Updating All Lookup Data
To update all lookup data and store them in session storage, use the UpdateAllLookupDataInSession
method:
this.lookupService.UpdateAllLookupDataInSession(true); // Pass `true` for a hard refresh
Methods
setBaseUrl(baseUrl: string): void
Sets the base URL for API requests.
- Parameters:
baseUrl: string
: The base URL for the API.
GetExtTicSourcesArrayFromLookUpData(hardRefresh: boolean = false): Observable<LookupData[]>
Fetches the external ticket sources lookup data.
- Parameters:
hardRefresh: boolean
: Iftrue
, forces a refresh and fetches data from the API.- Returns: Observable containing the lookup data.
GetTicSourcesArrayFromLookUpData(hardRefresh: boolean = false): Observable<LookupData[]>
Fetches the local ticket sources lookup data.
- Parameters:
hardRefresh: boolean
: Iftrue
, forces a refresh and fetches data from the API.- Returns: Observable containing the lookup data.
GetPlateSourcesArrayFromLookUpData(hardRefresh: boolean = false): Observable<LookupData[]>
Fetches the sources lookup data.
- Parameters:
hardRefresh: boolean
: Iftrue
, forces a refresh and fetches data from the API.- Returns: Observable containing the lookup data.
GetPlateColorsArrayFromLookUpData(hardRefresh: boolean = false): Observable<LookupData[]>
Fetches the vehicle plate colors lookup data.
- Parameters:
hardRefresh: boolean
: Iftrue
, forces a refresh and fetches data from the API.- Returns: Observable containing the lookup data.
GetPlateKindsArrayFromLookUpData(hardRefresh: boolean = false): Observable<LookupData[]>
Fetches the vehicle plate kinds lookup data.
- Parameters:
hardRefresh: boolean
: Iftrue
, forces a refresh and fetches data from the API.- Returns: Observable containing the lookup data.
GetNationalityArrayFromLookUpData(hardRefresh: boolean = false): Observable<LookupData[]>
Fetches the netionality lookup data.
- Parameters:
hardRefresh: boolean
: Iftrue
, forces a refresh and fetches data from the API.- Returns: Observable containing the lookup data.
UpdateAllLookupDataInSession(hardRefresh: boolean = false): void
Updates all lookup data and stores it in session storage.
- Parameters:
hardRefresh: boolean
: Iftrue
, forces a refresh and fetches data from the API.
getLookupData(key: LookUpKeyEnum, hardRefresh: boolean = false): Observable<LookupData[]>
Fetches data for a specific lookup key.
- Parameters:
key: LookUpKeyEnum
: The specific lookup key.hardRefresh: boolean
: Iftrue
, forces a refresh.- Returns: Observable containing the lookup data.
Session Storage
The lookup data for each key is stored in session storage to reduce the number of API calls. The following keys are used for storing data:
ADP_Lookup_Ext_Tic_Source
: External Ticket SourcesADP_Lookup_Tic_Source
: Local Ticket SourcesADP_Lookup_Plate_Source
: Plate SourcesADP_Lookup_Plate_Color
: Plate ColorsADP_Lookup_Plate_Kind
: Plate KindsADP_Nationality
: Nationality
These keys correspond to the LookUpKeyEnum
values.
Error Handling
If an error occurs during the API call, it is logged to the console. Ensure that the base URL is correctly set before using the service.
Conclusion
This service provides a lightweight, efficient way to manage lookup data in your Angular applications. It optimizes performance through session storage caching and offers an easy-to-use API for fetching and managing data.