parent
6f4e1476e2
commit
bb5b4c0c2a
@ -1,51 +1,48 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Work } from '../models/work.model';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Observable } from 'rxjs';
|
||||
import { map, Observable } from 'rxjs';
|
||||
import { NgForm } from '@angular/forms';
|
||||
import { environment } from '../../environments/environment';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class WorkService {
|
||||
API_URL = 'http://127.0.0.1:3000';
|
||||
|
||||
private works: Work[] = [];
|
||||
|
||||
constructor(private http: HttpClient) {}
|
||||
|
||||
getWorks(): Observable<Work[]> {
|
||||
return this.http.get<Work[]>(`${this.API_URL}/works`);
|
||||
getWorks(): Promise<Work[]> {
|
||||
return fetch(`${environment.apiUrl}/works`, {
|
||||
method: 'GET',
|
||||
credentials: 'include',
|
||||
}).then((response) => response.json());
|
||||
}
|
||||
|
||||
getWorkByLink(link: string): Observable<Work | null> {
|
||||
return this.http.get<Work>(`${this.API_URL}/works/${link}`);
|
||||
return this.http.get<Work>(`${environment.apiUrl}/works/${link}`);
|
||||
}
|
||||
|
||||
saveWork(form: NgForm): void {
|
||||
const code = form.value.content;
|
||||
|
||||
this.http.post(`${this.API_URL}/works/save`, code).subscribe();
|
||||
this.http.post(`${environment.apiUrl}/works/save`, code, { withCredentials: true });
|
||||
}
|
||||
|
||||
postWork(code: string, language: string, id_user: number): string {
|
||||
postWork(code: string, language: string): Observable<string> {
|
||||
const body = {
|
||||
id_user, // tant que ça pas résolu -> je peux pas faire le share
|
||||
link: crypto.randomUUID(),
|
||||
language: language,
|
||||
language,
|
||||
title: `Basic ${language}`,
|
||||
code: code,
|
||||
code,
|
||||
};
|
||||
|
||||
this.http.post(`${this.API_URL}/works`, body).subscribe();
|
||||
return body.link;
|
||||
return this.http.post<Work>(`${environment.apiUrl}/works`, body).pipe(map((work) => work.link));
|
||||
}
|
||||
|
||||
updateWork(id: string, code: string, language: string): void {
|
||||
let body = {
|
||||
const body = {
|
||||
newContent: code,
|
||||
language: language,
|
||||
};
|
||||
this.http.put<any>(`${this.API_URL}/works/${id}/content`, body).subscribe();
|
||||
this.http.put(`${environment.apiUrl}/works/${id}/content`, body, { withCredentials: true });
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue