private async Task AddSongToSpotify(Playlist playlist, string song, bool checkExistence = true) { if (playlist == null || string.IsNullOrWhiteSpace(song) || song == "+" || song == "-") { return; } var track = await _trackRepository.RetrieveAsync(searchText : song); if (track == null) { track = await _spotify.SearchForATrackAsync(song); if (track == null) { Log.Warning("SpotifyTrackNotFound! {SearchedText} for Radio {radio}", song, playlist.Name.Split(' ')[0]); return; } track.Artist = track.Artists.FirstOrDefault()?.Name; track.SearchText = song; track.CreateDate = DateTime.UtcNow; var dbTrack = await _trackRepository.RetrieveAsync(code : track.Code); if (dbTrack == null) { var trackId = await _trackRepository.RegisterAsync(track); if (trackId < 1) { Log.Error("Failed to register the track to repository! {@track}", track); return; } track.TrackId = trackId; } else { track.TrackId = dbTrack.TrackId; Log.Warning("Track is present with different search text! {@SpotifyTrack} {@DbTrack}", track, dbTrack); } } if (checkExistence && await _playlistRepository.ContainsTrackAsync(playlist, track)) { return; } if (await _spotify.AddTrackToPlaylistAsync(playlist, track)) { Log.Information("Spotify track is added. Playlist:{@Playlist} Track:{@Track}", playlist, track); } if (!await _playlistRepository.AddTrackAsync(playlist, track)) { Log.Error("Failed to add the track to playlist! {@Playlist} {@Track}", playlist, track); } }