-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fcf1d45
commit b11994e
Showing
10 changed files
with
111 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,49 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import { HttpClient, HttpHeaders } from '@angular/common/http'; | ||
import { Observable, of } from 'rxjs'; | ||
import { Record } from './record'; | ||
//import { writeFileSync, readFileSync } from 'fs'; | ||
//import fs from 'fs'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class RecordService { | ||
|
||
private recordUrl = 'https://recordantho.mysites.fr:3443/record'; // URL to web api | ||
|
||
constructor(private http: HttpClient) { } | ||
httpOptions = { | ||
headers: new HttpHeaders({ 'Content-Type': 'application/json' }) | ||
}; | ||
|
||
// Récupération de la liste des enregistrements | ||
getRecords():Promise <Array<Record>> { | ||
return new Promise((resolve, reject)=> | ||
{ | ||
this.http.get<Array<Record>>('/assets/records.json').subscribe(resultat=>{ resolve(resultat) }); | ||
this.http.get<Array<Record>>(`${this.recordUrl}/list`).subscribe(resultat=>{ resolve(resultat) }); | ||
}); | ||
} | ||
|
||
updateJson(record: Record) { | ||
this.getRecords().then(allRecords=>{ | ||
allRecords[allRecords.findIndex(element=>{ | ||
return element.id == record.id | ||
})] = record | ||
}) | ||
//fs.writeFile('/assets/records.json', allRecords) | ||
|
||
// Création d'un enregistrement | ||
createRecord(record: Record): Observable<any> { | ||
return this.http.post(`${this.recordUrl}/addrecord`, record, this.httpOptions) | ||
} | ||
|
||
// Update d'un enregistrement | ||
updateRecord(record: Record): Observable<any> { | ||
return this.http.put(`${this.recordUrl}/update`, record, this.httpOptions) | ||
} | ||
|
||
// Suppression d'une chaîne | ||
deleteRecord(idRecord: Number): Observable<any> { | ||
return this.http.delete(`${this.recordUrl}/delete/${idRecord}`, this.httpOptions) | ||
} | ||
|
||
// méthode genId pour générer un id unique et incrémental lors de la création d'un enregistrement | ||
// Si le tableau est vide, | ||
// la méthode génère (1). | ||
// Si le tableau n'est pas vide, la méthode génère l'idmax trouvé + 1 | ||
|
||
genRecordId(records: Record[]): number { | ||
return records.length > 0 ? Math.max(...records.map(record => record.id)) + 1 : 1; | ||
} | ||
} |