Created
May 13, 2020 08:49
-
-
Save afitzek/e3a8a67b43e0d21832a78be864f082c5 to your computer and use it in GitHub Desktop.
DataServiceExample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const BASE_API_URL = 'https://api.host.com'; | |
interface Observable<T> { | |
} | |
interface TableViewContainer { | |
} | |
interface PageQueryParams { | |
} | |
interface BaseModel { | |
id: string; | |
} | |
interface Configuration extends BaseModel { | |
name?: string; | |
} | |
interface Task extends BaseModel { | |
} | |
interface Result extends BaseModel { | |
} | |
interface IDataService<T extends BaseModel> { | |
getTableRows (queryParams?: PageQueryParams): Observable<TableViewContainer>; | |
deleteEntry (entry: T): Observable<T>; | |
updateEntry (entry: T): Observable<T>; | |
} | |
class DataService<T extends BaseModel> implements IDataService<T> { | |
protected standardUrl: string; | |
constructor(standardUrl: string) { | |
this.standardUrl = standardUrl; | |
} | |
getDeleteURL(entry: T):string { | |
return `${this.standardUrl}/${entry.id}`; | |
} | |
getUpdateURL(entry: T):string { | |
return `${this.standardUrl}/${entry.id}`; | |
} | |
getListURL():string { | |
return `${this.standardUrl}`; | |
} | |
transformIntoTableViewContainer(httpResponse: any): Observable<TableViewContainer> { | |
console.log(`Running the default converstion of http response into TableViewContainer`) | |
return httpResponse as Observable<TableViewContainer>; | |
} | |
getTableRows (queryParams?: PageQueryParams): Observable<TableViewContainer> { | |
console.log(`Perfoming an HTTP request to get table rows: ${this.getListURL()}`); | |
return this.transformIntoTableViewContainer([ | |
{ | |
id: 1 | |
}, | |
{ | |
id: 2 | |
} | |
]); | |
} | |
deleteEntry (entry: T): Observable<T> { | |
console.log(`Perfoming an HTTP request to delete entry: ${this.getDeleteURL(entry)}`); | |
return {}; | |
} | |
updateEntry (entry: T): Observable<T> { | |
console.log(`Perfoming an HTTP request to update entry: ${this.getUpdateURL(entry)}`); | |
return {}; | |
} | |
} | |
class NoServerFilteringDataService<T extends BaseModel> extends DataService<T> { | |
constructor(standardUrl: string) { | |
super(standardUrl); | |
} | |
transformIntoTableViewContainer(httpResponse: any): Observable<TableViewContainer> { | |
console.log(`Creating TableViewContainer for non server filtering`) | |
return httpResponse as Observable<TableViewContainer>; | |
} | |
} | |
class ConfigurationDataService extends DataService<Configuration> { | |
constructor() { | |
super(`${BASE_API_URL}/configurations`); | |
} | |
} | |
class ResultDataService extends NoServerFilteringDataService<Result> { | |
constructor() { | |
super(`${BASE_API_URL}/result`); | |
} | |
} | |
class TaskDataService extends DataService<Task> { | |
protected context?: Configuration; | |
constructor() { | |
super(`${BASE_API_URL}/tasks`); | |
} | |
setContext(context: Configuration) { | |
this.context = context; | |
} | |
transformIntoTableViewContainer(httpResponse: any): Observable<TableViewContainer> { | |
console.log(`Tasks http response is converted into TableViewContainer`) | |
return httpResponse as Observable<TableViewContainer>; | |
} | |
getListURL():string { | |
return `${this.standardUrl}/configuration/${this.context!.id}/tasks`; | |
} | |
getUpdateURL(entry: Task):string { | |
return `${this.standardUrl}`; | |
} | |
} | |
class ClientDataService<T extends BaseModel> implements IDataService<T> { | |
protected dataService: IDataService<T>; | |
private cache?: Observable<TableViewContainer>; | |
constructor(dataService: IDataService<T>) { | |
this.dataService = dataService; | |
} | |
getTableRows (queryParams?: PageQueryParams): Observable<TableViewContainer> { | |
if(!this.cache) { | |
console.log(`Reading data rows from the server, without sorting, paging or filtering`); | |
this.cache = this.dataService.getTableRows(); | |
} | |
console.log(`Performing client paging, sorting filtering on cache`); | |
return this.cache; | |
} | |
deleteEntry (entry: T): Observable<T> { | |
return this.dataService.updateEntry(entry); | |
} | |
updateEntry (entry: T): Observable<T> { | |
return this.dataService.updateEntry(entry); | |
} | |
} | |
const taskService = new TaskDataService(); | |
taskService.setContext( | |
{ | |
id: 'CONFIGURATION_ID' | |
} | |
); | |
taskService.getTableRows(); | |
taskService.updateEntry({ | |
id: 'TASK_ID' | |
}); | |
const taskClientService = new ClientDataService(taskService); | |
taskClientService.getTableRows(); | |
taskClientService.getTableRows(); | |
taskClientService.getTableRows(); | |
const configurationService = new ConfigurationDataService(); | |
configurationService.getTableRows(); | |
configurationService.deleteEntry({ | |
id: 'CONFIGURATION_ID_2' | |
}); | |
configurationService.updateEntry({ | |
id: 'CONFIGURATION_ID_2' | |
}); | |
const configurationClientService = new ClientDataService(configurationService); | |
configurationClientService.getTableRows(); | |
configurationClientService.getTableRows(); | |
configurationClientService.getTableRows(); | |
const resultService = new ResultDataService(); | |
resultService.getTableRows(); | |
resultService.updateEntry({ | |
id: 'RESULT_ID' | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment