FarmMapsLib/projects/common/src/fm/services/folder.service.ts

55 lines
2.3 KiB
TypeScript
Raw Normal View History

2019-07-15 14:54:19 +00:00
import { Injectable } from '@angular/core';
import { Observable , Observer } from 'rxjs';
import {map} from 'rxjs/operators';
import { IListItem } from '../models/list.item';
import { IItem } from '../models/item';
import { HttpClient } from "@angular/common/http";
import { AppConfig } from "../shared/app.config";
@Injectable()
export class FolderService {
2019-07-18 16:59:42 +00:00
constructor(public httpClient: HttpClient, public appConfig: AppConfig) {
}
ApiEndpoint() {
return this.appConfig.getConfig("apiEndPoint");
2019-07-15 14:54:19 +00:00
}
parseDates(item: any): IListItem {
item.created = new Date(Date.parse(item.created));
item.updated = new Date(Date.parse(item.updated));
item.dataDate = new Date(Date.parse(item.dataDate));
return item;
}
getFolder(code: string): Observable<IListItem> {
2019-07-18 16:59:42 +00:00
return this.httpClient.get<IListItem>(`${this.ApiEndpoint()}/api/v1/folders/${code}`).pipe(map(i => this.parseDates(i)));
2019-07-15 14:54:19 +00:00
}
getMyRoots(): Observable<IListItem[]> {
2019-07-18 16:59:42 +00:00
return this.httpClient.get<IListItem[]>(`${this.ApiEndpoint()}/api/v1/folders/my_roots`).pipe(map(ia => ia.map(i => this.parseDates(i))));
2019-07-15 14:54:19 +00:00
}
getFolderParents(code: string): Observable<IListItem[]> {
2019-07-18 16:59:42 +00:00
return this.httpClient.get<IListItem[]>(`${this.ApiEndpoint()}/api/v1/folders/${code}/parents`).pipe(map(ia => ia.map(i => this.parseDates(i))));
2019-07-15 14:54:19 +00:00
}
getChildFolders(code: string): Observable<IListItem[]> {
2019-07-18 16:59:42 +00:00
return this.httpClient.get<IListItem[]>(`${this.ApiEndpoint()}/api/v1/folders/${code}/listfolders`).pipe(map(ia => ia.map(i => this.parseDates(i))));
2019-07-15 14:54:19 +00:00
}
getItems(code: string,skip:number, take:number): Observable<IListItem[]> {
2019-07-18 16:59:42 +00:00
return this.httpClient.get<IListItem[]>(`${this.ApiEndpoint()}/api/v1/folders/${code}/list?skip=${skip}&take=${take}`).pipe(map(ia => ia.map(i => this.parseDates(i))));
2019-07-15 14:54:19 +00:00
}
moveItem(itemCode: string, newParentCode: string): Observable<IListItem> {
const body = { itemCode: itemCode,newParentCode: newParentCode };
2019-07-18 16:59:42 +00:00
return this.httpClient.post<IListItem>(`${this.ApiEndpoint()}/api/v1/items/move`, body).pipe(map(i => this.parseDates(i)));
2019-07-15 14:54:19 +00:00
}
createFolder(folder: IItem): Observable<IListItem> {
2019-07-18 16:59:42 +00:00
return this.httpClient.post<IListItem>(`${this.ApiEndpoint()}/api/v1/folders/`, folder).pipe(map(i => this.parseDates(i)));
2019-07-15 14:54:19 +00:00
}
}