/// <summary> /// Adds all new files marked for addition to the database /// Clears files marked for addition /// </summary> private async Task AddNewFiles() { if (m_filesToAdd.Count() < 1) { return; } foreach (var slice in m_filesToAdd.GetSlices(SAVE_TO_DISK_INTERVAL)) { var newTracks = new List <DbTrack>(); foreach (var trackFileName in slice) { var newTrack = new DbTrack { FileName = trackFileName }; newTrack.SetTrackData(TagLib.File.Create(trackFileName).Tag); newTracks.Add(newTrack); } using (MusicProvider mp = new MusicProvider()) { await mp.AllTracks.AddRangeAsync(newTracks); await mp.SaveChangesAsync(); } } }
/// <summary> /// Constructor. /// Creates an album from the set of tracks comprising the album. /// Metadata is taken from the first track in @tracks. /// </summary> /// <param name="tracks">A collection of tracks comprising an album.</param> public Album(IEnumerable <DbTrack> tracks) { DbTrack first = tracks.First(); Title = first.Album; Artist = first.AlbumArtist; Year = first.Year; Tracks = (uint)tracks.Count(); }
public async Task <AlbumDetail> UpdateAsync(int albumId, Album album) { PublishedStatusEnumMapper statusMapper = new PublishedStatusEnumMapper(); var dbalbum = await _context.Albums .Include(x => x.Artist) .Include(x => x.AlbumGenres) .ThenInclude(x => x.Genre) .Include(x => x.Tracks) .SingleOrDefaultAsync(x => x.Id == albumId); if (dbalbum != null) { dbalbum.Title = album.Title; dbalbum.DescriptionText = album.DescriptionText; dbalbum.PublishStatus = statusMapper.Map(album.PublishedStatus); dbalbum.Label = album.Label; dbalbum.Price = album.Price; dbalbum.Producer = album.Producer; dbalbum.ReleaseDate = album.ReleaseDate; dbalbum.TotalDurationInSeconds = album.TotalDurationInSeconds; dbalbum.ArtistId = album.ArtistId; // remove any existing genres that are not present on input model _context.AlbumGenres.RemoveRange(dbalbum.AlbumGenres.Where(x => x.AlbumId == albumId && !album.Genres.Contains(x.Genre.Name))); // add any genreas not already in the DB relationship foreach (string newGenreName in album.Genres) { if (dbalbum.AlbumGenres.FirstOrDefault(x => x.Genre.Name == newGenreName) == null) { var newGenreEntity = await _context.Genres.SingleOrDefaultAsync(x => x.Name == newGenreName); if (newGenreEntity != null) { dbalbum.AlbumGenres.Add(new DbAlbumGenre() { Genre = newGenreEntity, CreatedUtc = DateTime.UtcNow }); } else { // invalid genre throw new RepositoryException($"The supplied genre '{newGenreName}' does not exist."); } } } if (album.CoverImageId.HasValue) { var img = await this._context.ImageResources.SingleOrDefaultAsync(x => x.Id == album.CoverImageId.Value); if (img != null) { dbalbum.AlbumCoverImage = img; } else { throw new RepositoryException($"Invalid {nameof(album.CoverImageId)}, no image for ID"); } } // find any tracks with database ID's and remove anything in the database with an ID not in this list List <int> existingTrackIds = album.Tracks.Where(t => t.TrackId.HasValue).Select(t => t.TrackId.Value).ToList(); _context.Tracks.RemoveRange(dbalbum.Tracks.Where(x => x.AlbumId == albumId && !existingTrackIds.Contains(x.Id))); // now that any existing tracks which are not in the supplied model add or update based on the presence of a trackId foreach (var t in album.Tracks) { DbTrack existingTrack = null; if (t.TrackId.HasValue) { existingTrack = await _context.Tracks.SingleOrDefaultAsync(x => x.Id == t.TrackId.Value && x.AlbumId == albumId); } if (existingTrack != null) { // update track existingTrack.TrackNumber = t.TrackNumber; existingTrack.Title = t.Title; existingTrack.DurationInSeconds = t.DurationInSeconds; existingTrack.UpdatedUtc = DateTime.UtcNow; } else { //create new track dbalbum.Tracks.Add( new DbTrack { CreatedUtc = DateTime.UtcNow, DurationInSeconds = t.DurationInSeconds, Title = t.Title, TrackNumber = t.TrackNumber, UpdatedUtc = DateTime.UtcNow } ); } } await _context.SaveChangesAsync(); return(this._mapper.MapToDetailRep(dbalbum)); } else { throw new EntityNotFoundRepositoryException($"Album with ID {albumId} not found"); } }