Exemple #1
0
        public ActionResult Edit(ModifyMessageModel model)
        {
            var updateRecipients = false;

            ModelState.Remove(model.UrlOnly ? "Text" : "Url");
            ProcessRecipientsCsvUpload csvData = new ProcessRecipientsCsvUpload
            {
                PostedFile = model.UploadedFile,
            };

            if (!csvData.CheckCsvOk()) // check csv upload before modelstate so can process irrespectively
            {
                ModelState.AddModelError("UploadedFile", csvData.Error);
                model.RecipientList = MessageLayer.FieldToRecipientList(model.RecipientsField);
                return(View(model));
            }
            if (csvData.Empty && !model.DeleteRecipients && !ModelState.IsValid)
            {
                model.RecipientList = MessageLayer.FieldToRecipientList(model.RecipientsField);
                return(View(model));
            }
            var msg = Db.CmsMessages.FirstOrDefault(m => m.Id == model.Id);

            if (msg == null)
            {
                return(new HttpNotFoundResult("Message item was not found"));
            }
            var store = MessageLayer.ConvertModifyMessageModelToStoreMessage(model);

            if (!csvData.Empty)
            {
                updateRecipients = true;
                store.Recipients = csvData.Recipients;
            }
            else if (model.DeleteRecipients)
            {
                updateRecipients = true;
                store.Recipients = "";
            }
            else
            {
                store.Recipients = model.RecipientsField;
            }
            try
            {
                MessageLayer.UpdateMessage(Db, store, msg);
            }
            catch (Exception)
            {
                //FIXME: add logging here
                model.RecipientList = MessageLayer.FieldToRecipientList(model.RecipientsField);
                return(View(model));
            }
            return(updateRecipients ? RedirectToAction("Edit", new { id = model.Id }) : RedirectToAction("Index"));
        }
Exemple #2
0
        private ActionResult DoCreate(ModifyMessageModel model)
        {
            ModelState.Remove(model.UrlOnly ? "Text" : "Url");
            ProcessRecipientsCsvUpload csvData = new ProcessRecipientsCsvUpload
            {
                PostedFile = model.UploadedFile,
            };

            if (!csvData.CheckCsvOk()) // check csv upload before modelstate so can process irrespectively
            {
                ModelState.AddModelError("UploadedFile", csvData.Error);
                model.RecipientList = MessageLayer.FieldToRecipientList(model.RecipientsField);
                return(View("Create", model));
            }
            if (csvData.Empty && !ModelState.IsValid)
            {
                model.RecipientList = MessageLayer.FieldToRecipientList(model.RecipientsField);
                return(View("Create", model));
            }
            var store = MessageLayer.ConvertModifyMessageModelToStoreMessage(model);

            store.UserId = User.Identity.GetUserId();
            if (!csvData.Empty)
            {
                store.Recipients = csvData.Recipients;
            }
            //model.Recipients;
            if (!MessageLayer.AddMessage(Db, store))
            {
                model.RecipientList = MessageLayer.FieldToRecipientList(model.RecipientsField);
                return(View("Create", model));
            }
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Edit", new { id = store.NewId }));
            }
            return(RedirectToAction("Index"));
        }