Exemplo n.º 1
0
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var album = await _context.Albums.Where(a => a.Id == id).FirstOrDefaultAsync();

            if (album == null)
            {
                return(NotFound());
            }

            var model = new AlbumEditBindingModel
            {
                Id          = album.Id,
                Title       = album.Title,
                Description = album.Description,
                FileName    = Path.Combine(_configuration.GetValue <string>("CustomSettings:ImagesPath"), FileHelpers.GetValidAlbumFolderName(album.Title.Trim()), album.CoverPhoto)
            };

            return(View(model));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Edit(int id, AlbumEditBindingModel model)
        {
            if (id != model.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    Album oldAlbum = _context.Albums.AsNoTracking().Where(a => a.Id == model.Id).FirstOrDefault();

                    string folderName       = FileHelpers.GetValidAlbumFolderName(model.Title.Trim());
                    string albumFolder      = GetAlbumPath(folderName);
                    string albumThumbFolder = Path.Combine(albumFolder, "thumb");

                    //if the album name has been changed, then update folder name
                    if (oldAlbum.Title.Trim() != model.Title.Trim())
                    {
                        string sourcePath = GetAlbumPath(oldAlbum.AlbumFolderName);

                        if (sourcePath != albumFolder)
                        {
                            Directory.Move(sourcePath, albumFolder);
                        }
                    }

                    //replace  image if changed
                    string fileName = string.Empty;
                    if (model.UploadImage != null)
                    {
                        fileName = await PrepareImage.CoverPhotoAsync(model.UploadImage, albumFolder, albumThumbFolder, "AlbumCover");
                    }

                    Album newAlbum = new Album()
                    {
                        Id              = model.Id,
                        Title           = model.Title.Trim(),
                        CoverPhoto      = string.IsNullOrEmpty(fileName) ? oldAlbum.CoverPhoto : fileName,
                        AlbumFolderName = folderName,
                        Description     = model.Description
                    };

                    _context.Update(newAlbum);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AlbumExists(model.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }