Пример #1
0
        public async Task <IActionResult> Create([FromBody] ArticleCreateDTO articleCreateDTO)
        {
            var location = GetControllerActionNames();

            try
            {
                _logger.LogInfo(LogMessages.AttemptedToCreate(location));
                if (articleCreateDTO == null)
                {
                    _logger.LogWarn(LogMessages.EmptyRequest(location));
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogWarn(LogMessages.IncompleteData(location));
                    return(BadRequest(ModelState));
                }
                var article   = _mapper.Map <Article>(articleCreateDTO);
                var isSuccess = await _articleRepository.Create(article);

                if (!isSuccess)
                {
                    return(InternalError(LogMessages.CreateFailed(location)));
                }
                _logger.LogInfo(LogMessages.Success(location));
                _logger.LogInfo($"{article}");

                return(Created("Create", new { article }));
            }
            catch (Exception e)
            {
                return(InternalError(LogMessages.InternalError(location, e.Message, e.InnerException)));
            }
        }
Пример #2
0
        public ServiceMessage Create(ArticleCreateDTO articleDTO)
        {
            List <string> errors    = new List <string>();
            bool          succeeded = Validate(articleDTO, errors);

            if (succeeded)
            {
                try
                {
                    CategoryEntity categoryEntity = unitOfWork.Categories.GetByName(articleDTO.CategoryName);
                    if (categoryEntity != null)
                    {
                        ModeratorEntity moderatorEntity = unitOfWork.Moderators.GetByLogin(articleDTO.AuthorLogin);
                        if (moderatorEntity != null)
                        {
                            ArticleEntity articleEntity = new ArticleEntity
                            {
                                Author           = moderatorEntity,
                                Category         = categoryEntity,
                                DateCreated      = DateTime.Now,
                                DateLastModified = DateTime.Now,
                                Header           = articleDTO.Header,
                                PhotoLink        = articleDTO.PhotoLink,
                                ShortDescription = articleDTO.ShortDescription,
                                Text             = articleDTO.Text
                            };

                            unitOfWork.Articles.Add(articleEntity);
                            unitOfWork.Commit();
                        }
                        else
                        {
                            succeeded = false;
                            errors.Add(String.Format("Moderator with login {0} was not found", articleDTO.AuthorLogin));
                        }
                    }
                    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;
                }
            }

            return(new ServiceMessage
            {
                Errors = errors,
                Succeeded = succeeded
            });
        }
Пример #3
0
        public IActionResult CreateArticle(Guid authorId, [FromBody] ArticleCreateDTO articleCreateModel)
        {
            if (articleCreateModel == null)
            {
                return(BadRequest());
            }

            var articleEntity = _mapper.Map <Article>(articleCreateModel);

            _articleRepository.AddArticleForAuthor(authorId, articleEntity);
            if (!_articleRepository.Save())
            {
                throw new Exception("Kayit sirasinda hata olustu");
            }

            return(CreatedAtRoute("GetArticlesForAuthor",
                                  new { authorId = articleEntity.AuthorId },
                                  articleEntity));
        }
Пример #4
0
        private bool Validate(ArticleCreateDTO 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");
            }

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

            return(isValid);
        }
Пример #5
0
        public ActionResult Create(ArticleCreateViewModel model)
        {
            var categoryNames = GetAllCategoryNames();

            model.Categories = ConvertToSelectListItems(categoryNames);
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            string fileName   = String.Format("{0}_{1}{2}", model.CategoryName, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"), System.IO.Path.GetExtension(model.Photo.FileName));
            string serverPath = Server.MapPath("~/Images/Uploads");
            string path       = System.IO.Path.Combine(serverPath, fileName);

            ArticleCreateDTO articleDTO = Mapper.Map <ArticleCreateViewModel, ArticleCreateDTO>(model);

            articleDTO.PhotoLink = path;

            ServiceMessage serviceMessage = articleService.Create(articleDTO);

            if (serviceMessage.Succeeded)
            {
                model.Photo.SaveAs(path);
            }
            else
            {
                AddModelErrors(serviceMessage.Errors);
                return(View(model));
            }

            ModelState.Clear();
            return(View(new ArticleCreateViewModel
            {
                Categories = ConvertToSelectListItems(categoryNames)
            }));
        }
Пример #6
0
 public Task <OpResponse <string> > PublishImmediately(ArticleCreateDTO article)
 {
     return(SaveNewArticle(article, asDraft: false));
 }
Пример #7
0
 public Task <OpResponse <string> > SaveAsDraft(ArticleCreateDTO article)
 {
     return(SaveNewArticle(article, asDraft: true));
 }
Пример #8
0
 public async Task <OpResponse <string> > Publish([FromBody] ArticleCreateDTO dto)
 {
     return(await _articleService.PublishImmediately(dto));
 }
Пример #9
0
 public async Task <OpResponse <string> > Draft([FromBody] ArticleCreateDTO dto)
 {
     return(await _articleService.SaveAsDraft(dto));
 }