add show
This commit is contained in:
12
src/app/services/db.service.spec.ts
Normal file
12
src/app/services/db.service.spec.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
|
||||
import {DbService} from './db.service';
|
||||
|
||||
describe('DbService', () => {
|
||||
beforeEach(() => TestBed.configureTestingModule({}));
|
||||
|
||||
it('should be created', () => {
|
||||
const service: DbService = TestBed.get(DbService);
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
||||
44
src/app/services/db.service.ts
Normal file
44
src/app/services/db.service.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument} from '@angular/fire/firestore';
|
||||
import {Observable} from 'rxjs';
|
||||
import {map} from 'rxjs/operators';
|
||||
|
||||
type CollectionPredicate<T> = string | AngularFirestoreCollection<T>;
|
||||
type DocumentPredicate<T> = string | AngularFirestoreDocument<T>;
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class DbService {
|
||||
|
||||
constructor(private afs: AngularFirestore) {
|
||||
}
|
||||
|
||||
public col<T>(ref: CollectionPredicate<T>, queryFn?): AngularFirestoreCollection<T> {
|
||||
return typeof ref === 'string' ? this.afs.collection<T>(ref, queryFn) : ref;
|
||||
}
|
||||
|
||||
public doc<T>(ref: DocumentPredicate<T>): AngularFirestoreDocument<T> {
|
||||
return typeof ref === 'string' ? this.afs.doc<T>(ref) : ref;
|
||||
}
|
||||
|
||||
public doc$<T>(ref: DocumentPredicate<T>): Observable<T> {
|
||||
return this.doc(ref).snapshotChanges().pipe(
|
||||
map(doc => {
|
||||
const data = doc.payload.data();
|
||||
const id = doc.payload.id;
|
||||
return {...data, id} as T;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
public col$<T>(ref: CollectionPredicate<T>, queryFn?): Observable<T[]> {
|
||||
return this.col(ref, queryFn).snapshotChanges().pipe(
|
||||
map(doc => doc.map(_ => {
|
||||
const data = _.payload.doc.data();
|
||||
const id = _.payload.doc.id;
|
||||
return {...data, id} as T;
|
||||
}))
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -3,19 +3,19 @@ import {AngularFireAuth} from '@angular/fire/auth';
|
||||
import {Observable} from 'rxjs';
|
||||
import {filter, switchMap} from 'rxjs/operators';
|
||||
import {User} from './user';
|
||||
import {AngularFirestore} from '@angular/fire/firestore';
|
||||
import {DbService} from './db.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class UserService {
|
||||
constructor(private afAuth: AngularFireAuth, private afs: AngularFirestore) {
|
||||
constructor(private afAuth: AngularFireAuth, private db: DbService) {
|
||||
}
|
||||
|
||||
public get user$(): Observable<User> {
|
||||
return this.afAuth.authState.pipe(
|
||||
filter(_ => !!_),
|
||||
switchMap(auth => this.afs.doc<User>('user/' + auth.uid).valueChanges())
|
||||
switchMap(auth => this.db.doc$<User>('user/' + auth.uid))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
export interface User {
|
||||
id: string;
|
||||
name: string;
|
||||
role: 'admin';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user