public ActionResult Reorder(string button, AgreementImage agreementImage) { using (var context = new DatabaseContext()) { var images = context.Agreements .Where(x => x.Id == agreementImage.AgreementId) .Include(x => x.AgreementImages) .First().AgreementImages; var beforeImage = images .Where(x => x.SequenceNumber < agreementImage.SequenceNumber) .OrderByDescending(x => x.SequenceNumber) .FirstOrDefault(); var currentImage = images .First(x => x.SequenceNumber == agreementImage.SequenceNumber); var afterImage = images .Where(x => x.SequenceNumber > agreementImage.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", "Agreement", new { id = agreementImage.AgreementId }); }
public ActionResult Upload(AgreementImage agreementImage, HttpPostedFileBase file) { if (file != null && file.ContentLength > 0) { using (var context = new DatabaseContext()) { var agreementImages = context.AgreementImages .Where(x => x.AgreementId == agreementImage.AgreementId).ToList(); var sequenceNumber = (byte)(agreementImages.Count == 0 ? 1 : agreementImages.Max(x => x.SequenceNumber) + 1); agreementImage.SequenceNumber = sequenceNumber; var bytes = agreementImage.Initialize(file.InputStream, file.ContentLength, file.FileName); context.AgreementImages.Add(agreementImage); context.SaveChanges(); var storage = new AgreementImageStorage(); storage.Upload(agreementImage.Id, bytes); } } return RedirectToAction("WithImages", "Agreement", new { id = agreementImage.AgreementId }); }
/// <summary>Adds automatic images to the new agreement</summary> private static void AddAutomaticImages(DatabaseContext context, Agreement agreement) { if (agreement.Type == null) return; var fileInfos = new List<Tuple<AgreementType, byte, int>> { new Tuple<AgreementType, byte, int>(AgreementType.MetricsPassportsIds, 1, 1368708), new Tuple<AgreementType, byte, int>(AgreementType.MetricsPassportsIds, 3, 1204257), new Tuple<AgreementType, byte, int>(AgreementType.MetricsPassportsIds, 4, 1368708), new Tuple<AgreementType, byte, int>(AgreementType.DocumentPackageGathering, 2, 1019502), new Tuple<AgreementType, byte, int>(AgreementType.DocumentPackageGathering, 3, 850639) }; var infos = fileInfos.Where(x => x.Item1 == agreement.Type).ToList(); if (infos.Count == 0) return; var storage = new AgreementImageStorage(); infos.ForEach(info => { using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(string.Format( "O.Danheli.Azure.WebRole.Content.Agreements.{0}.Page{1}.jpg", info.Item1, info.Item2))) { var agreementImage = new AgreementImage { AgreementId = agreement.Id, SequenceNumber = info.Item2 }; var bytes = agreementImage.Initialize(stream, info.Item3, string.Format("Page{0}.jpg", info.Item2)); context.AgreementImages.Add(agreementImage); context.SaveChanges(); storage.Upload(agreementImage.Id, bytes); } }); }