Skip to content
Snippets Groups Projects
machines.service.ts 3.39 KiB
Newer Older
import { Injectable } from '@angular/core';
import { Machine } from './machine';
Your Name's avatar
Your Name committed
import { AuthService } from './auth.service';
import { environment } from '../environments/environment';
import { catchError, map, tap } from 'rxjs/operators';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import { HttpClient, HttpHeaders } from '@angular/common/http';
Your Name's avatar
Your Name committed
import { FormBuilder, FormGroup } from '@angular/forms';


@Injectable()
export class MachinesService {

	private machinesURL = environment.webserviceUrl+"/api/machines";
Your Name's avatar
Your Name committed
	httpOptions = { headers: new HttpHeaders({	}) };
	private updateHttpHeader() {
Your Name's avatar
Your Name committed
		this.httpOptions.headers = 
			this.httpOptions.headers.set('Authorization', this.authService.getToken());
Your Name's avatar
Your Name committed
	}

Your Name's avatar
Your Name committed
	private translateToServer(machine: Machine, op) {
Your Name's avatar
Your Name committed
		
Your Name's avatar
Your Name committed
		const formData = new FormData()
	  formData.append('serial_number', machine.serial_number);
	  formData.append('room', machine.room);
	  formData.append('date_last_maintenance', 
						machine.date_last_maintenance.month + '-' +
						machine.date_last_maintenance.day + '-' +
						machine.date_last_maintenance.year
						);
	  formData.append('date_next_maintenance', 
						machine.date_next_maintenance.month + '-' +
						machine.date_next_maintenance.day + '-' +
						machine.date_next_maintenance.year
						);
Your Name's avatar
Your Name committed
	  formData.append('status', machine.Status);

		formData.append('address', machine.address);
	  formData.append('address_number', machine.address_number);
	  formData.append('district', machine.district);
	  formData.append('city', machine.city);
	  formData.append('state', machine.state);

Your Name's avatar
Your Name committed
		if ((op == "post") || (machine.image.size != 0 )) { //if is post or image exists
			console.log("sending image");
			formData.append('file', machine.image, machine.image.name);
		}
Your Name's avatar
Your Name committed

		return formData;
Your Name's avatar
Your Name committed
	}
Your Name's avatar
Your Name committed
	getMachines(): Observable<Object> {
		return this.http.get(this.machinesURL)
			.pipe(
				tap(_ => console.log('fetched machines')),
				catchError(this.handleError<Observable<Object>>("getMachines")));
Your Name's avatar
Your Name committed
	}

Your Name's avatar
Your Name committed
	addMachine(machine: Machine): Observable<Machine> {
	
		if (this.authService.loggedIn()) { this.updateHttpHeader() }
Your Name's avatar
Your Name committed

Your Name's avatar
Your Name committed
		return this.http.post<any>(this.machinesURL,
Your Name's avatar
Your Name committed
																	this.translateToServer(machine, "post"), 
Your Name's avatar
Your Name committed
			tap(_ => console.log("added machine with serial number "+ machine.serial_number)),
			catchError(this.handleError<Machine>("addMachine"))
		);
	}

Your Name's avatar
Your Name committed
	updateMachine(machine: Machine): Observable<any> {
Your Name's avatar
Your Name committed

		if (this.authService.loggedIn()) { this.updateHttpHeader() }

		return this.http.put(this.machinesURL+"/"+machine.id,
Your Name's avatar
Your Name committed
													this.translateToServer(machine, "put"),
Your Name's avatar
Your Name committed
													this.httpOptions).pipe(
			tap(_ => console.log("updated machine with serial number " + machine.serial_number)),
Your Name's avatar
Your Name committed
			catchError(this.handleError<Machine>("updateMachine"))
		);
	}

	deleteMachine(id) {

		if (this.authService.loggedIn()) { this.updateHttpHeader() }
		
		return this.http.delete(this.machinesURL+"/"+id,
														this.httpOptions).pipe(
			tap(_ => console.log("removed machine with serial number " + id)),
			catchError(this.handleError<Machine>("removeMachine"))
		);

	}


	private handleError<T> (operation = 'operation', result?: T) {
		return (error: any): Observable<T> => {
			console.error(error);

			return Observable.of(result as T);
		}
Your Name's avatar
Your Name committed
  constructor(private http: HttpClient,
							private authService: AuthService) { }