public AlbumViewModel GetAlbum( int idAlbum ) { if( idAlbum == 0 ) { throw new ArgumentException( "The id of the album must not be 0", "idAlbum" ); } using( var ctx = new NwdMusikEntities() ) { Album album = ctx.Albums.SingleOrDefault( a => a.Id == idAlbum ); if( album != null ) { return new AlbumViewModel { AlbumName = album.Title, CoverImageUrl = album.CoverImagePath, Tracks = (from t in album.Tracks select new TrackViewModel { SongName = t.Song.Name, SongUrl = t.FileRelativePath, TrackNumber = t.Number } ).ToArray() }; } return null; } }
/// <summary> /// Gets wether an album exists or not. /// </summary> /// <param name="album"></param> /// <returns></returns> public bool AlbumExists( Album album ) { if( album == null ) { throw new ArgumentNullException( "album" ); } using( var ctx = new NwdMusikEntities() ) { return ctx.Albums.Any( a => a.Title == album.Title ); } }
public Album EditAlbum( HttpServerUtilityBase server, Album album ) { using( var ctx = new NwdMusikEntities() ) { //album = ctx.Albums.Attach( album ); //ctx.Entry( album ).Reference( e => e.Artist ).Load(); //ctx.Entry( album ).Collection( e => e.Tracks ).Load(); string directory; string physDirectory; EnsureDirectory( server, album, out directory, out physDirectory ); foreach( var track in album.Tracks ) { HttpPostedFileBase file = track.File; if( file != null ) { //TODO delete previous file string fileName = SaveFile( physDirectory, file ); track.FileRelativePath = Path.Combine( directory, fileName ); //ctx.Entry( track ).State = System.Data.Entity.EntityState.Added; //ctx.Entry( track.Song ).State = System.Data.Entity.EntityState.Added; } //else do not change the FileRelativePath since it is send by the form in an hidden input } if( album.CoverFile != null ) { string coverFileName = "cover.jpg"; string physPath = Path.Combine( physDirectory, coverFileName ); album.CoverFile.SaveAs( physPath ); album.CoverImagePath = Path.Combine( directory, coverFileName ); } //foreach( var e in ctx.ChangeTracker.Entries() ) //{ // e.State = System.Data.Entity.EntityState.Modified; //} //ctx.Entry( album ).State = System.Data.Entity.EntityState.Modified; ctx.SaveChanges(); return album; } }
public void Database_Should_Always_Be_Created() { Database.SetInitializer(new DropCreateDatabaseAlways<NwdMusikEntities>()); Database.SetInitializer( new DropCreateDatabaseAlways<NwdFrontOfficeContext>() ); using (var ctx = new NwdMusikEntities()) { ctx.Database.Initialize(true); Assert.That(ctx.Database.Exists()); Console.WriteLine(ctx.Database.Connection.ConnectionString); } using( var ctx = new NwdFrontOfficeContext() ) { ctx.Database.Initialize( true ); Assert.That( ctx.Database.Exists() ); Console.WriteLine( ctx.Database.Connection.ConnectionString ); } }
public Catalog GetCatalog() { using( var ctx = new NwdMusikEntities() ) { return new Catalog { Albums = (from a in ctx.Albums select new AlbumThumbnailViewModel { AlbumId = a.Id, AlbumName = a.Title, ArtistName = a.Artist.Name, CoverImageUrl = a.CoverImagePath, TracksCount = a.Tracks.Count }).ToArray(), NumberOfAlbum = ctx.Albums.Count() }; } }
public MiniPlayer GetMiniPlayerFor( int idAlbum, int trackNumber ) { using( var ctx = new NwdMusikEntities() ) { return ( from a in ctx.Albums let track = (from s in a.Tracks where s.AlbumId == a.Id && s.Number == trackNumber select s).FirstOrDefault() where a.Id == idAlbum select new MiniPlayer { SongFilePath = track.FileRelativePath } ).FirstOrDefault(); } }
/// <summary> /// Create an album and add it to the store. An album is composed of tracks. /// </summary> /// <param name="album"></param> /// <param name="server"></param> /// <returns></returns> public Album CreateAlbum( Album album, HttpServerUtilityBase server ) { if( album == null ) { throw new ArgumentNullException( "album" ); } if( server == null ) { throw new ArgumentNullException( "server" ); } using( var ctx = new NwdMusikEntities() ) { album = ctx.Albums.Add( album ); string directory; string physDirectory; EnsureDirectory( server, album, out directory, out physDirectory ); foreach( var track in album.Tracks ) { HttpPostedFileBase file = track.File; if( file == null ) throw new ApplicationException( "You must add a file to a track" ); string fileName = SaveFile( physDirectory, file ); track.FileRelativePath = Path.Combine( directory, fileName ); } if( album.CoverFile != null ) { string coverFileName = "cover.jpg"; string physPath = Path.Combine( physDirectory, coverFileName ); album.CoverFile.SaveAs( physPath ); album.CoverImagePath = Path.Combine( directory, coverFileName ); } ctx.SaveChanges(); return album; } }
public void DeleteAlbum(Album album) { if (album == null) { throw new ArgumentNullException("Aucun album"); } using (var ctx = new NwdMusikEntities()) { var albumToDelete = ctx.Albums.Find(album.Id); if (albumToDelete != null) ctx.Albums.Remove(albumToDelete); ctx.SaveChanges(); } }
public List<Album> GetAllAlbums() { using( var ctx = new NwdMusikEntities() ) { return ctx.Albums.ToList(); } }
public Album GetAlbumForEdit( int idAlbum ) { using( var ctx = new NwdMusikEntities() ) { return ctx.Albums.Include( "Tracks" ).Include( "Tracks.Song" ).Include( "Artist" ).SingleOrDefault( a => a.Id == idAlbum ); } }