import {Component, OnInit} from '@angular/core'; import {of} from 'rxjs'; import {IDataSource, IQueryCriteria} from '@poweredsoft/data'; import {HttpDataSourceService} from '@openharbor/ngx-data'; export interface IContact { id: number; displayName: string; } export interface IPerson { id: number; firstName: string; lastName: string; } export interface ICreatePerson { firstName: string; lastName: string; } export interface IEchoCommand { message: string; } export interface IMyQuery extends IQueryCriteria{ params: { showDisabled: boolean; } } export interface IOnePersonQuery { personId: number; } export interface IListPersonQuery { search?: string; } @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'] }) export class AppComponent implements OnInit { title = 'ngx-data'; dataSource: IDataSource; latestData: any; onePersonDs: IDataSource; listPersonDs: IDataSource; latestSingle: any; latestList: any; constructor(private hdss: HttpDataSourceService) { this.onePersonDs = hdss.singleBuilder() .keyResolver(t => t.id) .queryUrl('https://localhost:5001/api/query/onePerson') .beforeRead(_ => { return of({ personId: 1 }) }) .createDataSource(); this.listPersonDs = hdss.listBuilder() .keyResolver(t => t.id) .queryUrl('https://localhost:5001/api/query/listPerson') .beforeRead(criteria => { return of({ search: "Doe" }) }) .createDataSource(); const ds = hdss .builder() .keyResolver(m => m.id) .defaultCriteria({ page: 1, pageSize: 5 }) .beforeRead(q => { q.params = { showDisabled: true }; return of(q); }) .queryUrl('https://localhost:5001/api/query/contacts') .addCommandByUrl("createPerson", 'https://localhost:5001/api/command/createPerson', e => { return of ({ firstName: '', lastName: '' }) }) .addCommandByUrl('echo', 'https://localhost:5001/api/command/echo') .createDataSource(); this.dataSource = ds; } ngOnInit(): void { this.dataSource.data$.subscribe(newData => { this.latestData = newData; }); this.dataSource.notifyMessage$.subscribe(message => { if (message.type == 'error') alert(message.message); }); this.onePersonDs.data$.subscribe(onePerson => { this.latestSingle = onePerson; }) this.listPersonDs.data$.subscribe(t => { this.latestList = t; }) } refresh() { this.dataSource.refresh(); this.onePersonDs.refresh(); this.listPersonDs.refresh(); } echoCommand() { const message = prompt('What message you wish to echo? '); this.dataSource.executeCommandByName('echo', { message: message }).subscribe( commandResult => alert(commandResult), err => console.log(err) ); } }