import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

export class ApiService<Model> {
  constructor(http: HttpClient, baseUrl: string) {
    this.http = http;
    this.baseUrl = baseUrl;
  }

  http: HttpClient;
  baseUrl: string;

  getAll(): Observable<Model[]> {
    return this.http.get<Model[]>(this.baseUrl, { withCredentials: true });
  }

  getById(id: number): Observable<Model> {
    const url = `${this.baseUrl}/${id}`;
    return this.http.get<Model>(url, { withCredentials: true });
  }

  create(createModel: Model): Observable<Model> {
    return this.http.post<Model>(this.baseUrl, createModel, { withCredentials: true });
  }

  update(updateModel: Model): Observable<Model> {
    const url = `${this.baseUrl}`;
    return this.http.put<Model>(url, updateModel, { withCredentials: true });
  }

  delete(id: number): Observable<any> {
    const url = `${this.baseUrl}/${id}`;
    return this.http.delete<any>(url, { withCredentials: true });
  }
}