import {Injectable} from '@angular/core';
import {ApiService} from "../../shared/service/api.service";
import {Observable} from "rxjs";
import {MaterialType} from "../../shared/model/materialtype.model";

@Injectable({
  providedIn: 'root'
})
export class MaterialTypeService {
  constructor(
    private api: ApiService
  ) {
  }

  get all(): Observable<MaterialType[]> {
    return this.api.get<MaterialType[]>('/materialtype')
  }

  get(id: number): Observable<MaterialType> {
    return this.api.get<MaterialType>(`/materialtype/${id}`)
  }

  save(name: string, prefix: string, usePercentages: boolean): Observable<MaterialType> {
    const materialType = {name, prefix, usePercentages}
    return this.api.post<MaterialType>('/materialtype', materialType)
  }

  update(id: number, name: string, prefix: string): Observable<void> {
    const materialType = {id, name, prefix}
    return this.api.put<void>('/materialtype', materialType)
  }

  delete(id: number): Observable<void> {
    return this.api.delete<void>(`/materialtype/${id}`)
  }
}
