Exemplo n.º 1
0
        public ActionResult Create(CreateEditTopicViewModel viewModel)
        {
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                var cats = _categoryService.GetAllowedEditCategories(UsersRole);
                if (cats.Count > 0)
                {
                    if (ModelState.IsValid)
                    {
                        if (CheckCats(viewModel.Category, cats))
                        {
                            var topic = new Topic();
                            var post  = new Post();

                            topic.Name              = viewModel.Name;
                            topic.Category_Id       = viewModel.Category;
                            topic.IsLocked          = viewModel.IsLocked;
                            topic.IsSticky          = viewModel.IsSticky;
                            topic.MembershipUser_Id = LoggedOnReadOnlyUser.Id;
                            topic.Id = post.Id;

                            post.PostContent       = viewModel.Content;
                            post.MembershipUser_Id = LoggedOnReadOnlyUser.Id;
                            post.Topic_Id          = topic.Id;
                            post.IsTopicStarter    = true;



                            try
                            {
                                _topicServic.Add(topic);
                                _postSevice.Add(post);



                                unitOfWork.Commit();
                            }
                            catch (Exception ex)
                            {
                                LoggingService.Error(ex.Message);
                                unitOfWork.Rollback();
                            }
                        }
                        else
                        {
                            //viewModel.Category = null;
                            //No permission to create a Poll so show a message but create the topic
                            //TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel
                            //{
                            //    Message = LocalizationService.GetResourceString("Errors.NoPermissionCatergory"),
                            //    MessageType = GenericMessages.info
                            //};
                            ModelState.AddModelError(string.Empty, LocalizationService.GetResourceString("Errors.CatergoryMessage"));
                        }
                    }
                    viewModel.Categories = _categoryService.GetBaseSelectListCategories(cats);
                    return(View(viewModel));
                }
                return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.NoPermission")));
            }
        }
Exemplo n.º 2
0
        public ActionResult Create(AdminCreateEditTopicViewModel viewModel)
        {
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                var cats = _categoryService.GetAllowedEditCategories(UsersRole, false);
                if (cats.Count > 0)
                {
                    if (ModelState.IsValid)
                    {
                        if (CheckCats(viewModel.Category, cats))
                        {
                            var topic = new Topic();
                            var post  = new Post();

                            topic.Name              = viewModel.Name;
                            topic.Category_Id       = viewModel.Category;
                            topic.Image             = viewModel.Image;
                            topic.IsLocked          = viewModel.IsLocked;
                            topic.IsSticky          = viewModel.IsSticky;
                            topic.MembershipUser_Id = LoggedOnReadOnlyUser.Id;
                            topic.Post_Id           = post.Id;

                            topic.Intro          = viewModel.Intro;
                            topic.SEOKeyword     = viewModel.SEOKeyword;
                            topic.SEODescription = viewModel.SEODescription;

                            post.PostContent       = viewModel.Content;
                            post.MembershipUser_Id = LoggedOnReadOnlyUser.Id;
                            post.Topic_Id          = topic.Id;
                            post.IsTopicStarter    = true;

                            topic.ShotContent       = string.Concat(StringUtils.ReturnAmountWordsFromString(StringUtils.StripHtmlFromString(post.PostContent), 50), "....");
                            topic.isAutoShotContent = true;
                            var i = topic.ShotContent.Length;
                            if (i > 450)
                            {
                                topic.ShotContent = topic.ShotContent.Substring(0, 440) + "...";
                            }

                            try
                            {
                                _topicServic.Add(topic);
                                _postSevice.Add(post);


                                unitOfWork.Commit();

                                return(RedirectToAction("Edit", new { Id = topic.Id }));
                            }
                            catch (Exception ex)
                            {
                                LoggingService.Error(ex.Message);
                                unitOfWork.Rollback();
                            }
                        }
                        else
                        {
                            //viewModel.Category = null;
                            //No permission to create a Poll so show a message but create the topic
                            //TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel
                            //{
                            //    Message = LocalizationService.GetResourceString("Errors.NoPermissionCatergory"),
                            //    MessageType = GenericMessages.info
                            //};
                            ModelState.AddModelError(string.Empty, LocalizationService.GetResourceString("Errors.CatergoryMessage"));
                        }
                    }
                    viewModel.Categories = _categoryService.GetBaseSelectListCategories(cats);
                    return(View(viewModel));
                }
                return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.NoPermission")));
            }
        }