101 lines
2.9 KiB
TypeScript
101 lines
2.9 KiB
TypeScript
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<any> {
|
|
return this.http.post<UploadImageResponse>(`${this.baseUrl}/user-images/upload-base64-image`, base64Image, { headers: { "Content-Type": "application/json" } }
|
|
);
|
|
}
|
|
|
|
upload(file: File): Observable<HttpEvent<any>> {
|
|
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<any> {
|
|
return this.http.post(`${this.baseUrl}/saveZhipuAiImage`, {imageUrl:url});
|
|
}
|
|
|
|
getImages(): Observable<FileInfo[]> {
|
|
return this.http.get<FileInfo[]>(`${this.baseUrl}/getImages`);
|
|
}
|
|
|
|
|
|
loadImageAsBase64(imageUrl: string): Observable<string> {
|
|
return this.http.post<Base64ImageResponse>(`${this.baseUrl}/user-images/base64-image`, imageUrl)
|
|
.pipe(map(r => `data:${r.mime};base64,${(r.data || '').trim()}`));
|
|
}
|
|
|
|
getUserAllTutorials(): Observable<Tutorial[]> {
|
|
return this.http.get<Tutorial[]>(`${this.baseUrl}/tutorials`);
|
|
}
|
|
|
|
getTutorial(id: string | null | undefined): Observable<Tutorial> {
|
|
return this.http.get<Tutorial>(`${this.baseUrl}/tutorial-get/${id}`);
|
|
}
|
|
|
|
create(data: any): Observable<any> {
|
|
return this.http.post(`${this.baseUrl}/tutorial-add`, data);
|
|
}
|
|
|
|
update(id: any,data: any): Observable<any> {
|
|
return this.http.put(`${this.baseUrl}/tutorial-update/${id}`, data);
|
|
}
|
|
|
|
deleteTutorials(tutorials: Tutorial[]): Observable<Tutorial[]> {
|
|
return forkJoin(
|
|
tutorials.map((tutorial) =>
|
|
this.http.delete<Tutorial>(`${this.baseUrl}/tutorials/${tutorial.id}`)
|
|
)
|
|
);
|
|
}
|
|
|
|
deleteTutorial(id: number): Observable<Tutorial> {
|
|
return this.http.delete<Tutorial>(`${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
|
|
}
|
|
|