public ChatResponse Create(Guid groupId, string username) { var chat = new Models.Chat { Id = Guid.NewGuid(), GroupId = groupId }; var chatUserAuthorized = new ChatUser(_authorizedUser, chat); _chatUserRepository.Add(chatUserAuthorized); var user = _userRepository.GetUserByUsername(username); var chatUser = new ChatUser(user, chat); _chatUserRepository.Add(chatUser); var chatUserAuthorizedDocument = new Documents.ChatUser(_authorizedUser.Id, chat.Id); var chatUserDocument = new Documents.ChatUser(user.Id, chat.Id); _chatUserStore.AddToBus(chatUserAuthorizedDocument); _chatUserStore.AddToBus(chatUserDocument); var group = _groupRepository.Get(groupId); _chatEventService.AddChatCreatedEvent(group, _authorizedUser, chat); _chatEventService.AddUserAddedToChatEvent(group, _authorizedUser, _authorizedUser, chat); _chatEventService.AddUserAddedToChatEvent(group, _authorizedUser, user, chat); return(_mapper.Map <ChatResponse>(chat)); }
[HttpPost("createChat")]//now work work yet public IActionResult CreateChat(int id, string name, string type) { IActionResult result = new ObjectResult(false); GenericResult createResult = null; var authenticationHeader = Request?.Headers["Authorization"]; var token = authenticationHeader?.FirstOrDefault().Split(' ')[1]; var subject = _jwtFormater.GetSubject(token); var user = _userRepository.GetSingleByUsername(subject); try { var chat = new Chat() { Name = name, DateCreated = DateTime.Now.ToString(), Type = type }; _chatRepository.Add(chat); _chatRepository.Commit(); var cu = new ChatUser() { ChatId = chat.Id, UserId = user.Id }; var cu1 = new ChatUser() { ChatId = chat.Id, UserId = id }; _chatUserRepository.Add(cu); _chatUserRepository.Add(cu1); _chatUserRepository.Commit(); createResult = new GenericResult() { Succeeded = true, Message = chat.Id + "" }; } catch (Exception e) { createResult = new GenericResult() { Succeeded = false, Message = e.Message }; } result = new ObjectResult(createResult); return(result); }
public async void Post([FromBody] int chatId, int userId) { _chatUserRepository.Add(new ChatUser() { UserId = userId, ChatId = chatId }); await Clients.Group(chatId.ToString()).InvokeAsync("AddUser", userId.ToString()); }
public void CreateChat(string name, string guid) { var userElasticResult = _userRepository.Get(guid); if (!userElasticResult.Success) { Clients.Caller.onCreateChatCaller( JsonConvert.SerializeObject(new { error = true, message = UserErrorMessage })); return; } var user = userElasticResult.Value; var chatElasticResult = _chatRepository.Add(name, guid); if (!chatElasticResult.Success) { Clients.Caller.onCreateChatCaller( JsonConvert.SerializeObject(new { error = true, message = chatElasticResult.Message })); return; } var chat = chatElasticResult.Value; // Add User to new Chat var chatUserElasticResult = _chatUserRepository.Add(chat.Guid, user.Guid); if (!chatUserElasticResult.Success) { Clients.Caller.OnCreateChat( JsonConvert.SerializeObject(new { error = true, message = chatUserElasticResult.Message })); return; } // Add all the connection ids of the user to the new chat foreach (var connectionId in user.ConnectionIds) { Groups.Add(connectionId, chat.Guid); } Clients.Clients(user.ConnectionIds.ToArray()).OnCreateChatCaller(JsonConvert.SerializeObject(chat)); }
public void AddUserToChatByUsername(string username, Guid chatId) { var user = _userRepository.GetUserByUsername(username); var chat = _chatRepository.Get(chatId); var chatUser = new ChatUser(user, chat); _chatUserRepository.Add(chatUser); var chatUserDocument = new Documents.ChatUser(user.Id, chatId); _chatUserStore.AddToBus(chatUserDocument); _chatEventService.AddUserAddedToChatEvent(chat.Group, _authorizedUser, user, chat); }
public override void Seed() { if (_chatRepository.Any() || !_userRepository.Any()) { return; } var chats = new[] { new Chat() { Id = Guid.NewGuid(), GroupId = new Guid("c49ff16c-842c-4c13-853c-acea6ee4d28d") }, new Chat() { Id = Guid.NewGuid(), GroupId = new Guid("8f10e5e0-02f6-47cc-84c7-cd4e5b06792f") }, new Chat() { Id = Guid.NewGuid(), GroupId = new Guid("3348dce8-26f0-4da5-b683-f0dedb462d62") } }; var users = _userRepository.GetAll(); foreach (var user in users) { foreach (var chat in chats) { _chatUserRepository.Add(new ChatUser(user, chat)); } } var author = users.First(); foreach (var chat in chats) { var group = _groupRepository.Get(chat.GroupId); _chatEventService.AddChatCreatedEvent(group, author, chat); } }