11import { Injectable } from '@angular/core' ;
22import { AngularFireDatabase , AngularFireList } from '@angular/fire/compat/database' ;
3- import { map , Observable } from 'rxjs' ;
3+ import { first , map , Observable } from 'rxjs' ;
44import { Dish } from './IDish' ;
55
66@Injectable ( {
@@ -12,7 +12,7 @@ export class FireBaseServiceService {
1212 private nextId : number | undefined
1313 constructor ( private db : AngularFireDatabase ) {
1414 this . dishes = this . db . list ( 'Dishes' ) . valueChanges ( ) ;
15- this . db . list ( 'Dishes' , ref => ref . orderByChild ( 'id' ) . limitToLast ( 1 ) ) . valueChanges ( ) . subscribe ( ( res : any [ ] ) => { this . nextId = res . pop ( ) . id + 1 } )
15+ this . db . list ( 'Dishes' , ref => ref . orderByChild ( 'id' ) . limitToLast ( 1 ) ) . valueChanges ( ) . subscribe ( ( res : any [ ] ) => { this . nextId = res [ 0 ] ? .id + 1 } )
1616 }
1717
1818 getDishes ( ) : Observable < any [ ] > {
@@ -38,7 +38,7 @@ export class FireBaseServiceService {
3838
3939 removeDish ( idx : number ) {
4040 console . log ( idx )
41- this . db . list ( 'Dishes' ) . snapshotChanges ( ) . subscribe ( ( items :any ) => {
41+ this . db . list ( 'Dishes' ) . snapshotChanges ( ) . pipe ( first ( ) ) . subscribe ( ( items :any ) => {
4242 for ( let i of items ) {
4343 if ( i . payload . val ( ) . id == idx )
4444 {
@@ -47,8 +47,42 @@ export class FireBaseServiceService {
4747 }
4848 }
4949 } )
50+ }
5051
52+ changePriceOfDish ( idx : number , price : number ) {
53+ this . db . list ( 'Dishes' ) . snapshotChanges ( ) . pipe ( first ( ) ) . subscribe ( ( items :any ) => {
54+ for ( let i of items ) {
55+ if ( i . payload . val ( ) . id == idx )
56+ {
57+ console . log ( i . payload . key )
58+ this . db . list ( 'Dishes' ) . update ( i . payload . key , { Price : price } )
59+ }
60+ }
61+ } )
62+ }
5163
64+ changeNameOfDish ( idx : number , name : string ) {
65+ this . db . list ( 'Dishes' ) . snapshotChanges ( ) . pipe ( first ( ) ) . subscribe ( ( items :any ) => {
66+ for ( let i of items ) {
67+ if ( i . payload . val ( ) . id == idx )
68+ {
69+ console . log ( i . payload . key )
70+ this . db . list ( 'Dishes' ) . update ( i . payload . key , { Name : name } )
71+ }
72+ }
73+ } )
74+ }
75+
76+ changeDescOfDish ( idx : number , desc : string ) {
77+ this . db . list ( 'Dishes' ) . snapshotChanges ( ) . pipe ( first ( ) ) . subscribe ( ( items :any ) => {
78+ for ( let i of items ) {
79+ if ( i . payload . val ( ) . id == idx )
80+ {
81+ console . log ( i . payload . key )
82+ this . db . list ( 'Dishes' ) . update ( i . payload . key , { ShortDesc : desc } )
83+ }
84+ }
85+ } )
5286 }
5387
5488 getNextid ( ) {
0 commit comments