Пример #1
0
        public async Task<IActionResult> New(SaveTopicModel model)
        {
            var vm = new PostViewModel();
            vm.CategoryList = new SelectList(this.CategoryService.All(), "Key", "Name");
            vm.Model = model;

            if (!ModelState.IsValid)
            {
                return this.Notice(Core.Resource.Messages.ModelStateNotValid);
            }

            var result = await this.TopicService.Add(model);

            if (result.Success)
            {
                return this.RedirectToAction("Index", "Topic", new { id = result.Data });
            }
            else
            {
                return this.Notice(result.ErrorMessage);
            }
        }
Пример #2
0
        /// <summary>
        /// 创建主题
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<Result<long>> Add(SaveTopicModel model)
        {
            if (this.CategoryService.Get(model.Category) == null)
            {
                return Result<long>.ErrorResult("版块不存在");
            }

            using (var uw = this.CreateUnitOfWork())
            {
                var entity = new Topic
                {
                    Category = model.Category,
                    Content = model.Content,
                    CreateDate = DateTime.Now,
                    CreateUser = SecurityManager.CurrentUser.ID,
                    Title = model.Title
                };

                await uw.InsertAsync(entity);

                return Result.SuccessResult(entity.ID);
            }
        }
Пример #3
0
        public async Task<IActionResult> Edit(int id, SaveTopicModel model)
        {
            if (!ModelState.IsValid)
            {
                return this.Notice(Core.Resource.Messages.ModelStateNotValid);
            }

            var result = await this.TopicService.Edit(id, model);

            if (result.Success)
            {
                return this.RedirectToAction("Index", "Topic", new { id = id });
            }
            else
            {
                return this.Notice(result.ErrorMessage);
            }
        }
Пример #4
0
        /// <summary>
        /// 编辑主题
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<Result<long>> Edit(long id, SaveTopicModel model)
        {
            if (this.CategoryService.Get(model.Category) == null)
            {
                return Result<long>.ErrorResult("版块不存在");
            }

            using (var uw = this.CreateUnitOfWork())
            {
                var entity = await uw.GetAsync<Topic>(t => t.ID == id);

                if (entity == null)
                {
                    return Result<long>.ErrorResult("主题不存在");
                }
                if (!this.SecurityManager.CanOperateTopic(entity))
                {
                    return Result<long>.ErrorResult("无权操作");
                }

                entity.Title = model.Title;
                entity.Category = model.Category;
                entity.Content = model.Content;
                entity.UpdateDate = DateTime.Now;

                await uw.UpdateAsync(entity);

                return Result.SuccessResult(entity.ID);
            }
        }