Exemplo n.º 1
0
        public ActionResult Create(WallModel model)
        {
            if (ModelState.IsValid)
            {
                using (var db = new MySelfieEntities())
                {
                    try
                        {
                            Wall wall;

                            wall = new Wall();

                            wall.MergeWithOtherType(model);

                            if (model.LogoPath.IsEmptyOrNull()) wall.LogoPath = "";
                            if (model.FrameTopColor.IsEmptyOrNull()) wall.FrameTopColor = "#DDDDDD";
                            if (model.FrameBottomColor.IsEmptyOrNull()) wall.FrameBottomColor = "#FFFFFF";
                            if (model.RetweetMessage.IsEmptyOrNull()) wall.RetweetMessage = "";

                            if (model.CaptionText.IsEmptyOrNull()) wall.CaptionText = "";
                            if (model.DescriptionText.IsEmptyOrNull()) wall.DescriptionText = "";
                            if (model.RightText.IsEmptyOrNull()) wall.RightText = "";
                            if (model.Title.IsEmptyOrNull()) wall.Title = "";

                            if (model.LogoImageFile.HasFile())
                            {
                                wall.LogoImage = model.LogoImageFile.getFileBytes();    // extracts bytes from posted file
                                wall.LogoImageType = model.LogoImageFile.getFileType(); // extracts type from posted file
                            }
                            //Johnm - some mismatches on column naming, thus mergeWithOtherType not working
                            wall.PostingAccount_InstagramToken = model.Post_InstagramToken;

                            wall.IsActive = false;
                            wall.CreatedAt = DateTime.UtcNow;
                            wall.Status = "new";

                            db.Walls.Add(wall);

                            wall.Name = model.Name;

                            db.SaveChanges();
                        }
                    catch (DbEntityValidationException ex)
                    {
                        // Retrieve the error messages as a list of strings.
                        var errorMessages = ex.EntityValidationErrors
                                .SelectMany(x => x.ValidationErrors)
                                .Select(x => x.ErrorMessage);

                        // Join the list to a single string.
                        var fullErrorMessage = string.Join("; ", errorMessages);

                        // Combine the original exception message with the new one.
                        var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                        Logger.Log(exceptionMessage, "error", User.Identity.Name, "POST /Wall/Create");

                        ModelState.AddModelError("ex", ex);

                        return View(model);
                    }
                    catch (Exception ex)
                    {
                        Logger.Log(ex.ToString(), "error", User.Identity.Name, "POST /Wall/Create");

                        ModelState.AddModelError("ex", ex);

                        return View(model);
                    }
                }

                return RedirectToAction("Index", "Wall");
            }

            return View(model);
        }