public ChatServiceTest() { var userStore = Mock.Of <IUserStore <User> >(); userManager = new Mock <UserManager <User> >(userStore, null, null, null, null, null, null, null, null); userChatroomRepository = new Mock <IUserChatroomRepository>(); chatroomRepository = new Mock <IChatroomRepository>(); chatroomMessageRepository = new Mock <IChatroomMessageRepository>(); var chatroomMappingProfile = new ChatroomMapping(); var chatroomMessageMappingProfile = new ChatroomMessageMapping(); var configuration = new MapperConfiguration(cfg => cfg.AddProfiles(new List <Profile> { chatroomMappingProfile, chatroomMessageMappingProfile })); mapper = new Mapper(configuration); chatService = new ChatService(userManager.Object, mapper, chatroomRepository.Object, userChatroomRepository.Object, chatroomMessageRepository.Object); user = new User(); chatroom = new Chatroom(); userChatroom = new UserChatroom() { User = user, UserId = user.Id, Chatroom = chatroom, ChatroomId = chatroom.Id }; chatroomMessage = new ChatroomMessage(); }
public async Task <IHttpActionResult> Leave(UserChatroom userChatroom) { bool success = RemoveUserFromChatroom(userChatroom); if (success) { await Hub.Groups.Remove(ConnectionId, userChatroom.Chatroom.PublicName); return(Ok("good")); } return(null); }
public async Task <IHttpActionResult> Join(UserChatroom userChatroom) { bool success = AddUserToChatroom(userChatroom); if (success) { await Hub.Groups.Add(ConnectionId, userChatroom.Chatroom.PublicName); return(Ok("good")); } return(null); }
public bool RemoveUserFromChatroom(UserChatroom userChatroom) { bool success = false; RedisCache Cache = GetCache(); int chatroomFromCache = Cache.Chatrooms.FindIndex(x => x.PublicName == userChatroom.Chatroom.PublicName); if (chatroomFromCache >= 0) { // Remove the user from the chatroom Cache.Chatrooms[chatroomFromCache].UserGuids.RemoveAll(x => x == userChatroom.User.Guid); SetCache(Cache); success = true; } return(success); }
public async Task <UserChatroom> RemoveUserChatroomAsync(UserChatroom userChatroom) { if (userChatroom is null) { return(null); } var removedUserChatroom = context.UserChatroom.Remove(userChatroom); var result = await context.SaveChangesAsync(); if (result < 1) { return(null); } return(removedUserChatroom.Entity); }
public async Task <UserChatroom> AddUserChatroomAsync(UserChatroom userChatroom) { if (userChatroom is null) { return(null); } var entityEntry = await context.UserChatroom.AddAsync(userChatroom); var result = await context.SaveChangesAsync(); if (result < 1) { return(null); } return(entityEntry.Entity); }
public async Task CanAddUserChatroom() { var user = new User() { Email = "*****@*****.**" }; await flyItContext.AddAsync(user); var flight = new Flight() { FlightNo = "FlightNo" }; await flyItContext.AddAsync(flight); var chatroom = new Chatroom() { Flight = flight, FlightId = flight.Id, }; await flyItContext.AddAsync(chatroom); await flyItContext.SaveChangesAsync(); var userChatroom = new UserChatroom() { User = user, UserId = user.Id, Chatroom = chatroom, ChatroomId = chatroom.Id }; var result = await userChatroomRepository.AddUserChatroomAsync(userChatroom); var userChatroomInDatabase = await flyItContext.UserChatroom.SingleOrDefaultAsync(uc => uc.ChatroomId == chatroom.Id && uc.UserId == user.Id); Assert.IsNotNull(result); Assert.IsNotNull(userChatroomInDatabase); Assert.AreEqual(userChatroom.Chatroom, result.Chatroom); Assert.AreEqual(userChatroom.User, result.User); }
public async Task CanRemoveUserChatroom() { var user = new User() { Email = "*****@*****.**" }; await flyItContext.AddAsync(user); var flight = new Flight() { FlightNo = "FlightNo" }; await flyItContext.AddAsync(flight); var chatroom = new Chatroom() { Flight = flight, FlightId = flight.Id, }; await flyItContext.AddAsync(chatroom); var userChatroom = new UserChatroom() { Chatroom = chatroom, User = user, ChatroomId = chatroom.Id, UserId = user.Id }; await flyItContext.AddAsync(userChatroom); await flyItContext.SaveChangesAsync(); var result = await userChatroomRepository.RemoveUserChatroomAsync(userChatroom); var afterRemove = await flyItContext.UserChatroom.ToListAsync(); Assert.IsNotNull(result); Assert.IsTrue(afterRemove.Count < 1); }
public async Task CanGetUserChatroomById() { var user = new User() { Email = "*****@*****.**" }; await flyItContext.AddAsync(user); var flight = new Flight() { FlightNo = "FlightNo" }; await flyItContext.AddAsync(flight); var chatroom = new Chatroom() { Flight = flight, FlightId = flight.Id, }; await flyItContext.AddAsync(chatroom); var userChatroom = new UserChatroom() { Chatroom = chatroom, User = user, ChatroomId = chatroom.Id, UserId = user.Id }; await flyItContext.AddAsync(userChatroom); await flyItContext.SaveChangesAsync(); var result = await userChatroomRepository.GetUserChatroomByIdAsync(chatroom.Id, user.Id); Assert.IsNotNull(result); Assert.AreEqual(userChatroom.Chatroom, result.Chatroom); Assert.AreEqual(userChatroom.User, result.User); }
public bool AddUserToChatroom(UserChatroom userChatroom) { bool success = false; RedisCache cache = GetCache(); int chatroomFromCache = cache.Chatrooms.FindIndex(x => x.PublicName == userChatroom.Chatroom.PublicName); if (chatroomFromCache >= 0) { // If the user isn't in the chatroom if (!cache.Chatrooms[chatroomFromCache].UserGuids.Any(x => x == userChatroom.User.Guid)) { // Add the user's guid to the chatroom cache.Chatrooms[chatroomFromCache].UserGuids.Add(userChatroom.User.Guid); SetCache(cache); success = true; } } return(success); }
public async Task <IHttpActionResult> DeleteUser(UserChatroom userChatroom) { RedisCache cache = GetCache(); int userIndex = GetChatroomIndex(userChatroom.User.Guid); if (userIndex < 0) { return(NotFound()); } bool success = PostUserToChatroom(userChatroom.Chatroom, userChatroom.User, cache); if (!success) { return(NotFound()); } await Hub.Groups.Remove(ConnectionId, userChatroom.Chatroom.Name); return(Ok(userChatroom)); }