import {Injectable} from '@angular/core'; import {ShowSongDataService} from './show-song-data.service'; import {firstValueFrom, Observable} from 'rxjs'; import {ShowSong} from './show-song'; import {SongDataService} from '../../songs/services/song-data.service'; import {UserService} from '../../../services/user/user.service'; import {ShowService} from './show.service'; @Injectable({ providedIn: 'root', }) export class ShowSongService { public constructor( private showSongDataService: ShowSongDataService, private songDataService: SongDataService, private userService: UserService, private showService: ShowService, ) { } public async new$(showId: string, songId: string, addedLive = false): Promise { const song = await firstValueFrom(this.songDataService.read$(songId)); const user = await firstValueFrom(this.userService.user$); if (!song || !user) return null; const data: Partial = { ...song, songId, key: song.key, keyOriginal: song.key, chordMode: user.chordMode, addedLive, }; await this.userService.incSongCount(songId); return await this.showSongDataService.add(showId, data); } public read$ = (showId: string, songId: string): Observable => this.showSongDataService.read$(showId, songId); public read = (showId: string, songId: string): Promise => firstValueFrom(this.read$(showId, songId)); public list$ = (showId: string): Observable => this.showSongDataService.list$(showId); public list = (showId: string): Promise => firstValueFrom(this.list$(showId)); public async delete$(showId: string, showSongId: string, index: number): Promise { const showSong = await this.read(showId, showSongId); await this.showSongDataService.delete(showId, showSongId); const show = await firstValueFrom(this.showService.read$(showId)); if (!show) return; const order = show.order; order.splice(index, 1); await this.showService.update$(showId, {order}); await this.userService.decSongCount(showSong.songId); } public update$ = async (showId: string, songId: string, data: Partial): Promise => await this.showSongDataService.update$(showId, songId, data); }