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);
            }
        }
        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());
        }
예제 #3
0
        public void AttachNewsItemToCategory(int newsItemId, int categoryId)
        {
            if (newsItemId == 0)
            {
                throw new Exception("Can't attach newsItem with media with Id '0'");
            }
            //insert newsItem picture only if it doesn't exist
            var insertRequired =
                !_newsItemCategoryRepository.Get(x => x.NewsItemId == newsItemId && x.NewsCategoryId == categoryId).Any();

            if (!insertRequired)
            {
                return;
            }


            var entityMedia = new NewsItemCategory()
            {
                NewsItemId     = newsItemId,
                NewsCategoryId = categoryId,
                DisplayOrder   = 0
            };

            _newsItemCategoryRepository.Insert(entityMedia);
        }
예제 #4
0
        public virtual void DeleteNewsItemCategory(NewsItemCategory newsItemCategory)
        {
            if (newsItemCategory == null)
            {
                throw new ArgumentNullException("newsItemCategory");
            }

            _newsItemCategoryRepository.Delete(newsItemCategory);

            //cache
            _cacheManager.RemoveByPattern(CATEGORIES_PATTERN_KEY);
            _cacheManager.RemoveByPattern(NEWSITEMCATEGORIES_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityDeleted(newsItemCategory);
        }
예제 #5
0
        public ActionResult NewsItemCategoryInsert(GridCommand command, NewsItemModel.NewsItemCategoryModel model)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageCatalog))
            {
                return(AccessDeniedView());
            }

            var newsItemCategory = new NewsItemCategory()
            {
                NewsItemId = model.NewsItemId,
                CategoryId = Int32.Parse(model.Category), //use Category property (not CategoryId) because appropriate property is stored in it
                //DisplayOrder = model.DisplayOrder
                CreatedOnUtc = DateTime.UtcNow
            };

            _categoryService.InsertNewsItemCategory(newsItemCategory);

            return(NewsItemCategoryList(command, model.NewsItemId));
        }
예제 #6
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));
        }
예제 #7
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));
        }
        public IActionResult Put(NewsItemModel entityModel)
        {
            #region validate
            //if (!ModelState.IsValid)
            //    return BadRequest();
            if (entityModel == null)
            {
                VerboseReporter.ReportError("Dữ liệu không hợp lệ.");
                return(RespondFailure());
            }
            DateTime publishDt = entityModel.StartDateStr.ToDateTime();

            //get
            var article = _newsItemService.Get(entityModel.Id,
                                               x => x.NewsItemCategories,
                                               x => x.NewsItemTags);
            if (article == null)
            {
                VerboseReporter.ReportError("Không tìm thấy tin này", "post");
                return(RespondFailure());
            }
            #endregion

            #region NewsItem
            article.Name  = entityModel.Name;
            article.Short = entityModel.Short.GetSubString(ConstantKey.SummaryMaxLength);

            article.Full = HttpUtility.HtmlDecode(entityModel.Full);

            article.StartDateUtc = publishDt;
            article.EndDateUtc   = entityModel.EndDateUtc ?? publishDt.AddDays(7);
            article.DisplayOrder = entityModel.DisplayOrder;
            article.Published    = entityModel.Published;
            article.UpdatedOnUtc = DateTime.Now;
            article.Version      = article.Version + 1;

            article.MetaTitle       = entityModel.MetaTitle ?? article.Name;
            article.MetaDescription = entityModel.MetaDescription ?? article.Short;
            article.MetaKeywords    = entityModel.MetaKeywords ?? article.Name;
            //save it
            _newsItemService.Update(article);

            VerboseReporter.ReportSuccess("Sửa tin tức thành công", "put");
            #endregion


            #region newsCategory
            if (entityModel.newsCategoryIds != null && entityModel.newsCategoryIds.Length > 0)
            {
                foreach (int newsCategoryId in entityModel.newsCategoryIds)
                {
                    // tim chuyen muc
                    var foundNewsCategory = article.NewsItemCategories.FirstOrDefault(x => x.NewsCategoryId == newsCategoryId);
                    // neu khong tim thay chuyen muc trong bang quan he thi them moi
                    if (foundNewsCategory == null)
                    {
                        NewsItemCategory newsItemCategory = new NewsItemCategory()
                        {
                            NewsItemId     = article.Id,
                            NewsCategoryId = newsCategoryId,
                        };
                        _newsPubsService.Insert(newsItemCategory);
                    }
                    //    foreach (var newsPub in article.NewsPubs)
                    //    {
                    //        if (Array.IndexOf(entityModel.newsCategoryIds, newsPub.cat_id) == -1)
                    //        {
                    //            NewsPubs newsPubs = new NewsPubs()
                    //            {
                    //                article_id = article.id,
                    //                cat_id = newsCategoryId,
                    //            };
                    //            if (article.news_status != (int) NewsItemStatus.Unpublish)
                    //            {
                    //                newsPubs.approve_date = DateTime.Now;
                    //                newsPubs.approver = CurrentUser.full_name;
                    //            }
                    //            article.NewsPubs.Add(newsPubs);
                    //        }
                    //    }
                }
                //_NewsItemService.Update(article);
                // find list newsPubs by article_id
                var newsItemCategories = _newsPubsService.Get(x => x.NewsCategoryId == entityModel.Id).ToList();
                foreach (var newsItemCategory in newsItemCategories)
                {
                    // tim chuyen muc
                    var foundNewsCategory = entityModel.newsCategoryIds.FirstOrDefault(x => x == newsItemCategory.NewsCategoryId);
                    // neu khong tim thay chuyen muc trong mang danh muc gui len thi xoa di
                    if (foundNewsCategory == 0)
                    {
                        _newsPubsService.Delete(newsItemCategory);
                    }
                }
            }
            #endregion

            #region TagIds

            if (entityModel.TagIds != null && entityModel.TagIds.Length > 0)
            {
                foreach (var entityModelTagId in entityModel.TagIds)
                {
                    var foundNewsItemTag =
                        article.NewsItemTags.FirstOrDefault(x => x.TagId == entityModelTagId);
                    if (foundNewsItemTag == null)
                    {
                        NewsItemTag tag = new NewsItemTag()
                        {
                            TagId      = entityModelTagId,
                            NewsItemId = article.Id
                        };
                        _newsItemTagService.Insert(tag);
                    }
                }

                var newsItemTags = _newsItemTagService.Get(x => x.NewsItemId == entityModel.Id).ToList();
                foreach (var newsItemTag in newsItemTags)
                {
                    var foundNewsItemTag = entityModel.TagIds.FirstOrDefault(x => x == newsItemTag.TagId);
                    if (foundNewsItemTag == 0)
                    {
                        _newsItemTagService.Delete(newsItemTag);
                    }
                }
            }
            else
            {
                var newsItemTags = _newsItemTagService.Get(x => x.NewsItemId == entityModel.Id).ToList();
                foreach (var newsItemTag in newsItemTags)
                {
                    _newsItemTagService.Delete(newsItemTag);
                }
            }

            #endregion

            #region SaveLocalizedValue
            UpdateLocales(article, entityModel);
            #endregion

            return(RespondSuccess(article));
        }
        public IActionResult Post(NewsItemModel entityModel)
        {
            //if (!ModelState.IsValid)
            //    return BadRequest();
            if (entityModel == null)
            {
                VerboseReporter.ReportError("Dữ liệu không hợp lệ.");
                return(RespondFailure());
            }
            DateTime publishDt = entityModel.StartDateStr.ToDateTime();

            #region save NewsItem
            var article = new NewsItem
            {
                Name      = entityModel.Name,
                Short     = entityModel.Short.GetSubString(ConstantKey.SummaryMaxLength),
                Full      = HttpUtility.HtmlDecode(entityModel.Full),
                Published = entityModel.Published,

                CreatedOnUtc = publishDt, // DateTime.Now,

                StartDateUtc = publishDt,
                EndDateUtc   = entityModel.EndDateUtc,

                MetaTitle    = entityModel.MetaTitle ?? entityModel.Name,
                MetaKeywords = entityModel.MetaKeywords ?? entityModel.Name,
            };

            article.MetaDescription = entityModel.MetaDescription ?? article.Short;

            //save it
            _newsItemService.Insert(article);

            VerboseReporter.ReportSuccess("Tạo tin tức thành công", "post");
            #endregion

            #region newsCategoryIds
            if (entityModel.newsCategoryIds != null && entityModel.newsCategoryIds.Length > 0)
            {
                article.NewsItemCategories = new List <NewsItemCategory>();

                foreach (int newsCategoryId in entityModel.newsCategoryIds)
                {
                    NewsItemCategory newsPubs = new NewsItemCategory()
                    {
                        NewsItemId     = article.Id,
                        NewsCategoryId = newsCategoryId,
                    };
                    article.NewsItemCategories.Add(newsPubs);
                }
                _newsItemService.Update(article);
            }
            #endregion

            #region TagIds

            if (entityModel.TagIds != null && entityModel.TagIds.Length > 0)
            {
                article.NewsItemTags = new List <NewsItemTag>();
                foreach (var entityModelTagId in entityModel.TagIds)
                {
                    NewsItemTag newsItemTag = new NewsItemTag()
                    {
                        NewsItemId = article.Id,
                        TagId      = entityModelTagId
                    };
                    article.NewsItemTags.Add(newsItemTag);
                }
                _newsItemService.Update(article);
            }

            #endregion


            InsertLocales(article, entityModel);


            return(RespondSuccess(article));
        }