import { Injectable, inject } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable, Subscription } from 'rxjs'; import { signal } from '@angular/core'; import { Filament } from '../models/filament.model'; /** * API base URL — matches the Extrudex backend. * TODO: Move to environment config when environments are set up. */ const API_BASE_URL = '/api/filaments'; /** * Service for managing filament inventory data. * * Provides: * - A reactive `filaments` signal for components to bind to * - REST methods for GET, POST, DELETE endpoints * - Real-time updates via SignalR should be layered on top when the hub is ready */ @Injectable({ providedIn: 'root' }) export class FilamentService { private readonly http = inject(HttpClient); /** Reactive filament data — components read from this signal */ readonly filaments = signal([]); /** Fetch all filament spools and update the signal */ getFilaments(): Observable { const req = this.http.get(API_BASE_URL); req.subscribe({ next: (data) => this.filaments.set(data), error: (err) => console.error('Failed to load filaments:', err), }); return req; } /** Fetch a single filament by ID */ getFilament(id: string): Observable { return this.http.get(`${API_BASE_URL}/${id}`); } /** Set filament data directly — used by components or SignalR handlers */ setFilaments(data: Filament[]): void { this.filaments.set(data); } /** Delete a filament spool by ID */ deleteFilament(id: string): Observable { return this.http.delete(`${API_BASE_URL}/${id}`); } }