public Artist CreateArtist( Artist artist, HttpServerUtilityBase server ) { if( artist == null ) throw new ArgumentException( "Artist can't be null" ); if( server == null ) throw new ArgumentException( "Server can't be null" ); using( var ctx = new NwdBackOfficeContext() ) { if( !ArtistExists( artist ) ) ctx.Artists.Add( artist ); ctx.SaveChanges(); return ctx.Artists.Single( a => a.Name == artist.Name ); } }
// POST api/track public async Task Post( [FromBody]TrackRegisterViewModel model ) { Stream buffer = null; try { if( Request.Content.IsMimeMultipartContent() ) { var provider = await Request.Content.ReadAsMultipartAsync(); buffer = await provider.Contents[0].ReadAsStreamAsync(); } if( buffer == null ) throw new ArgumentNullException(); using( var ctx = new NwdBackOfficeContext() ) { int number = ctx.Albums.Where( a => a.Id == model.AlbumId ).FirstOrDefault().Tracks.Max( a => a.Number ); Track t = new Track { AlbumId = model.AlbumId, Duration = new TimeSpan( model.Duration ), Song = new Song { Composed = model.Song.Composed, Name = model.Song.Name }, Number = number }; ctx.Albums.SingleOrDefault( a => a.Id == model.AlbumId ).Tracks.Add( t ); ctx.SaveChanges(); string path = String.Format( "~/Private/{0}/{1}", model.AlbumId, t.AlbumId, t.Number ); t.FileRelativePath = path; FileStream f = File.Create( HostingEnvironment.MapPath( path ) ); buffer.Seek( 0, SeekOrigin.Begin ); buffer.CopyTo( f ); f.Close(); ctx.SaveChanges(); } } finally { buffer.Dispose(); } }
public static Album CreateAlbum(Album album, HttpServerUtilityBase server) { int i = 0; if (album == null) { throw new ArgumentNullException("album"); } if (server == null) { throw new ArgumentNullException("server"); } using (var ctx = new NwdBackOfficeContext()) { album = ctx.Albums.Add(album); string directory; string physDirectory; EnsureDirectory(album, server, out directory, out physDirectory); foreach (var track in album.Tracks) { track.Number = i; i++; 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 Album EditAlbum( Album album, HttpServerUtilityBase server ) { using( var ctx = new NwdBackOfficeContext() ) { 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( album, server, 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 ); } //else do not change the FileRelativePath since it is send by the form in an hidden input } if( GetAlbumForEdit( (int)album.Id ).CoverImagePath != album.CoverImagePath ) { if( album.CoverImagePath == null || album.CoverImagePath.Length == 0 ) album.CoverImagePath = "album.jpg"; string coverFileName = "cover.jpg"; string physPath = Path.Combine( physDirectory, coverFileName ); _fileRepo.SaveFile( album.CoverImagePath, physPath ); album.CoverImagePath = Path.Combine( directory, coverFileName ); } ctx.Entry( album ).State = System.Data.EntityState.Modified; //foreach( var e in ctx.ChangeTracker.Entries() ) //{ // e.State = System.Data.EntityState.Modified; //} ctx.SaveChanges(); return album; } }