You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sandkasten-web/src/app/services/work.service.ts

52 lines
1.3 KiB

import { Injectable } from '@angular/core';
import { Work } from '../models/work.model';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { NgForm } from '@angular/forms';
@Injectable({
providedIn: 'root',
})
export class WorkService {
API_URL = 'http://127.0.0.1:3000';
private works: Work[] = [];
constructor(private http: HttpClient) {}
getWorks(): Observable<any> {
return this.http.get(`${this.API_URL}/works`);
}
getWorkByLink(link: string): Observable<any> {
return this.http.get(`${this.API_URL}/works/${link}`);
}
saveWork(form: NgForm): void {
const code = form.value.content;
this.http.post<any>(`${this.API_URL}/works/save`, code).subscribe();
}
postWork(code: string, language: string, id_user: number): string {
let body = {
id_user: id_user, // tant que ça pas résolu -> je peux pas faire le share
link: crypto.randomUUID(),
language: language,
title: `Basic ${language}`,
code: code,
};
this.http.post<any>(`${this.API_URL}/works`, body).subscribe();
return body.link;
}
updateWork(id: string, code: string, language: string): void {
let body = {
newContent: code,
language: language,
};
this.http.put<any>(`${this.API_URL}/works/${id}/content`, body).subscribe();
}
}