public async Task <ActionResult> Create(ChatMessage chatMessage) { //var errors = ModelState.Values.SelectMany(v => v.Errors); if (ModelState.IsValid) { int Room; if (!int.TryParse(Request.Form.Get("ChatRoomID"), out Room)) { Json("Error : ChatRoomID must be present"); } chatMessage.ChatRoom = db.ChatRooms.Find(Room); chatMessage.SequenceNumber = db.ChatMessages.Where(s => s.ChatRoom.ChatRoomId == Room).Count() + 1; chatMessage.DateTime = DateTime.UtcNow; chatMessage.User = db.Users.Find(System.Web.HttpContext.Current.User.Identity.GetUserId()); var test = ModelState.IsValidField("ChatMessageID"); db.ChatMessages.Add(chatMessage); await db.SaveChangesAsync(); return(Content("Success :)")); } return(Json("Error :")); }
public async Task UpdateUserGroupLastActivityDateAsync(int groupId, string userId) { var userGroup = await _context.UserGroups.FirstAsync(ug => ug.GroupId == groupId && ug.UserId == userId); userGroup.LastActivityDate = DateTime.UtcNow; await _context.SaveChangesAsync(); }
public async Task <IActionResult> Create([Bind("id,name,created_at")] User user) { if (ModelState.IsValid) { _context.Add(user); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(user)); }
private static async Task SeedGroup(WebChatContext context, List <User> users, string name) { int numberOfUsers = users.Count; Group group = new Group() { Name = name, IsPrivateChat = (numberOfUsers == 2), LastActivityDate = DateTime.UtcNow, UserGroups = new List <UserGroup>() }; var firstChatResultUserGroups = await context.Groups.AddAsync(group); var userGroups = new List <UserGroup>(); for (int i = 0; i < numberOfUsers; i++) { userGroups.Add(new UserGroup() { GroupId = group.Id, UserId = users[i % numberOfUsers].Id }); } context.UserGroups.AddRange(userGroups); await context.SaveChangesAsync(); int userIdIndex = 0; for (int i = 1; i <= 20; i++) { if (i < 10) { userIdIndex = i % numberOfUsers; } else if (i <= 15) { userIdIndex = numberOfUsers - 1; } else { userIdIndex = numberOfUsers - 2; } context.Messages.Add(new Message() { GroupId = group.Id, UserId = users[userIdIndex].Id, Text = $"Message {i}", Date = DateTime.UtcNow }); await context.SaveChangesAsync(); } }
public async Task CreateNewGroupAsyncShould_CreateGroupChatIf3UserIdsAreProvided() { const string databaseName = nameof(CreateNewGroupAsyncShould_CreateGroupChatIf3UserIdsAreProvided); using (var arrangeContext = new WebChatContext(TestUtils.GetOptions(databaseName))) { await arrangeContext.Users.AddAsync(new User { Id = "1", UserName = "******" }); await arrangeContext.Users.AddAsync(new User { Id = "2", UserName = "******" }); await arrangeContext.Users.AddAsync(new User { Id = "3", UserName = "******" }); await arrangeContext.SaveChangesAsync(); } using (var actContext = new WebChatContext(TestUtils.GetOptions(databaseName))) { GroupService sut = new GroupService(actContext); CreateGroupRequest groupRequest = new CreateGroupRequest() { Name = "test", UserIds = { "1", "2", "3" } }; var result = await sut.CreateNewGroupAsync(groupRequest, "1"); Assert.AreEqual("test", result.Name); Assert.AreEqual(false, result.IsPrivateChat); Assert.IsTrue(DateTime.UtcNow - result.LastActivityDate < TimeSpan.FromSeconds(2)); }; }
public async Task SaveGroupMessageAsync_UpdatesGroupLastActivityDate() { const string databaseName = nameof(SaveGroupMessageAsync_UpdatesGroupLastActivityDate); using (var arrangeContext = new WebChatContext(TestUtils.GetOptions(databaseName))) { await arrangeContext.Users.AddAsync(new User { Id = "1", UserName = "******" }); await arrangeContext.Groups.AddAsync(new Group { Id = 1, Name = "Group1" }); await arrangeContext.UserGroups.AddAsync(new UserGroup { UserId = "1", GroupId = 1 }); await arrangeContext.SaveChangesAsync(); } using (var actContext = new WebChatContext(TestUtils.GetOptions(databaseName))) { var sut = new MessageService(actContext); await sut.SaveGroupMessageAsync("1", "Message1", 1); var result = await actContext.Groups.FirstAsync(g => g.Name == "Group1"); Assert.IsTrue(DateTime.UtcNow - result.LastActivityDate < TimeSpan.FromSeconds(2)); } }
public async Task SaveGroupMessageAsync_SaveMessageToGroup() { const string databaseName = nameof(SaveGroupMessageAsync_SaveMessageToGroup); using (var arrangeContext = new WebChatContext(TestUtils.GetOptions(databaseName))) { await arrangeContext.Users.AddAsync(new User { Id = "1", UserName = "******" }); await arrangeContext.Groups.AddAsync(new Group { Id = 1, Name = "Group1" }); await arrangeContext.UserGroups.AddAsync(new UserGroup { UserId = "1", GroupId = 1 }); await arrangeContext.SaveChangesAsync(); } using (var actContext = new WebChatContext(TestUtils.GetOptions(databaseName))) { var sut = new MessageService(actContext); MessageWithUserData result = await sut.SaveGroupMessageAsync("1", "Message1", 1); Assert.AreEqual(1, result.GroupId); Assert.AreEqual("User1", result.UserName); Assert.AreEqual("1", result.UserId); Assert.AreEqual("Message1", result.Text); Assert.IsTrue(DateTime.UtcNow - DateTime.Parse(result.Date) < TimeSpan.FromSeconds(2)); } }
public async Task <MessageViewModel> AddMessage(MessageViewModel message) { Message messageToAdd = this.mappingService.MapMessageViewModelToMessageModel(message); await ctx.Message.AddAsync(messageToAdd); await ctx.SaveChangesAsync(); return(mappingService.MapMessageModelToMessageViewModel(messageToAdd)); }
// GET ALL MESSAGES FROM PARTICULAR CONVERSATION public async Task <IActionResult> Conversation(int ID) { // Current User var UserID = HttpContext.Session.GetInt32("userId"); //var UserID = 1; // CHECK IF CURRENT USER IS IN THE CONVERSATION var checkUserConversation = _context.UserConversations. FirstOrDefault(cuc => cuc.ConversationID == ID && cuc.UserID == UserID); if (checkUserConversation == null) { return(NotFound()); } // UPDATE USER SEEN STATUS IN USERCONVERSATION var ucToUpdate = await _context.UserConversations.FindAsync(checkUserConversation.ID); if (ucToUpdate.UserSeen == 0) { ucToUpdate.UserSeen = 1; if (ucToUpdate == null) { return(NotFound()); } if (await TryUpdateModelAsync <UserConversation>( ucToUpdate, "UserConversation", s => s.UserSeen)) { await _context.SaveChangesAsync(); } } // GET ALL MESSAGES var messagesList = _context.Messages.Where(m => m.ConversationID == ID).ToList(); foreach (var message in messagesList) { var senderID = message.UserID; var senderAvatar = _context.Profiles.FirstOrDefault(p => p.UserID == senderID).UserAvatar; var messageText = message.MessageText; var sentTime = message.SentTime; MessVMs.Add( new MessagesViewModel { ConversationID = ID, SenderID = senderID, SenderAvatar = senderAvatar, SenderMessage = messageText, MessagedTime = sentTime }); } if (messagesList == null) { return(Json(1)); } return(Json(MessVMs)); }
private static async Task SeedPublicGroupAsync(WebChatContext context, List <User> users) { var messages = new List <Message>(); for (int i = 1; i <= 30; i++) { context.Messages.AddRange(new Message() { GroupId = null, UserId = users[(i > 25) ? i % i : 2].Id, Text = $"Public group message {i}", Date = DateTime.UtcNow.AddDays((i > 25) ? 0 : -30 + i) }); await context.SaveChangesAsync(); } }
public async Task <UpdateUserResponse> SaveUserProfilePicture(string userId, string folder, string fileName, byte[] content) { string filePath = Path.Combine(folder, userId, fileName); await _fileService.SaveAsync(filePath, content); // TODO: save profile picture file path in DB$ User user = await _context.Users.FirstAsync(x => x.Id == userId); user.ProfilePicturePath = Path.Combine(userId, fileName); await _context.SaveChangesAsync(); return(new UpdateUserResponse { Success = true, UserInfo = new BasicUserInfo(user), Error = "" }); }
public async Task <GroupWithUsers> CreateNewGroupAsync(CreateGroupRequest createGroupRequest, string currentUserId) { int groupUsersNumber = createGroupRequest.UserIds.Count; if (groupUsersNumber == 2) { //check if this private chat already exists Group sameGroup = await _context.Groups .Include(g => g.UserGroups) .ThenInclude(ug => ug.User) .SingleOrDefaultAsync(g => g.IsPrivateChat && g.UserGroups.Count == 2 && g.UserGroups.Any(ug => ug.UserId == createGroupRequest.UserIds[0]) && g.UserGroups.Any(ug => ug.UserId == createGroupRequest.UserIds[1]) ); if (sameGroup != null) { return(new GroupWithUsers(sameGroup, currentUserId, " ")); } } var newGroup = await _context.Groups.AddAsync(new Group { Name = createGroupRequest.Name, IsPrivateChat = createGroupRequest.UserIds.Count == 2, LastActivityDate = DateTime.UtcNow, UserGroups = createGroupRequest.UserIds.Select(userID => new UserGroup() { UserId = userID, }).ToList() }); await _context.SaveChangesAsync(); var insertedGroup = _context.Groups .Include(x => x.UserGroups).ThenInclude(ug => ug.User) .First(x => x.Id == newGroup.Entity.Id); return(new GroupWithUsers(insertedGroup, currentUserId, " ")); }
public async Task SaveGroupMessageAsync_ThrowsIfUserDoesNotBelongToAGroup() { const string databaseName = nameof(SaveGroupMessageAsync_ThrowsIfUserDoesNotBelongToAGroup); using (var arrangeContext = new WebChatContext(TestUtils.GetOptions(databaseName))) { await arrangeContext.Users.AddAsync(new User { Id = "1", UserName = "******" }); await arrangeContext.Groups.AddAsync(new Group { Id = 1, Name = "Group1" }); await arrangeContext.UserGroups.AddAsync(new UserGroup { UserId = "2", GroupId = 1 }); await arrangeContext.SaveChangesAsync(); } using (var actContext = new WebChatContext(TestUtils.GetOptions(databaseName))) { var sut = new MessageService(actContext); await Assert.ThrowsExceptionAsync <GroupAccessException>(() => sut.SaveGroupMessageAsync("1", "Message1", 1)); } }
public async Task SaveChangesAsync() { await _webChatContext.SaveChangesAsync(); }