import { Injectable } from '@angular/core'; import {forkJoin, Observable, switchMap} from 'rxjs'; import {Tutorial} from '../../models/tutorial.model'; import {HttpClient, HttpEvent, HttpHeaders, HttpRequest} from '@angular/common/http'; import {GlobalConstants} from '../../global-constants'; import {FileInfo} from '../../models/file-info'; import {map} from 'rxjs/operators'; @Injectable({ providedIn: 'root' }) export class UserApiService { baseUrl = `${GlobalConstants.API_URL}/user`; constructor(private http: HttpClient) { } uploadUserBase64Image(base64Image: Base64ImagePayload): Observable { return this.http.post(`${this.baseUrl}/user-images/upload-base64-image`, base64Image, { headers: { "Content-Type": "application/json" } } ); } upload(file: File): Observable> { const formData: FormData = new FormData(); formData.append('file', file); const req = new HttpRequest('POST', `${this.baseUrl}/upload`, formData, { reportProgress: true, responseType: 'json' }); return this.http.request(req); } saveZhipuaiImage(url: string): Observable { return this.http.post(`${this.baseUrl}/saveZhipuAiImage`, {imageUrl:url}); } getImages(): Observable { return this.http.get(`${this.baseUrl}/getImages`); } loadImageAsBase64(imageUrl: string): Observable { return this.http.post(`${this.baseUrl}/user-images/base64-image`, imageUrl) .pipe(map(r => `data:${r.mime};base64,${(r.data || '').trim()}`)); } getUserAllTutorials(): Observable { return this.http.get(`${this.baseUrl}/tutorials`); } getTutorial(id: string | null | undefined): Observable { return this.http.get(`${this.baseUrl}/tutorial-get/${id}`); } create(data: any): Observable { return this.http.post(`${this.baseUrl}/tutorial-add`, data); } update(id: any,data: any): Observable { return this.http.put(`${this.baseUrl}/tutorial-update/${id}`, data); } deleteTutorials(tutorials: Tutorial[]): Observable { return forkJoin( tutorials.map((tutorial) => this.http.delete(`${this.baseUrl}/tutorials/${tutorial.id}`) ) ); } deleteTutorial(id: number): Observable { return this.http.delete(`${this.baseUrl}/tutorials/${id}`); } } export interface Base64ImagePayload { mime: string; // e.g., "image/png" data: string; // base64 string (no "data:" prefix) } export interface UploadImageResponse { message: string; fileName: string; url: string; // URL to access the stored image mime: string; size: number; // bytes } export interface Base64ImageResponse { message: string; mime: string; data: string; // Base64 without "data:" prefix }