public async Task RemovePreviouslyPlayedSongsAsync(int currentRoomId) { var room = await _playlistsContext.Rooms.SingleOrDefaultAsync(s => s.Id == currentRoomId); var spotify = new SpotifyService(room.SpotifyAuthCode); var currentlyPlayingSong = await spotify.GetCurrentlyPlayingSongAsync(); void RemoveSongs() { if (currentlyPlayingSong != null) { if ((bool)!currentlyPlayingSong?.IsPlaying && (bool)room.RoomSongs.Single(s => s.Song.ServiceId == currentlyPlayingSong?.Item?.Uri)?.PreviouslyPlayed) { room.RoomSongs.RemoveAll(s => s.PreviouslyPlayed); } room.RoomSongs.RemoveAll(s => s.PreviouslyPlayed && s.Song.ServiceId != currentlyPlayingSong?.Item?.Uri); } } await UpdatePreviouslyPlayedSongs(currentRoomId); RemoveSongs(); await _playlistsContext.SaveChangesAsync(); }
public async Task UpdatePreviouslyPlayedSongs(int currentRoomId) { var room = await _playlistsContext .Rooms .Include(s => s.RoomSongs) .ThenInclude(s => s.Song) .SingleOrDefaultAsync(s => s.Id == currentRoomId); var spotify = new SpotifyService(room.SpotifyAuthCode); var currentlyPlayingSong = await spotify.GetCurrentlyPlayingSongAsync(); var roomSong = room.RoomSongs.SingleOrDefault(s => s.Song.ServiceId == currentlyPlayingSong?.Item?.Uri); if (roomSong != null && !roomSong.PreviouslyPlayed) { roomSong.PreviouslyPlayed = true; _playlistsContext.RoomSongs.Update(roomSong); } }