public async Task <IActionResult> Reply(Topic model) { User user = await _dbContext.User.SingleOrDefaultAsync(u => u.Name == User.Identity.Name); if (user == null || user.IsLocked) { return(Content("Access Denied")); } Topic rootTopic = await _dbContext.Topic.Include("Forum").SingleOrDefaultAsync(t => t.Id == model.RootTopicId); Topic repliedToTopic = await _dbContext.Topic.SingleOrDefaultAsync(t => t.Id == model.ReplyToTopicId); if (rootTopic == null || rootTopic.IsLocked || rootTopic.Forum.IsLocked || user.IsLocked || rootTopic.Id != repliedToTopic.RootTopicId || rootTopic.ForumId != model.ForumId) { return(RedirectToAction("AccessDenied", "Error")); } if (model.Content == null || model.Content.Length == 0) { return(Content("No content to submit")); } model.Title = ""; model.PostDateTime = DateTime.Now; model.OwnerId = user.Id; model.IsLocked = false; model.ModifiedByUserId = null; model.ModifyDateTime = null; await _dbContext.AddAsync(model); await _dbContext.SaveChangesAsync(); return(RedirectToAction("Read", new { rootTopicId = model.RootTopicId })); }
public async Task <IActionResult> Send(Message model) { User sentFromUser = await _dbContext.User.SingleOrDefaultAsync(u => u.Id == model.FromUserId); if (sentFromUser == null || User.Identity.Name != sentFromUser.Name || sentFromUser.IsLocked) { return(Content("Access Denied")); } User sentToUser = await _dbContext.User.SingleOrDefaultAsync(u => u.Id == model.ToUserId); if (sentToUser == null) { return(Content($"User with id: {model.ToUserId} is not found.")); } if (sentToUser.Id == model.FromUserId) { return(Content("You can't send a self message.")); } if (!ModelState.IsValid) { return(RedirectToAction("Detail", new { user1 = sentFromUser.Id, user2 = sentToUser.Id })); } model.Title = ""; model.SendDateTime = DateTime.Now; model.IsRead = false; await _dbContext.AddAsync(model); await _dbContext.SaveChangesAsync(); return(RedirectToAction("Detail", new { user1 = model.FromUserId, user2 = model.ToUserId })); }