Exemple #1
0
        public async Task <IActionResult> Create(TopicCreateOrUpdateViewModel model)
        {
            if (ModelState.IsValid)
            {
                var userId = User.Identity.GetUserId <long>();

                var entity = new Topic()
                {
                    Title   = model.Title,
                    Content = model.Content,
                    // Guid = Guid.NewGuid(),
                    Status        = TopicStatus.Published, // default published
                    PublishedTime = DateTime.Now,
                    TagsId        = model.TagsId,
                    UserId        = userId,
                };

                try
                {
                    await _topicService.AddAsync(entity);

                    return(RedirectToAction(nameof(Details), new { id = entity.Id }));
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }

            PrepareViewModel(model);
            return(View(model));
        }
Exemple #2
0
        public async Task <IActionResult> Edit(TopicCreateOrUpdateViewModel model)
        {
            if (ModelState.IsValid)
            {
                var userId = User.Identity.GetUserId <long>();

                var entity = await _topicService.GetTopicByIdAsync(model.Id, false, false);

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

                entity.Content = model.Content;
                entity.TagsId  = model.TagsId;
                entity.Title   = model.Title;

                entity.PublishedTime        = DateTime.Now;
                entity.LastModificationTime = DateTime.Now;

                try
                {
                    await _topicService.UpdateAsync(entity);

                    return(RedirectToAction(nameof(Details), new { id = entity.Id }));
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }

            PrepareViewModel(model);
            return(View(model));
        }
Exemple #3
0
        public IActionResult Create()
        {
            var model = new TopicCreateOrUpdateViewModel();

            PrepareViewModel(model);

            return(View(model));
        }
Exemple #4
0
 private void PrepareViewModel(TopicCreateOrUpdateViewModel viewModel)
 {
     viewModel.AllTags = _tagsService.GetList();
 }