Example #1
0
        public async Task <IActionResult> Put(long id, NewsItemForm model)
        {
            if (!ModelState.IsValid)
            {
                return(new BadRequestObjectResult(ModelState));
            }

            var newsItem = _newsItemRepository.Query()
                           .Include(x => x.ThumbnailImage)
                           .Include(x => x.Categories)
                           .FirstOrDefault(x => x.Id == id);

            var currentUser = await _workContext.GetCurrentUser();

            newsItem.Name         = model.Name;
            newsItem.SeoTitle     = model.SeoTitle;
            newsItem.ShortContent = model.ShortContent;
            newsItem.FullContent  = model.FullContent;
            newsItem.IsPublished  = model.IsPublished;
            newsItem.UpdatedOn    = DateTimeOffset.Now;
            newsItem.UpdatedBy    = currentUser;

            AddOrDeleteCategories(model, newsItem);

            if (model.ThumbnailImage != null && newsItem.ThumbnailImage != null)
            {
                _mediaService.DeleteMedia(newsItem.ThumbnailImage);
            }

            SaveServiceMedia(model.ThumbnailImage, newsItem);

            _newsItemService.Update(newsItem);

            return(Ok());
        }
Example #2
0
        private void AddOrDeleteCategories(NewsItemForm model, NewsItem newsItem)
        {
            foreach (var categoryId in model.NewsCategoryIds)
            {
                if (newsItem.Categories.Any(x => x.CategoryId == categoryId))
                {
                    continue;
                }

                var newsItemCategory = new NewsItemCategory
                {
                    CategoryId = categoryId
                };
                newsItem.AddNewsItemCategory(newsItemCategory);
            }

            var deletedNewsItemCategories =
                newsItem.Categories.Where(newsItemCategory => !model.NewsCategoryIds.Contains(newsItemCategory.CategoryId))
                .ToList();

            foreach (var deletedNewsItemCategory in deletedNewsItemCategories)
            {
                deletedNewsItemCategory.NewsItem = null;
                newsItem.Categories.Remove(deletedNewsItemCategory);
            }
        }
Example #3
0
        public async Task <IActionResult> Post(NewsItemForm model)
        {
            if (!ModelState.IsValid)
            {
                return(new BadRequestObjectResult(ModelState));
            }

            var currentUser = await _workContext.GetCurrentUser();

            var newsItem = new NewsItem
            {
                Name         = model.Name,
                SeoTitle     = model.SeoTitle,
                ShortContent = model.ShortContent,
                FullContent  = model.FullContent,
                IsPublished  = model.IsPublished,
                CreatedBy    = currentUser
            };

            foreach (var categoryId in model.NewsCategoryIds)
            {
                var newsItemCategory = new NewsItemCategory
                {
                    CategoryId = categoryId
                };
                newsItem.AddNewsItemCategory(newsItemCategory);
            }

            SaveServiceMedia(model.ThumbnailImage, newsItem);

            _newsItemService.Create(newsItem);

            return(Ok());
        }
        public async Task <IActionResult> Get(long id)
        {
            var newsItem = await _newsItemRepository.Query()
                           .Include(x => x.ThumbnailImage)
                           .Include(x => x.Categories)
                           .FirstOrDefaultAsync(x => x.Id == id);

            if (newsItem == null)
            {
                return(NotFound());
            }

            var model = new NewsItemForm()
            {
                Name              = newsItem.Name,
                Id                = newsItem.Id,
                Slug              = newsItem.Slug,
                MetaTitle         = newsItem.MetaTitle,
                MetaKeywords      = newsItem.MetaKeywords,
                MetaDescription   = newsItem.MetaDescription,
                ShortContent      = newsItem.ShortContent,
                FullContent       = newsItem.FullContent,
                IsPublished       = newsItem.IsPublished,
                ThumbnailImageUrl = _mediaService.GetThumbnailUrl(newsItem.ThumbnailImage),
                NewsCategoryIds   = newsItem.Categories.Select(x => x.CategoryId).ToList()
            };

            return(Json(model));
        }
        public async Task <IActionResult> Put(long id, NewsItemForm model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var newsItem = await _newsItemRepository.Query()
                           .Include(x => x.ThumbnailImage)
                           .Include(x => x.Categories)
                           .FirstOrDefaultAsync(x => x.Id == id);

            if (newsItem == null)
            {
                return(NotFound());
            }

            var currentUser = await _workContext.GetCurrentUser();

            newsItem.Name            = model.Name;
            newsItem.Slug            = model.Slug;
            newsItem.MetaTitle       = model.MetaTitle;
            newsItem.MetaKeywords    = model.MetaKeywords;
            newsItem.MetaDescription = model.MetaDescription;
            newsItem.ShortContent    = model.ShortContent;
            newsItem.FullContent     = model.FullContent;
            newsItem.IsPublished     = model.IsPublished;
            newsItem.UpdatedOn       = DateTimeOffset.Now;
            newsItem.UpdatedBy       = currentUser;

            AddOrDeleteCategories(model, newsItem);

            if (model.ThumbnailImage != null && newsItem.ThumbnailImage != null)
            {
                await _mediaService.DeleteMediaAsync(newsItem.ThumbnailImage);
            }

            await SaveServiceMedia(model.ThumbnailImage, newsItem);

            _newsItemService.Update(newsItem);
            return(Accepted());
        }
Example #6
0
        public IActionResult Get(long id)
        {
            var newsItem = _newsItemRepository.Query()
                           .Include(x => x.ThumbnailImage)
                           .Include(x => x.Categories)
                           .FirstOrDefault(x => x.Id == id);

            var model = new NewsItemForm()
            {
                Name              = newsItem.Name,
                Id                = newsItem.Id,
                SeoTitle          = newsItem.SeoTitle,
                ShortContent      = newsItem.ShortContent,
                FullContent       = newsItem.FullContent,
                IsPublished       = newsItem.IsPublished,
                ThumbnailImageUrl = _mediaService.GetThumbnailUrl(newsItem.ThumbnailImage),
                NewsCategoryIds   = newsItem.Categories.Select(x => x.CategoryId).ToList()
            };

            return(Json(model));
        }
Example #7
0
        public async Task <IActionResult> Post(NewsItemForm model)
        {
            if (!ModelState.IsValid)
            {
                return(new BadRequestObjectResult(ModelState));
            }

            var currentUser = await _workContext.GetCurrentUser();

            var newsItem = new NewsItem
            {
                Name = model.Name,
                //Alteração 01/12/2018
                Abrev        = model.Abrev,
                SeoTitle     = model.Slug,
                ShortContent = model.ShortContent,
                FullContent  = model.FullContent,
                //IsPublished = model.IsPublished  - Alteração 02/11/2018
                IsPublished = model.IsPublished,
                CreatedOn   = model.CreatedOn,
                PublishedOn = model.CreatedOn,
                CreatedBy   = currentUser
            };

            foreach (var categoryId in model.NewsCategoryIds)
            {
                var newsItemCategory = new NewsItemCategory
                {
                    CategoryId = categoryId
                };
                newsItem.AddNewsItemCategory(newsItemCategory);
            }

            await SaveServiceMedia(model.ThumbnailImage, newsItem);

            _newsItemService.Create(newsItem);
            return(CreatedAtAction(nameof(Get), new { id = newsItem.Id }, null));
        }
Example #8
0
        public async Task <IActionResult> Post(NewsItemForm model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var currentUser = await _workContext.GetCurrentUser();

            var newsItem = new NewsItem
            {
                Name            = model.Name,
                Slug            = model.Slug,
                MetaTitle       = model.MetaTitle,
                MetaKeywords    = model.MetaKeywords,
                MetaDescription = model.MetaDescription,
                ShortContent    = model.ShortContent,
                FullContent     = model.FullContent,
                IsPublished     = model.IsPublished,
                CreatedBy       = currentUser,
                LatestUpdatedBy = currentUser
            };

            foreach (var categoryId in model.NewsCategoryIds)
            {
                var newsItemCategory = new NewsItemCategory
                {
                    CategoryId = categoryId
                };
                newsItem.AddNewsItemCategory(newsItemCategory);
            }

            await SaveServiceMedia(model.ThumbnailImage, newsItem);

            _newsItemService.Create(newsItem);
            return(CreatedAtAction(nameof(Get), new { id = newsItem.Id }, null));
        }