Skip to content
Snippets Groups Projects
Commit 56394ab2 authored by Lafarge-Marc-Antoine's avatar Lafarge-Marc-Antoine
Browse files

Correction de bug majeur

Correction du Mock en Json, Suppression du logger, Netoyage de fichier. Etat : Fonctionnel pour afficher un character à partir de son id en base
parent 89f71c3b
Branches Inmemory
No related tags found
No related merge requests found
......@@ -5,17 +5,16 @@ export class Character {
mana: number;
max_life: number = 100;
max_mana: number = 100;
//static cpid: number = 0;
constructor(name:string = "Unknow", id:number = -1,mlife: number = 100, mmana : number = 100){
this.id = id //Character.cpid;
constructor(name:string = "Unknow", id:number = -1,max_life: number = 100, max_mana : number = 100){
this.id = id
this.life = 100;
this.mana = 100;
this.name = name
this.max_life = mlife;
this.max_mana = mmana;
///Character.cpid++;
this.max_life = max_life;
this.max_mana = max_mana;
}
/*
takeDamage(dmg:number){
this.life -= dmg;
if(this.life < 0)
......@@ -37,4 +36,5 @@ export class Character {
this.mana = 0;
}
}
*/
}
\ No newline at end of file
import { Character } from './Character';
export const Characters: Character[] =[
new Character("Harry Potter", 1),
new Character("Hermione Granger", 2),
new Character("Ron Weasley", 3),
new Character("Voldmort", 666),
new Character("Drago Malefoy",4)
];
\ No newline at end of file
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientInMemoryWebApiModule, InMemoryDbService } from 'angular-in-memory-web-api';
import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api';
import { HttpClientModule } from '@angular/common/http';
import {InMemoryDataService} from './in-memory-data.service';
......
......@@ -3,8 +3,5 @@
<div><span>name: </span>{{character.name}}</div>
<div><span>HP:</span>{{character.life}} <span>/</span> {{character.max_life}}</div>
<div><span>mana:</span>{{character.mana}} <span>/</span>{{character.max_mana}}</div>
</div>
<button click="goBack()">précédent</button>
<p>Test</p>
import { Injectable } from '@angular/core';
import { Character } from '../Classes/Character';
import { MessageService } from './message-service.service';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
......@@ -10,30 +9,24 @@ import { catchError, tap } from 'rxjs/operators';
providedIn: 'root'
})
export class CharacterService {
private characterUrl = "api/character";
private log(message: string)
{
this.messageService.add('CharacterService: ${message}');
}
private characterUrl = "api/characters";
private handleError<T>(operation = 'operation', result?: T){
return (error: any): Observable<T> => {
console.error(error);
this.log(`${operation} failed: ${error.message}`);
return of(result as T);
}
}
constructor(
private messageService: MessageService,
private http: HttpClient
) { }
getCharacters():Observable<Character[]>{
return this.http.get<Character[]>(this.characterUrl)
.pipe(
tap(_ =>this.log('fetched characters')),
tap(_ =>console.log('fetched characters')),
catchError(this.handleError<Character[]>('getCharacters',[]))
)
}
......@@ -42,7 +35,7 @@ export class CharacterService {
getCharacter(id: number): Observable<Character>{
const url = `${this.characterUrl}/${id}`;
return this.http.get<Character>(url).pipe(
tap(_=> this.log(`fetcted character id=${id}`)),
tap(_=> console.log(`fetcted character id=${id}`)),
catchError(this.handleError<Character>(`getCharacter id = ${id}`))
);
}
......
......@@ -9,9 +9,10 @@ import { Character } from './../../Classes/Character';
})
export class CharacterComponent implements OnInit {
character = new Character("Riaile", 0);
constructor(private characterService: CharacterService) {
//Character.cpid++;
character; //= new Character("Riaile", 0);
constructor(
private characterService: CharacterService
) {//Character.cpid++;
}
getCharacter(id: number):void{
this.characterService.getCharacter(id)
......
......@@ -9,16 +9,25 @@ export class InMemoryDataService implements InMemoryDbService {
createDb(){
const characters = [
new Character("Harry Potter", 1),
new Character("Hermione Granger", 2),
new Character("Ron Weasley", 3),
new Character("Voldmort", 666),
new Character("Drago Malefoy",4)
{ id: 1,
name: "Harry Potter",
life: 150,
mana: 100
}
];
const dragon = [
{ id: 1,
name: "Harry Potter",
life: 150,
mana: 100,
max_life: 150,
max_mana: 100
}
];
return characters;
return {characters, dragon};
}
getId(characters: Character[]):number{
return characters.length > 0? Math.max(...characters.map(characters => characters.id))+1 :11;
genId(characters: Character[]):number{
return characters.length > 0? Math.max(...characters.map(characters => characters.id))+1 :1;
}
}
import { TestBed } from '@angular/core/testing';
import { MessageService } from './message-service.service';
describe('MessageServiceService', () => {
beforeEach(() => TestBed.configureTestingModule({}));
it('should be created', () => {
const service: MessageService = TestBed.get(MessageService);
expect(service).toBeTruthy();
});
});
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class MessageService {
messages: string[] = [];
add(message: string){
this.messages.push(message);
}
clear(){
this.messages = [];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment