/// <summary>GET action of the CREATE functionality</summary>
 /// <returns>View with new object</returns>
 public ActionResult Create(int clientId)
 {
     using (var context = new DatabaseContext())
     {
         var client = context.Clients
             .Include(x => x.Manager)
             .First(x => x.Id == clientId);
         var agreement = new Agreement
         {
             ClientId = clientId
         };
         return View(new Tuple<Manager, Client, Agreement>(client.Manager, client, agreement));
     }
 }
        public ActionResult Create(Agreement agreement)
        {
            using (var context = new DatabaseContext())
            {
                if (!ModelState.IsValid) return View(CreateTuple(context, agreement));
                try
                {
                    context.Entry(agreement).State = EntityState.Added;
                    context.SaveChanges();
                    AddAutomaticImages(context, agreement);

                    return RedirectToAction("WithAgreements", "Client", new { id = agreement.ClientId });
                }
                catch { return View(CreateTuple(context, agreement)); }
            }
        }
        /// <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);
                }
            });
        }
 /// <summary>Creates view model</summary>
 /// <returns>View model</returns>
 private static Tuple<Manager, Client, Agreement> CreateTuple(
     DatabaseContext context, Agreement agreement)
 {
     agreement.Client = context.Clients
         .Include(x => x.Manager)
         .First(x => x.Id == agreement.ClientId);
     return new Tuple<Manager, Client, Agreement>(
         agreement.Client.Manager, agreement.Client, agreement);
 }