not working

This commit is contained in:
2021-01-13 15:40:43 +01:00
parent da51077869
commit d4caaae0f2
7 changed files with 162 additions and 55 deletions

View File

@ -17,7 +17,7 @@ export class DashboardComponent implements OnInit {
}
getHeroes(): void {
this.heroService.getHeroes().subscribe(heroes => this.heroes = heroes.slice(1, 5));
this.heroService.getHeroes().then(heroes => this.heroes = heroes.slice(1, 5));
}
}

View File

@ -0,0 +1,28 @@
import { Connection, ConnectionConfig, createConnection } from 'mariadb'
export class DbHandle {
private connConfig?: ConnectionConfig
private conn?: Connection
public constructor() {
this.connConfig = {
host: '172.16.10.18',
user: 'heroes',
password: 'test123',
database: 'heroes'
}
}
public async connect(): Promise<void> {
this.conn = await createConnection(this.connConfig)
}
public query(stmt: string) : Promise<any> {
return this.conn?.query(stmt) ?? Promise.reject(new Error('Connection not ready (query)'))
}
public close() : Promise<void> {
return this.conn?.end() ?? Promise.reject(new Error('Connection not ready (close)'))
}
}

View File

@ -22,7 +22,7 @@ export class HeroDetailComponent implements OnInit {
getHero(): void {
const id = +this.route.snapshot.paramMap.get('id');
this.heroService.getHero(id).subscribe(hero => this.hero = hero);
this.heroService.getHero(id).then(hero => this.hero = hero);
}
goBack(): void {
@ -30,6 +30,6 @@ export class HeroDetailComponent implements OnInit {
}
save(): void {
this.heroService.updateHero(this.hero).subscribe(() => this.goBack());
this.heroService.updateHero(this.hero).then(() => this.goBack());
}
}

View File

@ -1,5 +1,6 @@
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { DbHandle } from './db-handle';
import { Hero } from './hero';
import { HEROES } from './mock-heroes';
@ -9,21 +10,36 @@ import { MessageService } from './message.service';
providedIn: 'root'
})
export class HeroService {
constructor(private messageService: MessageService) { }
getHeroes(): Observable<Hero[]> {
async getHeroes(): Promise<Hero[]> {
this.messageService.add('HeroService: fetched heroes');
return of(HEROES);
const dbHandle = new DbHandle()
try {
await dbHandle.connect()
const result = await dbHandle.query('SELECT id, name FROM hero')
return result.map((row: Record<string, unknown>) => {
return {id: row.id ?? -1, name: row.name ?? 'unknown' }
})
} finally {
dbHandle.close()
}
}
getHero(id: number): Observable<Hero> {
async getHero(id: number): Promise<Hero> {
this.messageService.add(`HeroService: fetch hero id=${id}`);
return of(HEROES.find(hero => hero.id === id));
const dbHandle = new DbHandle()
try {
await dbHandle.connect()
const result = await dbHandle.query(`SELECT id, name FROM hero WHERE id = ${id}`)
return { id: result[0].id ?? -1, name: result[0].name ?? 'unknown' }
} finally {
dbHandle.close()
}
}
updateHero(hero: Hero): Observable<any> {
updateHero(hero: Hero): Promise<any> {
this.messageService.add(`HeroService: save change: id=${hero.id}, name=${hero.name}`);
return of(true);
return Promise.resolve(true);
}
}

View File

@ -19,7 +19,7 @@ export class HeroesComponent implements OnInit {
constructor(private heroService: HeroService, private messageService: MessageService) { }
getHeroes(): void {
this.heroService.getHeroes().subscribe(heroes => this.heroes = heroes);
this.heroService.getHeroes().then(heroes => this.heroes = heroes);
}
ngOnInit(): void {