Exemple #1
0
 /// <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 ActionResult Edit(Album album)
 {
     try
     {
         AlbumRepository.EditAlbum(album, Server);
         ViewBag.success = "Modification effectuée.";
     }
     catch
     {
         ViewBag.success = "/!\\Erreur lors de la modification./!\\";
     }
     return View();
 }
Exemple #3
0
        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 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 ActionResult CreateOrEdit(Album album)
 {
     try
     {
         if (album.Title != null || album.Title != string.Empty)
             if (album.Artist != null)
             {
                 album.ReleaseDate = DateTime.Now;
                 AlbumRepository.CreateAlbum(album, Server);
                 @ViewBag.success = "Album créé.";
             }
     }
     catch
     {
         @ViewBag.success = "/!\\Erreur lors de la création./!\\";
     }
     return View();
 }
        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;
            }
        }
Exemple #7
0
 private static void EnsureDirectory( HttpServerUtilityBase server, Album album, out string directory, out string physDirectory )
 {
     directory = String.Format( "/Content/musics/{0}/", album.Title );
     physDirectory = server.MapPath( directory );
     if( physDirectory != null && !Directory.Exists( physDirectory ) )
     {
         Directory.CreateDirectory( physDirectory );
     }
 }
        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;
            }
        }