This commit is contained in:
@@ -1,12 +0,0 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { ApiService } from './api.service';
|
||||
|
||||
describe('ApiService', () => {
|
||||
beforeEach(() => TestBed.configureTestingModule({}));
|
||||
|
||||
it('should be created', () => {
|
||||
const service: ApiService = TestBed.get(ApiService);
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -24,11 +24,11 @@ export class ApiService {
|
||||
|
||||
public request(endpoint, params = {}, method: 'get'|'post' = 'get'): Observable<ApiResponse> {
|
||||
return new Observable<ApiResponse>(sub => {
|
||||
const data: any = {}
|
||||
let data: any = {}
|
||||
if ( method === 'get' ) {
|
||||
data.params = params;
|
||||
} else {
|
||||
data.body = params;
|
||||
data = params;
|
||||
}
|
||||
|
||||
this.http[method](this._build_url(endpoint), data).subscribe({
|
||||
|
||||
74
src/app/service/page.service.ts
Normal file
74
src/app/service/page.service.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import {Host, Injectable} from '@angular/core';
|
||||
import {Observable} from 'rxjs';
|
||||
import PageRecord from '../structures/PageRecord';
|
||||
import {ApiService} from './api.service';
|
||||
import HostRecord from '../structures/HostRecord';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class PageService {
|
||||
|
||||
constructor(
|
||||
protected api: ApiService,
|
||||
) { }
|
||||
|
||||
load(id: string): Observable<PageRecord> {
|
||||
return new Observable<PageRecord>(sub => {
|
||||
this.api.get(`/page/${id}`).subscribe(response => {
|
||||
if ( response.status === 200 ) {
|
||||
sub.next(new PageRecord(response.data));
|
||||
sub.complete();
|
||||
} else {
|
||||
throw new Error(response.message);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
save(page: PageRecord): Observable<PageRecord> {
|
||||
return new Observable<PageRecord>(sub => {
|
||||
this.api.post(`/page/${page.UUID}/save`, page.toSave()).subscribe(res => {
|
||||
sub.next(new PageRecord(res.data));
|
||||
sub.complete();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
get_nodes(page: PageRecord): Observable<Array<HostRecord>> {
|
||||
return new Observable<Array<HostRecord>>(sub => {
|
||||
this.api.get(`/page/${page.UUID}/nodes`).subscribe(res => {
|
||||
const returns = [];
|
||||
res.data.forEach(rec => {
|
||||
const host = new HostRecord(rec.Value.Value);
|
||||
host.load(rec);
|
||||
returns.push(host);
|
||||
});
|
||||
|
||||
sub.next(returns);
|
||||
sub.complete();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
save_nodes(page: PageRecord, nodes: Array<HostRecord>): Observable<Array<HostRecord>> {
|
||||
return new Observable<Array<HostRecord>>(sub => {
|
||||
nodes = nodes.map(x => {
|
||||
x.PageId = page.UUID;
|
||||
return x.toSave();
|
||||
});
|
||||
|
||||
this.api.post(`/page/${page.UUID}/nodes/save`, nodes).subscribe(res => {
|
||||
const returns = [];
|
||||
res.data.forEach(rec => {
|
||||
const host = new HostRecord(rec.Value.Value);
|
||||
host.load(rec);
|
||||
returns.push(host);
|
||||
});
|
||||
|
||||
sub.next(returns);
|
||||
sub.complete();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user