public Task AddPlaylist(UserPlaylist playlist) { Playlists.Add(playlist); PlaylistAdded?.Invoke(this, playlist); using ( var command = new SQLiteCommand( "INSERT INTO `Playlists` (Name, Guid) VALUES (@name, @guid)", _connection)) { command.Parameters.AddWithValue("@name", playlist.Name); command.Parameters.AddGuid("@guid", playlist.Id); return(command.ExecuteNonQueryAsync()); } }
public async Task <Playlist> AddPlaylist(string name, IEnumerable <string> filenames) { if (name == null) { throw new ArgumentNullException("name"); } await _dbContextMutex.WaitAsync(); var existingPlaylist = _dukeboxData.Playlists.FirstOrDefault(a => a.Name.Equals(name, StringComparison.CurrentCulture)); if (existingPlaylist != null) { _dbContextMutex.Release(); logger.WarnFormat("Not adding playlist with name '{0}' as another playlist with the same name already exists in the database", name); return(existingPlaylist); } if (filenames == null) { throw new ArgumentNullException("filenames"); } var stopwatch = Stopwatch.StartNew(); var newPlaylist = new Playlist() { Name = name, FilenamesCsv = string.Join(",", filenames) }; try { _dukeboxData.Playlists.Add(newPlaylist); await _dukeboxData.SaveChangesAsync(); // Invalidate playlist cache. _allPlaylistsCache = null; stopwatch.Stop(); logger.InfoFormat("Added playlist to library: {0}", newPlaylist.Name); logger.DebugFormat("Adding playlist to library took {0}ms. Playlist id: {1}", stopwatch.ElapsedMilliseconds, newPlaylist.Id); PlaylistAdded?.Invoke(this.PlaylistAdded, EventArgs.Empty); return(newPlaylist); } catch (DbEntityValidationException ex) { logger.ErrorFormat("Error adding playlist '{0}' to database due to entity validation errors", newPlaylist.Name); _dukeboxData.LogEntityValidationException(ex); } catch (Exception ex) { logger.Error(string.Format("Error adding playlist '{0}' to database", newPlaylist.Name), ex); } finally { _dbContextMutex.Release(); } return(null); }