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