This commit is contained in:
2020-03-07 23:00:11 +01:00
committed by smuddy
parent ccd91aa81c
commit d68cd590ad
57 changed files with 2012 additions and 3489 deletions

View File

@@ -1,27 +1,27 @@
import {Injectable} from '@angular/core';
import {SongDataService} from './song-data.service';
import {File} from './file';
import {Observable} from 'rxjs';
import {map} from 'rxjs/operators';
import {FileServer} from './fileServer';
import {DbService} from '../../../services/db.service';
@Injectable({
providedIn: 'root'
})
export class FileDataService {
constructor(private songDataService: SongDataService) {
constructor(private db: DbService) {
}
public async put(songId: string, file: FileServer): Promise<string> {
const songRef = this.songDataService.getSongRef(songId);
public async set(songId: string, file: FileServer): Promise<string> {
const songRef = this.db.doc('songs/' + songId);
const fileCollection = songRef.collection('files');
const id = await fileCollection.add(file);
return id.id;
}
public get$(songId: string): Observable<File[]> {
const songRef = this.songDataService.getSongRef(songId);
public read$(songId: string): Observable<File[]> {
const songRef = this.db.doc('songs/' + songId);
return songRef.collection<File>('files').snapshotChanges().pipe(map(actions => {
return actions.map(a => ({
...a.payload.doc.data(),

View File

@@ -31,7 +31,7 @@ describe('SongDataService', () => {
it('should list songs', async(() => {
const service: SongDataService = TestBed.get(SongDataService);
service.list().subscribe(s => {
service.list$().subscribe(s => {
expect(s).toEqual([
{title: 'title1'}
] as any);

View File

@@ -1,40 +1,17 @@
import {Injectable} from '@angular/core';
import {AngularFirestore, AngularFirestoreCollection} from '@angular/fire/firestore';
import {Song} from '../models/song';
import {Song} from './song';
import {Observable} from 'rxjs';
import {map} from 'rxjs/operators';
import {AngularFirestoreDocument} from '@angular/fire/firestore/document/document';
import {DbService} from '../../../services/db.service';
@Injectable({
providedIn: 'root'
})
export class SongDataService {
private songCollection: AngularFirestoreCollection<Song>;
private readonly songs: Observable<Song[]>;
constructor(private afs: AngularFirestore) {
this.songCollection = afs.collection<Song>('songs');
this.songs = this.songCollection.snapshotChanges().pipe(map(actions => {
return actions.map(a => ({
...a.payload.doc.data(),
id: a.payload.doc.id
}));
}));
constructor(private dbService: DbService) {
}
public list = (): Observable<Song[]> => this.songs;
public getSongRef = (songId: string): AngularFirestoreDocument<Song> => this.afs.doc<Song>('songs/' + songId);
public read(songId: string): Observable<Song | undefined> {
return this.getSongRef(songId).valueChanges().pipe(map(song => ({
...song,
id: songId
} as Song)));
}
public async update(songId: string, data: any): Promise<void> {
await this.getSongRef(songId).update(data);
}
public list$ = (): Observable<Song[]> => this.dbService.col$('songs');
public read$ = (songId: string): Observable<Song | undefined> => this.dbService.doc$('songs/' + songId);
public update = async (songId: string, data: any): Promise<void> => await this.dbService.doc(songId).update(data);
}

View File

@@ -1,6 +1,6 @@
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs';
import {Song} from '../models/song';
import {Song} from './song';
import {SongDataService} from './song-data.service';
import {tap} from 'rxjs/operators';
@@ -26,8 +26,8 @@ export class SongService {
importCCLI = (songs: Song[]) => this.updateFromCLI(songs);
}
public list$ = (): Observable<Song[]> => this.songDataService.list().pipe(tap(_ => this.list = _));
public read = (songId: string): Observable<Song | undefined> => this.songDataService.read(songId);
public list$ = (): Observable<Song[]> => this.songDataService.list$().pipe(tap(_ => this.list = _));
public read = (songId: string): Observable<Song | undefined> => this.songDataService.read$(songId);
public async update(songId: string, data: any): Promise<void> {
await this.songDataService.update(songId, data);

View File

@@ -0,0 +1,22 @@
export interface Song {
id: string;
comment: string;
final: boolean;
key: string;
number: number;
tempo: number;
text: string;
title: string;
type: string;
legalType: string;
legalLink: string;
legalOwner: string;
legalOwnerId: string;
legalLicenseId: string;
artist: string;
label: string;
termsOfUse: string;
origin: string;
}

View File

@@ -55,6 +55,6 @@ export class UploadService extends FileBase {
path: upload.path,
createdAt: new Date()
};
await this.fileDataService.put(songId, file);
await this.fileDataService.set(songId, file);
}
}