All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src.app.core.services.dataset.service.ts Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { environment } from '@env/environment';
import { Dataset, KeyValue } from '@model';
import { HttpClient } from '@angular/common/http';

@Injectable({
    providedIn: 'root'
})
export class DataSetService {

    private resourceUrl = '/api/v1/datasets';

    constructor(private httpClient: HttpClient) { }

    findAll(): Observable> {
        return this.httpClient.get>(environment.backend + this.resourceUrl)
            .pipe(map((res: Array) => {
                res = res.map(dto => new Dataset(
                    dto.name,
                    dto.description,
                    dto.tags,
                    dto.lastUpdated,
                    [],
                    [],
                    dto.version,
                    dto.id
                ));

                return res;
            }));
    }

    findById(id: string): Observable {
        return this.httpClient.get(environment.backend + this.resourceUrl + '/' + id)
            .pipe(
                map(dto => this.fromDto(dto))
            );
    }

    save(dataset: Dataset): Observable {
        if (dataset.id && dataset.id.length > 0) {
            return this.httpClient.put(environment.backend + this.resourceUrl, dataset)
                .pipe(
                    map(dto => this.fromDto(dto))
                );
        } else {
            return this.httpClient.post(environment.backend + this.resourceUrl, dataset)
                .pipe(
                    map(dto => this.fromDto(dto))
                );
        }
    }

    delete(id: String): Observable {
        return this.httpClient.delete(environment.backend + this.resourceUrl + '/' + id);
    }

    private fromDto(dto: any): Dataset {
        return new Dataset(
            dto.name,
            dto.description,
            dto.tags,
            dto.lastUpdated,
            dto.uniqueValues.map(o => new KeyValue(o.key, o.value)),
            dto.multipleValues.map(l => l.map(o => new KeyValue(o.key, o.value))),
            dto.version,
            dto.id);
    }
}