public ActionResult Reorder(string button, ArchiveDocumentImage archiveDocumentImage)
        {
            using (var context = new DatabaseContext())
            {
                var images = context.ArchiveDocuments
                    .Where(x => x.Id == archiveDocumentImage.ArchiveDocumentId)
                    .Include(x => x.ArchiveDocumentImages)
                    .First().ArchiveDocumentImages;
                var beforeImage = images
                    .Where(x => x.SequenceNumber < archiveDocumentImage.SequenceNumber)
                    .OrderByDescending(x => x.SequenceNumber)
                    .FirstOrDefault();
                var currentImage = images
                    .First(x => x.SequenceNumber == archiveDocumentImage.SequenceNumber);
                var afterImage = images
                    .Where(x => x.SequenceNumber > archiveDocumentImage.SequenceNumber)
                    .OrderBy(x => x.SequenceNumber)
                    .FirstOrDefault();

                var otherImage = (button == "Поднять выше" ? beforeImage : button == "Опустить ниже" ? afterImage : null);
                if (otherImage != null)
                {
                    var swapNumber = otherImage.SequenceNumber;
                    otherImage.SequenceNumber = currentImage.SequenceNumber;
                    currentImage.SequenceNumber = swapNumber;
                    context.SaveChanges();
                }
            }
            return RedirectToAction("WithImages", "ArchiveDocument", new { id = archiveDocumentImage.ArchiveDocumentId });
        }
        public ActionResult Upload(ArchiveDocumentImage archiveDocumentImage, HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                using (var context = new DatabaseContext())
                {
                    var archiveDocumentImages = context.ArchiveDocumentImages
                        .Where(x => x.ArchiveDocumentId == archiveDocumentImage.ArchiveDocumentId).ToList();
                    var sequenceNumber = (byte)(archiveDocumentImages.Count == 0 ? 1
                        : archiveDocumentImages.Max(x => x.SequenceNumber) + 1);
                    archiveDocumentImage.SequenceNumber = sequenceNumber;
                    var bytes = archiveDocumentImage.Initialize(file.InputStream, file.ContentLength, file.FileName);

                    context.ArchiveDocumentImages.Add(archiveDocumentImage);
                    context.SaveChanges();

                    var storage = new ArchiveDocumentImageStorage();
                    storage.Upload(archiveDocumentImage.Id, bytes);
                }
            }
            return RedirectToAction("WithImages", "ArchiveDocument", new { id = archiveDocumentImage .ArchiveDocumentId});
        }
        /// <summary>Creates POC object from its properties</summary>
        /// <returns>POC object</returns>
        private static ArchiveDocumentImage ParseEntity(string folderName, string sequenceNumber, string archiveDocumentId)
        {
            var archiveDocumentImage = new ArchiveDocumentImage();

            byte number;
            if (!byte.TryParse(sequenceNumber, out number))
                throw new Exception("Incorrect ID during instantiating of Archive Document Image");
            archiveDocumentImage.SequenceNumber = number;

            int archiveDocumentNo;
            if (!int.TryParse(archiveDocumentId, out archiveDocumentNo))
                throw new Exception("Incorrect Archive Document ID during instantiating of Archive Document Image with ID = " + archiveDocumentImage.SequenceNumber);
            archiveDocumentImage.ArchiveDocumentId = archiveDocumentNo;

            var imagePath = string.Format(Constants.Blobs.ArchiveDocumentImageFormat, folderName, archiveDocumentNo, number);
            var fileInfo = new FileInfo(imagePath);
            using (var stream = File.OpenRead(imagePath))
            {
                archiveDocumentImage.Initialize(stream, (int)fileInfo.Length, fileInfo.Name);
            }

            return archiveDocumentImage;
        }