public async Task <IActionResult> AddMessage(TopicShowViewModel model) { if (ModelState.IsValid) { User user = await _userManager.FindByNameAsync(User.Identity.Name); Topic topic = _db.Topics.FirstOrDefault(t => t.Id == model.Id); Message msg = new Message { Text = model.Text, UserId = user.Id, TopicId = topic.Id, Published = DateTime.UtcNow, }; topic.MessageCount++; user.MessageCount++; await _db.Messages.AddAsync(msg); await _db.SaveChangesAsync(); _logger.LogInformation("To topic {0} user {1} has added message with id {2}", topic.Id, user.UserName, msg.Id); return(RedirectToAction("Show", "Topic", new { id = topic.Id })); } else { return(View("~/Views/Topic/Show.cshtml", model)); } }
public ActionResult ShowTopic(int id) { topic t = gt.getByIdWithComments(id); // List<comment> comments = gc.FindByCondition(e => e.topic.id == id).ToList(); // t.comments = comments; TopicShowViewModel tsvm = new TopicShowViewModel { topic = t, }; return(View("ShowTopic", tsvm)); }
public ActionResult AddCommentToTopic(TopicShowViewModel tsvp) { comment c = new comment(); /* c.topic = gt.FindById(tsvp.topic.id); * c.user = SessionPersister.User;*/ c.Topic_ID = tsvp.topic.id; c.User_ID = SessionPersister.User.id; c.body = tsvp.newComment; gc.Create(c); gc.Commit(); return(RedirectToAction("ShowTopic", "Topic", new { id = tsvp.topic.id })); }