=> await this.showSongDataService.update$(showId, songId, data);
diff --git a/src/app/modules/songs/song/song.component.html b/src/app/modules/songs/song/song.component.html
index d706157..c0ad167 100644
--- a/src/app/modules/songs/song/song.component.html
+++ b/src/app/modules/songs/song/song.component.html
@@ -29,10 +29,10 @@
Verlag: {{ song.label }}
Quelle: {{ song.origin }}
Quelle: {{ song.origin }}
+ Wie oft verwendet: {{ count }}
-
+ combineLatest([this.user$, this.song$]).pipe(
+ map(([user, song]) => {
+ return user.songUsage[song.id];
+ }),
+ distinctUntilChanged()
+ );
}
diff --git a/src/app/services/user/user.service.ts b/src/app/services/user/user.service.ts
index 708e975..8fb2732 100644
--- a/src/app/services/user/user.service.ts
+++ b/src/app/services/user/user.service.ts
@@ -6,6 +6,8 @@ import {User} from './user';
import {DbService} from '../db.service';
import {environment} from '../../../environments/environment';
import {Router} from '@angular/router';
+import {ShowDataService} from '../../modules/shows/services/show-data.service';
+import {ShowSongDataService} from '../../modules/shows/services/show-song-data.service';
@Injectable({
providedIn: 'root',
@@ -29,7 +31,9 @@ export class UserService {
public constructor(
private afAuth: AngularFireAuth,
private db: DbService,
- private router: Router
+ private router: Router,
+ private showDataService: ShowDataService,
+ private showSongDataService: ShowSongDataService
) {
this.afAuth.authState
.pipe(
@@ -49,6 +53,7 @@ export class UserService {
const aUser = await this.afAuth.signInWithEmailAndPassword(user, password);
if (!aUser.user) return null;
const dUser = await this.readUser(aUser.user.uid);
+ await this.initSongUsage(dUser);
this.iUser$.next(dUser);
this.iUserId$.next(aUser.user.uid);
@@ -84,6 +89,40 @@ export class UserService {
await this.router.navigateByUrl('/brand/new-user');
}
+ public incSongCount = (songId: string) => this.updateSongUsage(songId, 1);
+ public decSongCount = (songId: string) => this.updateSongUsage(songId, -1);
+
+ private async updateSongUsage(songId: string, direction: number) {
+ const user = await firstValueFrom(this.user$);
+ if (!user) return null;
+
+ const songUsage = user?.songUsage ?? {};
+ let currentSongCount = songUsage[songId];
+ if (currentSongCount === null || currentSongCount === undefined) currentSongCount = 0;
+ else currentSongCount = currentSongCount + direction;
+ songUsage[songId] = Math.max(0, currentSongCount);
+
+ await this.update$(user.id, {songUsage});
+ }
+
+ private async initSongUsage(user: User) {
+ if (user.songUsage) return;
+
+ const shows = await firstValueFrom(this.showDataService.listRaw$());
+ const myShows = shows.filter(show => show.owner === user.id);
+ const songUsage: {[songId: string]: number} = {};
+ for (const show of myShows) {
+ const showSongs = await firstValueFrom(this.showSongDataService.list$(show.id));
+ for (const showSong of showSongs) {
+ const current = songUsage[showSong.songId] ?? 0;
+ songUsage[showSong.songId] = current + 1;
+ }
+ }
+
+ await this.update$(user.id, {songUsage});
+ return;
+ }
+
private readUser$ = (uid: string) => this.db.doc$('users/' + uid);
private readUser: (uid: string) => Promise = (uid: string) => firstValueFrom(this.readUser$(uid));
}
diff --git a/src/app/services/user/user.ts b/src/app/services/user/user.ts
index c63a7eb..dc76bb2 100644
--- a/src/app/services/user/user.ts
+++ b/src/app/services/user/user.ts
@@ -5,4 +5,5 @@ export interface User {
name: string;
role: string;
chordMode: ChordMode;
+ songUsage: {[songId: string]: number};
}