示例#1
0
        public async Task <ResponseModel> UpdateNewsAsync(Guid id, NewsManageModel newsManageModel)
        {
            var news = await _newsRepository.GetByIdAsync(id);

            if (news == null)
            {
                return(new ResponseModel()
                {
                    StatusCode = System.Net.HttpStatusCode.NotFound,
                    Message = "This news is not exist"
                });
            }
            else
            {
                var existedNewsName = await _newsRepository.FetchFirstAsync(x => x.Title == newsManageModel.Title && x.Id != id);

                if (existedNewsName != null)
                {
                    return(new ResponseModel()
                    {
                        StatusCode = System.Net.HttpStatusCode.BadRequest,
                        Message = "Scientific Report Type " + news.Title + " is exist on system. Please try again!",
                    });
                }
                else
                {
                    newsManageModel.GetNewsFromModel(news);
                    return(await _newsRepository.UpdateAsync(news));
                }
            }
        }
示例#2
0
        public ActionResult PopupEdit(NewsManageModel model, SubmitType submit)
        {
            if (ModelState.IsValid)
            {
                var response = _newsService.SaveNews(model);
                SetResponseMessage(response);
                if (response.Success)
                {
                    switch (submit)
                    {
                    case SubmitType.PopupSave:
                        return(View("CloseFancyBox", new CloseFancyBoxViewModel
                        {
                            IsReload = false,
                            ReturnUrl = string.Empty
                        }));

                    case SubmitType.SaveAndContinueEdit:
                        return(RedirectToAction("PopupEdit", new { id = model.Id }));
                    }
                }
            }
            SetupPopupAction();
            return(View(model));
        }
示例#3
0
        public async Task <ResponseModel> CreateNewsAsync(NewsManageModel newsManageModel)
        {
            var news = await _newsRepository.FetchFirstAsync(x => x.Title == newsManageModel.Title);

            news = new News();
            newsManageModel.GetNewsFromModel(news);
            await _newsRepository.InsertAsync(news);

            return(new ResponseModel
            {
                StatusCode = System.Net.HttpStatusCode.OK,
                Data = new NewsViewModel(news),
            });
        }
示例#4
0
        /// <summary>
        /// Update value for property of model
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResponseModel UpdateNewsData(XEditableModel model)
        {
            var news = GetById(model.Pk);

            if (news != null)
            {
                var property =
                    ReflectionUtilities.GetAllPropertiesOfType(typeof(NewsManageModel))
                    .FirstOrDefault(p => p.Name.Equals(model.Name, StringComparison.CurrentCultureIgnoreCase));
                if (property != null)
                {
                    object value = model.Value.ToType(property, WorkContext.CurrentTimezone);

                    #region Validate

                    var manageModel = new NewsManageModel(news);
                    manageModel.SetProperty(model.Name, value);

                    var validationResults = manageModel.ValidateModel();

                    if (validationResults.Any())
                    {
                        return(new ResponseModel
                        {
                            Success = false,
                            Message = validationResults.BuildValidationMessages()
                        });
                    }

                    #endregion

                    news.SetProperty(model.Name, value);
                    var response = Update(news);
                    return(response.SetMessage(response.Success
                        ? T("News_Message_UpdateNewsInfoSuccessfully")
                        : T("News_Message_UpdateNewsInfoFailure")));
                }
                return(new ResponseModel
                {
                    Success = false,
                    Message = T("News_Message_PropertyNotFound")
                });
            }
            return(new ResponseModel
            {
                Success = false,
                Message = T("News_Message_ObjectNotFound")
            });
        }
示例#5
0
        public ActionResult Create(NewsManageModel model, SubmitType submit)
        {
            if (ModelState.IsValid)
            {
                var response = _newsService.SaveNews(model);
                SetResponseMessage(response);
                if (response.Success)
                {
                    var id = (int)response.Data;
                    switch (submit)
                    {
                    case SubmitType.Save:
                        return(RedirectToAction("Index"));

                    default:
                        return(RedirectToAction("Edit", new { id }));
                    }
                }
            }
            return(View(model));
        }
示例#6
0
        public ActionResult Edit(NewsManageModel model, SubmitTypeEnums submit)
        {
            if (ModelState.IsValid)
            {
                var response = _newsServices.SaveNewsManageModel(model);
                if (response.Success)
                {
                    SetSuccessMessage(response.Message);
                    switch (submit)
                    {
                    case SubmitTypeEnums.Save:
                        return(RedirectToAction("Index"));

                    default:
                        return(RedirectToAction("Edit", new { id = model.Id }));
                    }
                }
                SetErrorMessage(response.Message);
            }
            model.StatusList     = _newsServices.GetStatus();
            model.NewsCategories = _newsCategoryServices.GetNewsCategories(model.Id);
            return(View(model));
        }
示例#7
0
        public ActionResult Edit(NewsManageModel model, string returnUrl, SubmitType submit)
        {
            if (ModelState.IsValid)
            {
                var response = _newsService.SaveNews(model);
                SetResponseMessage(response);
                if (response.Success)
                {
                    switch (submit)
                    {
                    case SubmitType.Save:
                        if (!string.IsNullOrEmpty(returnUrl))
                        {
                            return(Redirect(returnUrl));
                        }
                        return(RedirectToAction("Index"));

                    default:
                        return(RedirectToAction("Edit", new { id = model.Id, returnUrl }));
                    }
                }
            }
            return(View(model));
        }
示例#8
0
        /// <summary>
        /// Save news manage model
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResponseModel SaveNewsManageModel(NewsManageModel model)
        {
            ResponseModel response;
            var           news = GetById(model.Id);

            #region Edit News
            if (news != null)
            {
                news.Title       = model.Title;
                news.Status      = model.Status;
                news.Description = model.Description;
                news.Content     = model.Content;
                news.ImageUrl    = model.ImageUrl;
                news.IsHotNews   = model.IsHotNews;

                var currentCategories = news.NewsNewsCategories.Select(nc => nc.NewsCategoryId).ToList();
                foreach (var id in currentCategories)
                {
                    if (!model.NewsCategoryIds.Contains(id))
                    {
                        _newsNewsCategoryRepository.Delete(id);
                    }
                }
                foreach (var categoryId in model.NewsCategoryIds)
                {
                    if (currentCategories.All(n => n != categoryId))
                    {
                        var newsNewsCategory = new NewsNewsCategory
                        {
                            NewsId         = news.Id,
                            NewsCategoryId = categoryId
                        };
                        _newsNewsCategoryRepository.Insert(newsNewsCategory);
                    }
                }

                //Get page record order
                response = Update(news);
                return(response.SetMessage(response.Success ?
                                           _localizedResourceServices.T("AdminModule:::News:::Messages:::UpdateSuccessfully:::Update news successfully.")
                    : _localizedResourceServices.T("AdminModule:::News:::Messages:::UpdateFailure:::Update news failed. Please try again later.")));
            }
            #endregion

            news = new EntityModel.News
            {
                Title       = model.Title,
                Status      = model.Status,
                Description = model.Description,
                Content     = model.Content,
                ImageUrl    = model.ImageUrl,
                IsHotNews   = model.IsHotNews,
                Updated     = DateTime.Now,
                UpdatedBy   = WorkContext.CurrentUser.Email
            };

            response = Insert(news);
            foreach (var categoryId in model.NewsCategoryIds)
            {
                var newsNewsCategory = new NewsNewsCategory
                {
                    NewsId         = news.Id,
                    NewsCategoryId = categoryId
                };
                _newsNewsCategoryRepository.Insert(newsNewsCategory);
            }
            return(response.SetMessage(response.Success ?
                                       _localizedResourceServices.T("AdminModule:::News:::Messages:::CreateSuccessfully:::Create news successfully.")
                : _localizedResourceServices.T("AdminModule:::News:::Messages:::CreateFailure:::Create news failed. Please try again later.")));
        }
示例#9
0
        public async Task <IActionResult> Update(Guid id, [FromBody] NewsManageModel newsManageModel)
        {
            var response = await _newsService.UpdateNewsAsync(id, newsManageModel);

            return(new CustomActionResult(response));
        }
示例#10
0
        public async Task <IActionResult> Create([FromBody] NewsManageModel newsManageModel)
        {
            var response = await _newsService.CreateNewsAsync(newsManageModel);

            return(new CustomActionResult(response));
        }
示例#11
0
        /// <summary>
        /// Save news manage model
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResponseModel SaveNews(NewsManageModel model)
        {
            ResponseModel response;
            var           news = GetById(model.Id);

            #region Edit News

            if (news != null)
            {
                news.Title     = model.Title;
                news.Status    = model.Status;
                news.Abstract  = model.Abstract;
                news.Content   = model.Content;
                news.ImageUrl  = model.ImageUrl;
                news.IsHotNews = model.IsHotNews;
                news.DateStart = model.DateStart;
                news.DateEnd   = model.DateEnd;

                #region Categories

                var currentCategories = news.NewsNewsCategories.Select(nc => nc.NewsCategoryId).ToList();

                if (model.NewsCategoryIds == null)
                {
                    model.NewsCategoryIds = new List <int>();
                }

                // Remove reference to deleted categories
                var removedCategoryIds = currentCategories.Where(id => !model.NewsCategoryIds.Contains(id));
                _newsNewsCategoryRepository.Delete(news.Id, removedCategoryIds);

                // Add new reference to categories
                var addedCategoryIds = model.NewsCategoryIds.Where(id => !currentCategories.Contains(id));
                _newsNewsCategoryRepository.Insert(news.Id, addedCategoryIds);

                #endregion

                //Get page record order
                response = Update(news);
                return(response.SetMessage(response.Success
                    ? T("News_Message_UpdateSuccessfully")
                    : T("News_Message_UpdateFailure")));
            }

            #endregion

            Mapper.CreateMap <NewsManageModel, Entity.Entities.Models.News>();
            news = Mapper.Map <NewsManageModel, Entity.Entities.Models.News>(model);

            response = Insert(news);

            #region Categories

            if (model.NewsCategoryIds == null)
            {
                model.NewsCategoryIds = new List <int>();
            }

            _newsNewsCategoryRepository.Insert(news.Id, model.NewsCategoryIds);

            #endregion

            return(response.SetMessage(response.Success
                ? T("News_Message_CreateSuccessfully")
                : T("News_Message_CreateFailure")));
        }