import {EnvironmentInjector, Injectable, inject, runInInjectionContext} from '@angular/core'; import {doc, Firestore, getDoc} from '@angular/fire/firestore'; import {Observable} from 'rxjs'; import {shareReplay} from 'rxjs/operators'; import {DbService} from 'src/app/services/db.service'; import {GuestShow} from './guest-show'; @Injectable({ providedIn: 'root', }) export class GuestShowDataService { private dbService = inject(DbService); private firestore = inject(Firestore); private environmentInjector = inject(EnvironmentInjector); private collection = 'guest'; public list$: Observable = this.dbService.col$(this.collection).pipe( shareReplay({ bufferSize: 1, refCount: true, }) ); public read$: (id: string) => Observable = (id: string): Observable => this.dbService.doc$(`${this.collection}/${id}`); public read: (id: string) => Promise = async (id: string): Promise => { const snapshot = await runInInjectionContext(this.environmentInjector, () => getDoc(doc(this.firestore, `${this.collection}/${id}`))); if (!snapshot.exists()) { return null; } return { id: snapshot.id, ...(snapshot.data() as Omit), }; }; public update$: (id: string, data: Partial) => Promise = async (id: string, data: Partial): Promise => await this.dbService.doc(this.collection + '/' + id).update(data); public add: (data: Partial) => Promise = async (data: Partial): Promise => (await this.dbService.col(this.collection).add(data)).id; public delete: (id: string) => Promise = async (id: string): Promise => await this.dbService.doc(this.collection + '/' + id).delete(); }