Skip to content

Commit 683852d

Browse files
committed
last polishes
1 parent af84b1c commit 683852d

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

lab5/restauracja/src/app/fire-base-service.service.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable } from '@angular/core';
22
import { AngularFireDatabase, AngularFireList } from '@angular/fire/compat/database';
3-
import { map, Observable } from 'rxjs';
3+
import { first, map, Observable } from 'rxjs';
44
import { 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

Comments
 (0)