예제 #1
0
        public ServiceMessage Edit(ArticleEditDTO articleDTO)
        {
            int    id;
            string decryptedId = encryptor.Decrypt(articleDTO.Id);

            List <string> errors    = new List <string>();
            bool          succeeded = true;

            if (Int32.TryParse(decryptedId, out id))
            {
                if (succeeded = Validate(articleDTO, errors))
                {
                    try
                    {
                        CategoryEntity categoryEntity = unitOfWork.Categories.GetByName(articleDTO.CategoryName);
                        if (categoryEntity != null)
                        {
                            ArticleEntity articleEntity = unitOfWork.Articles.Get(id);
                            if (articleEntity != null)
                            {
                                articleEntity.Category         = categoryEntity;
                                articleEntity.DateLastModified = DateTime.Now;
                                articleEntity.Header           = articleDTO.Header;
                                articleEntity.PhotoLink        = articleDTO.PhotoLink;
                                articleEntity.ShortDescription = articleDTO.ShortDescription;
                                articleEntity.Text             = articleDTO.Text;

                                unitOfWork.Commit();
                            }
                            else
                            {
                                succeeded = false;
                                errors.Add("Article was not found");
                            }
                        }
                        else
                        {
                            succeeded = false;
                            errors.Add(String.Format("Category with name {0} was not found", articleDTO.CategoryName));
                        }
                    }
                    catch (Exception ex)
                    {
                        ExceptionMessageBuilder.FillErrors(ex, errors);
                        succeeded = false;
                    }
                }
            }
            else
            {
                succeeded = false;
                errors.Add("Article was not found");
            }

            return(new ServiceMessage
            {
                Errors = errors,
                Succeeded = succeeded
            });
        }
예제 #2
0
        public DataServiceMessage <ArticleEditDTO> Get(string encryptedId)
        {
            string decryptedId = encryptor.Decrypt(encryptedId);
            int    id;

            List <string>  errors    = new List <string>();
            bool           succeeded = true;
            ArticleEditDTO data      = null;

            if (Int32.TryParse(decryptedId, out id))
            {
                try
                {
                    ArticleEntity articleEntity = unitOfWork.Articles.Get(id);
                    if (articleEntity != null)
                    {
                        data = new ArticleEditDTO
                        {
                            Id               = encryptedId,
                            CategoryName     = articleEntity.Category.Name,
                            Header           = articleEntity.Header,
                            PhotoLink        = articleEntity.PhotoLink,
                            Text             = articleEntity.Text,
                            ShortDescription = articleEntity.ShortDescription
                        };
                    }
                    else
                    {
                        succeeded = false;
                        errors.Add("Article was not found");
                    }
                }
                catch (Exception ex)
                {
                    succeeded = false;
                    ExceptionMessageBuilder.FillErrors(ex, errors);
                }
            }
            else
            {
                succeeded = false;
                errors.Add("Article was not found");
            }

            return(new DataServiceMessage <ArticleEditDTO>
            {
                Errors = errors,
                Succeeded = succeeded,
                Data = data
            });
        }
예제 #3
0
        public ActionResult Edit(ArticleEditViewModel model)
        {
            var categoryNames = GetAllCategoryNames();

            model.Categories = ConvertToSelectListItems(categoryNames);

            bool success = false;

            if (ModelState.IsValid)
            {
                model.Id = HttpUtility.UrlDecode(model.Id);
                ArticleEditDTO articleDTO     = Mapper.Map <ArticleEditViewModel, ArticleEditDTO>(model);
                ServiceMessage serviceMessage = articleService.Edit(articleDTO);
                if (!serviceMessage.Succeeded)
                {
                    AddModelErrors(serviceMessage.Errors);
                }

                success = serviceMessage.Succeeded;
            }

            return(JsonOnFormPost(success, "~/Views/Article/Edit.cshtml", model));
        }
예제 #4
0
        private bool Validate(ArticleEditDTO articleDTO, ICollection <string> errors)
        {
            bool isValid = true;

            if (String.IsNullOrEmpty(articleDTO.Header))
            {
                isValid = false;
                errors.Add("Header cannot be empty");
            }

            if (String.IsNullOrEmpty(articleDTO.Text))
            {
                isValid = false;
                errors.Add("Text cannot be empty");
            }

            if (String.IsNullOrEmpty(articleDTO.ShortDescription))
            {
                isValid = false;
                errors.Add("Description cannot be empty");
            }

            if (String.IsNullOrEmpty(articleDTO.PhotoLink))
            {
                isValid = false;
                errors.Add("A photo must be chosen");
            }

            if (String.IsNullOrEmpty(articleDTO.CategoryName))
            {
                isValid = false;
                errors.Add("Category must be selected");
            }

            return(isValid);
        }