public IActionResult Index() { List<RoomDto> rooms = roomService.RetrieveAll().Data.ToList(); byte[] userNameArray; bool userNameAvailable = HttpContext.Session.TryGetValue("User", out userNameArray); if(userNameAvailable) { UserDto user = (UserDto)userNameArray.ToObject(); } return View("Index", rooms); }
public IActionResult Index(Guid Id) { byte[] userArray; bool userAvailable = HttpContext.Session.TryGetValue("User", out userArray); UserDto user = null; if (userAvailable) { user = (UserDto)userArray.ToObject(); } RoomDto room = _roomService.RetrieveAll().Data.ToList().Where(x => x.Id == Id).FirstOrDefault(); if (room != null) { PagedResult <UserRoomDto> userRooms = _userRoomService.RetrieveAll(x => x.UserID == user.Id && x.RoomID == room.Id); UserRoomDto userRoom = userRooms != null && userRooms.Paging.Total > 0 ? userRooms.Data.ElementAt(0) : null; if (userRooms == null || userRooms.Paging.Total < 1) { userRoom = new UserRoomDto() { UserID = user.Id, RoomID = room.Id, JoinedAt = DateTime.Now, }; _userRoomService.Create(userRoom); } PagedResult <ChatMessageDto> roomChats = _chatService.RetrieveAll(x => x.RoomID == room.Id && x.CreatedAt >= userRoom.JoinedAt); if (roomChats != null && roomChats.Paging.Total > 0) { foreach (ChatMessageDto mess in roomChats.Data) { mess.User = _userService.Retrieve(mess.UserID); } } HttpContext.Session.Set("Room", room.ToByteArray()); ChatViewModel vm = new ChatViewModel() { UserID = userRoom.UserID, RoomID = room.Id, Chats = roomChats.Data.ToList() }; return(View("Index", vm)); } else { return(Redirect("/Rooms/Index")); } }