Esempio n. 1
0
        public async Task <ActionResult> AddNews(string editorId, [FromBody] News news)
        {
            string           tempNewsMainPicture = null;
            List <Paragraph> tempNewsParagraphs  = null;

            if (!string.IsNullOrEmpty(news.MainPicturePath))
            {
                tempNewsMainPicture  = news.MainPicturePath;
                news.MainPicturePath = null;
            }

            foreach (Paragraph p in news.Paragraphs)
            {
                if (!string.IsNullOrEmpty(p.PicturePath))
                {
                    tempNewsParagraphs = news.Paragraphs;
                    news.Paragraphs    = null;
                    break;
                }
            }

            string newsId = await _repository.AddNews(news);

            if (!string.IsNullOrEmpty(tempNewsMainPicture) || tempNewsParagraphs != null)
            {
                if (!string.IsNullOrEmpty(tempNewsMainPicture))
                {
                    news.MainPicturePath = FileManagerService.SaveImageToFile(tempNewsMainPicture, "mainPicture" + news.Id);
                }

                if (tempNewsParagraphs != null)
                {
                    int i = 0;
                    news.Paragraphs = tempNewsParagraphs;
                    foreach (Paragraph p in tempNewsParagraphs)
                    {
                        if (!string.IsNullOrEmpty(p.PicturePath))
                        {
                            news.Paragraphs.ElementAt(i).PicturePath = FileManagerService.SaveImageToFile(p.PicturePath, "paragraphPicture" + i + news.Id);
                        }
                        i++;
                    }
                }

                await _repository.AddNewsPictures(newsId, news.MainPicturePath, news.Paragraphs);
            }

            if (newsId != null && newsId != "")
            {
                await _editorRepository.AddNews(editorId, newsId);
            }

            return(Ok());
        }