set song count

This commit is contained in:
2024-06-09 22:28:18 +02:00
parent 669bd0d852
commit 2f523c5092
8 changed files with 82 additions and 6 deletions

12
.run/build.run.xml Normal file
View File

@@ -0,0 +1,12 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="build" type="js.build_tools.npm" nameIsGenerated="true">
<package-json value="$PROJECT_DIR$/package.json"/>
<command value="run"/>
<scripts>
<script value="build"/>
</scripts>
<node-interpreter value="project"/>
<envs/>
<method v="2"/>
</configuration>
</component>

12
.run/deploy.run.xml Normal file
View File

@@ -0,0 +1,12 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="deploy" type="js.build_tools.npm" nameIsGenerated="true">
<package-json value="$PROJECT_DIR$/package.json"/>
<command value="run"/>
<scripts>
<script value="deploy"/>
</scripts>
<node-interpreter value="project"/>
<envs/>
<method v="2"/>
</configuration>
</component>

View File

@@ -14,6 +14,7 @@ export class ShowDataService {
this.dbService.col$<Show>(this.collection).subscribe(_ => this.list$.next(_)); this.dbService.col$<Show>(this.collection).subscribe(_ => this.list$.next(_));
} }
public listRaw$ = () => this.dbService.col$<Show>(this.collection);
public list$ = new BehaviorSubject<Show[]>([]); public list$ = new BehaviorSubject<Show[]>([]);
public read$ = (showId: string): Observable<Show | null> => this.list$.pipe(map(_ => _.find(s => s.id === showId) || null)); public read$ = (showId: string): Observable<Show | null> => this.list$.pipe(map(_ => _.find(s => s.id === showId) || null));

View File

@@ -29,6 +29,7 @@ export class ShowSongService {
chordMode: user.chordMode, chordMode: user.chordMode,
addedLive, addedLive,
}; };
await this.userService.incSongCount(songId);
return await this.showSongDataService.add(showId, data); return await this.showSongDataService.add(showId, data);
} }
@@ -38,13 +39,15 @@ export class ShowSongService {
public list$ = (showId: string): Observable<ShowSong[]> => this.showSongDataService.list$(showId); public list$ = (showId: string): Observable<ShowSong[]> => this.showSongDataService.list$(showId);
public list = (showId: string): Promise<ShowSong[]> => firstValueFrom(this.list$(showId)); public list = (showId: string): Promise<ShowSong[]> => firstValueFrom(this.list$(showId));
public async delete$(showId: string, songId: string, index: number): Promise<void> { public async delete$(showId: string, showSongId: string, index: number): Promise<void> {
await this.showSongDataService.delete(showId, songId); const showSong = await this.read(showId, showSongId);
await this.showSongDataService.delete(showId, showSongId);
const show = await firstValueFrom(this.showService.read$(showId)); const show = await firstValueFrom(this.showService.read$(showId));
if (!show) return; if (!show) return;
const order = show.order; const order = show.order;
order.splice(index, 1); order.splice(index, 1);
await this.showService.update$(showId, {order}); await this.showService.update$(showId, {order});
await this.userService.decSongCount(showSong.songId);
} }
public update$ = async (showId: string, songId: string, data: Partial<ShowSong>): Promise<void> => await this.showSongDataService.update$(showId, songId, data); public update$ = async (showId: string, songId: string, data: Partial<ShowSong>): Promise<void> => await this.showSongDataService.update$(showId, songId, data);

View File

@@ -29,10 +29,10 @@
<div *ngIf="song.label">Verlag: {{ song.label }}</div> <div *ngIf="song.label">Verlag: {{ song.label }}</div>
<div *ngIf="song.origin">Quelle: {{ song.origin }}</div> <div *ngIf="song.origin">Quelle: {{ song.origin }}</div>
<div *ngIf="song.origin">Quelle: {{ song.origin }}</div> <div *ngIf="song.origin">Quelle: {{ song.origin }}</div>
<div *ngIf="songCount$()|async as count">Wie oft verwendet: {{ count }}</div>
</div> </div>
</div> </div>
<!-- <div class="text">{{song.text}}</div>-->
<app-song-text <app-song-text
*ngIf="user$ | async as user" *ngIf="user$ | async as user"
[chordMode]="user.chordMode" [chordMode]="user.chordMode"

View File

@@ -1,9 +1,9 @@
import {Component, OnInit} from '@angular/core'; import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router'; import {ActivatedRoute, Router} from '@angular/router';
import {SongService} from '../services/song.service'; import {SongService} from '../services/song.service';
import {map, switchMap} from 'rxjs/operators'; import {distinctUntilChanged, map, switchMap} from 'rxjs/operators';
import {Song} from '../services/song'; import {Song} from '../services/song';
import {Observable} from 'rxjs'; import {combineLatest, Observable} from 'rxjs';
import {FileDataService} from '../services/file-data.service'; import {FileDataService} from '../services/file-data.service';
import {File} from '../services/file'; import {File} from '../services/file';
import {UserService} from '../../../services/user/user.service'; import {UserService} from '../../../services/user/user.service';
@@ -71,4 +71,12 @@ export class SongComponent implements OnInit {
await this.showService.update$(show?.id, {order: [...show.order, newId ?? '']}); await this.showService.update$(show?.id, {order: [...show.order, newId ?? '']});
await this.router.navigateByUrl('/shows/' + show.id); await this.router.navigateByUrl('/shows/' + show.id);
} }
public songCount$ = () =>
combineLatest([this.user$, this.song$]).pipe(
map(([user, song]) => {
return user.songUsage[song.id];
}),
distinctUntilChanged()
);
} }

View File

@@ -6,6 +6,8 @@ import {User} from './user';
import {DbService} from '../db.service'; import {DbService} from '../db.service';
import {environment} from '../../../environments/environment'; import {environment} from '../../../environments/environment';
import {Router} from '@angular/router'; 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({ @Injectable({
providedIn: 'root', providedIn: 'root',
@@ -29,7 +31,9 @@ export class UserService {
public constructor( public constructor(
private afAuth: AngularFireAuth, private afAuth: AngularFireAuth,
private db: DbService, private db: DbService,
private router: Router private router: Router,
private showDataService: ShowDataService,
private showSongDataService: ShowSongDataService
) { ) {
this.afAuth.authState this.afAuth.authState
.pipe( .pipe(
@@ -49,6 +53,7 @@ export class UserService {
const aUser = await this.afAuth.signInWithEmailAndPassword(user, password); const aUser = await this.afAuth.signInWithEmailAndPassword(user, password);
if (!aUser.user) return null; if (!aUser.user) return null;
const dUser = await this.readUser(aUser.user.uid); const dUser = await this.readUser(aUser.user.uid);
await this.initSongUsage(dUser);
this.iUser$.next(dUser); this.iUser$.next(dUser);
this.iUserId$.next(aUser.user.uid); this.iUserId$.next(aUser.user.uid);
@@ -84,6 +89,40 @@ export class UserService {
await this.router.navigateByUrl('/brand/new-user'); 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$<User>('users/' + uid); private readUser$ = (uid: string) => this.db.doc$<User>('users/' + uid);
private readUser: (uid: string) => Promise<User | null> = (uid: string) => firstValueFrom(this.readUser$(uid)); private readUser: (uid: string) => Promise<User | null> = (uid: string) => firstValueFrom(this.readUser$(uid));
} }

View File

@@ -5,4 +5,5 @@ export interface User {
name: string; name: string;
role: string; role: string;
chordMode: ChordMode; chordMode: ChordMode;
songUsage: {[songId: string]: number};
} }