public ArticlePublishViewModel(ArticlePublish publish, ArticleViewModel article)
 {
     if (publish != null)
     {
         ID      = publish.ID;
         Article = article;
         Status  = publish.Status;
         Message = publish.Message;
     }
 }
 public ArticlePublishViewModel(ArticlePublish publish)
 {
     if (publish != null)
     {
         ID      = publish.ID;
         Article = new ArticleViewModel(publish.Article);
         Status  = publish.Status;
         Message = publish.Message;
     }
 }
        public IActionResult UpdateArticle([FromBody] CreateUpdateArticleRequest request, long id)
        {
            var user = UserService.Get(long.Parse(User.Identity.Name));

            if (user == null)
            {
                return(NotFound(new ResponseModel
                {
                    Success = false,
                    Message = "Пользователь не найден"
                }));
            }

            if (user.Role == UserRole.Client)
            {
                return(BadRequest(new ResponseModel
                {
                    Success = false,
                    Message = "Ошибка доступа"
                }));
            }

            var specialist = SpecialistService.GetSpecialistFromUser(user);

            if (specialist == null)
            {
                return(BadRequest(new ResponseModel
                {
                    Success = false,
                    Message = "Ошибка доступа"
                }));
            }

            var article = ArticleService.Get(id);

            if (article == null)
            {
                return(NotFound(new ResponseModel
                {
                    Success = false,
                    Message = "Статья не найдена"
                }));
            }

            if (article.Author != specialist)
            {
                return(BadRequest(new ResponseModel
                {
                    Success = false,
                    Message = "Ошибка доступа"
                }));
            }

            var previewImage = FileService.Get(request.PreviewImageID);

            if (previewImage == null)
            {
                return(NotFound(new ResponseModel
                {
                    Success = false,
                    Message = "Изображение не найдено"
                }));
            }

            // TODO Обновлять дату при редактировании?
            article.Title            = request.Title;
            article.ShortText        = request.ShortText;
            article.Text             = Encoding.UTF8.GetBytes(request.Text);
            article.Image            = previewImage;
            article.ModerationStatus = ArticleModerationStatus.New;

            ArticleService.Update(article);

            var articlePublish = ArticlePublishService.GetArticlePublish(article);

            if (articlePublish == null)
            {
                articlePublish = new ArticlePublish
                {
                    Article = article,
                    Status  = ArticleModerationStatus.New
                };

                ArticlePublishService.Create(articlePublish);
            }
            else
            {
                articlePublish.Status  = ArticleModerationStatus.New;
                articlePublish.Message = null;
                ArticlePublishService.Update(articlePublish);
            }

            return(Ok(new DataResponse <ArticleViewModel>
            {
                Data = new ArticleViewModel(article)
            }));
        }
        public IActionResult CreateArticle([FromBody] CreateUpdateArticleRequest request)
        {
            var user = UserService.Get(long.Parse(User.Identity.Name));

            if (user == null)
            {
                return(NotFound(new ResponseModel
                {
                    Success = false,
                    Message = "Пользователь не найден"
                }));
            }

            if (user.Role == UserRole.Client)
            {
                return(BadRequest(new ResponseModel
                {
                    Success = false,
                    Message = "Ошибка доступа"
                }));
            }

            if (ArticleService.IsArticleExist(request.Title))
            {
                return(BadRequest(new ResponseModel
                {
                    Success = false,
                    Message = "Статья с таким названием уже существует"
                }));
            }

            var specialist = SpecialistService.GetSpecialistFromUser(user);

            if (specialist == null)
            {
                return(BadRequest(new ResponseModel
                {
                    Success = false,
                    Message = "Ошибка доступа"
                }));
            }

            var previewImage = FileService.Get(request.PreviewImageID);

            if (previewImage == null)
            {
                return(NotFound(new ResponseModel
                {
                    Success = false,
                    Message = "Изображение не найдено"
                }));
            }

            if (request.ShortText.Length > 65535)
            {
                return(BadRequest(new ResponseModel
                {
                    Success = false,
                    Message = "Слишком длинный текст превью"
                }));
            }

            var article = new Article
            {
                Title            = request.Title,
                ShortText        = request.ShortText,
                Text             = Encoding.UTF8.GetBytes(request.Text),
                Image            = previewImage,
                Author           = specialist,
                Date             = DateTime.UtcNow,
                ModerationStatus = ArticleModerationStatus.New
            };

            ArticleService.Create(article);

            var articlePublish = new ArticlePublish
            {
                Article = article,
                Status  = ArticleModerationStatus.New
            };

            ArticlePublishService.Create(articlePublish);

            return(Ok(new DataResponse <ArticleViewModel>
            {
                Data = new ArticleViewModel(article)
            }));
        }