UNPKG

tfabrica

Version:

library for TFabrica - TechSol

59 lines (50 loc) 2.1 kB
import { Injectable } from '@angular/core'; import { Http, Headers, Response, RequestOptions } from "@angular/http"; import 'rxjs/Rx'; import { Observable } from "rxjs"; import { TfabricaUserData } from '../models/tfabrica.userdata.model'; import { TfabricaLog } from '../models/tfabrica.log.model'; import { TfabricaSharedService } from '../main/tfabrica.shared.service'; @Injectable() export class TfabricaLogService { constructor( private _http: Http, private _shared: TfabricaSharedService ) { } public AddLog(log: TfabricaLog) { this.AddLogService(log) .subscribe(result => { console.log(result); }, err => { console.log(err); }); } public AddLogService(log: TfabricaLog): Observable<boolean> { let that = this; let urlToCall = this._shared.appSettings.getlogApiUrl(); console.log("Start call service: " + urlToCall); let username = ""; try { username = this._shared.getUser().username; } catch (Err) { } let logComplete = { user: username, operation: log.operation, description: log.description, message: log.message, isError: !log.success, app: this._shared.appSettings.appName }; let bodyString = JSON.stringify(logComplete); let headers = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON let options = new RequestOptions({ headers: headers }); // Create a request option return this._http.post(urlToCall, bodyString, options) .map((res: Response) => { let response = res.json(); console.log(response); return response; }) // ...and calling .json() on the response to return data .catch((error: any) => Observable.throw(error.json().error || 'Server error')); //...errors if any } }