public IActionResult ViewTheWall() { int?thisUserId = HttpContext.Session.GetInt32("UserId"); if (thisUserId == null) { return(View("Index")); } else { WallWrapper vMod = new WallWrapper(); int oneUserId = (int)HttpContext.Session.GetInt32("UserId"); // This is grabbing all comments and it is showing who the user that created the comment. vMod.AllComments = dbContext.Comments .Include(c => c.User) .ThenInclude(c => c.CreatedComments) .ToList(); // This is grabbing one user and the messages that they created. vMod.User = dbContext.Users .Include(u => u.CreatedMessages) .FirstOrDefault(u => u.UserId == oneUserId); // This is grabbing all the messages and including the comments and users under those messages. vMod.AllMessages = dbContext.Messages .Include(u => u.AllComments) .Include(u => u.User) .ThenInclude(u => u.CreatedMessages) .ToList(); return(View(vMod)); } }
public IActionResult CreateMessage(WallWrapper fromForm) { int thisUserId = (int)HttpContext.Session.GetInt32("UserId"); fromForm.Message.UserId = thisUserId; if (ModelState.IsValid) { dbContext.Add(fromForm.Message); dbContext.SaveChanges(); return(RedirectToAction("ViewTheWall", new { messageId = fromForm.Message.MessageId })); } else { return(View("ViewTheWall")); } }
public IActionResult Wall(int UserId) { WallWrapper WMod = new WallWrapper(); WMod.WallUser = _context.Users .Include(u => u.PostedMessage) .Include(u => u.RepliedComment) .ThenInclude(w => w.User) .FirstOrDefault(u => u.UserId == UserId); WMod.MessagesList = _context.Messages .Include(w => w.MessageCreator) .Include(w => w.Comments) .ToList(); return(View("Wall", WMod)); }
public IActionResult CreateComment(int messageId, WallWrapper fromForm) { int thisUserId = (int)HttpContext.Session.GetInt32("UserId"); fromForm.Comment.UserId = thisUserId; if (ModelState.IsValid) { int oneUserId = (int)thisUserId; // Lines 147 - 148 is assigning fromForm.Comment which is in WallWrapper and grabbing the UserId and setting that to the one in Session. // Also it is grabbing the MessageId and assigning that to the messageId that is being passed through the POST route. // Line 149 is adding the whole WallWrapper model into the database. fromForm.Comment.UserId = oneUserId; fromForm.Comment.MessageId = messageId; dbContext.Add(fromForm.Comment); dbContext.SaveChanges(); return(RedirectToAction("ViewTheWall")); } else { return(View("ViewTheWall")); } }