public async Task AddContactAsync(string contactId, string contactName) { var url = ChatRootUrl + PrivateChatRoute + ConnectedUser.Id + "/new"; using var client = new HttpClient(); using var request = new HttpRequestMessage(HttpMethod.Post, url); var content = new NewContactForm(ConnectedUser.Username, contactId, contactName); var jsonContent = JsonConvert.SerializeObject(content); using var stringContent = new StringContent(jsonContent, Encoding.UTF8, "application/json"); request.Content = stringContent; using var response = await client.SendAsync(request).ConfigureAwait(false); var debug = await response.Content.ReadAsStringAsync(); await UpdateUserInfoAsync(); }
public void open(Main main) { NewContactForm addNew = new NewContactForm(main.Pb, main); addNew.ShowDialog(); }
public async Task <ActionResult <Contact> > CreateContact(string userId, [FromBody] NewContactForm newContact) { if (!ModelState.IsValid) { return(BadRequest(new { message = "Contact form is not valid" })); } var contactValid = await accountRepository.GetUser(newContact.ContactId); if (contactValid == null) { return(BadRequest(new { message = "Cannot find the contact in the database" })); } var senderDocument = await accountRepository.GetUser(userId); if (senderDocument == null) { return(BadRequest(new { message = "Cannot find the user in the database" })); } if (senderDocument.Contacts.Any(c => c.ContactId == newContact.ContactId)) { return(BadRequest(new { message = "Cannot add a contact twice" })); } var senderConnection = await userConnectedRepository.GetConnectedUserByUserId(userId); if (senderConnection == null) { return(BadRequest(new { message = "Verify your connection to the SignalR Hub" })); } var senderConnectionId = senderConnection.ConnectionId; var senderUpdatedDocument = await userRepository.AddContactToUser(userId, newContact.ContactId, newContact.ContactName); if (senderUpdatedDocument == null) { return(NotFound(new { message = "Contact was not added to the database" })); } var newSenderContact = senderUpdatedDocument.Contacts.Find(c => c.ContactId == newContact.ContactId); if (newSenderContact == null) { return(NotFound(new { message = "Couldn't find the new contact in the database" })); } var receiverUpdatedDocument = await userRepository.AddContactToUser(newContact.ContactId, userId, newContact.Username); if (receiverUpdatedDocument == null) { return(NotFound(new { message = "Contact was not added to the database" })); } var newReceiverContact = receiverUpdatedDocument.Contacts.Find(c => c.ContactId == userId); if (newReceiverContact == null) { return(NotFound(new { message = "Couldn't find the new contact in the database" })); } var receiverConnection = await userConnectedRepository.GetConnectedUserByUserId(newContact.ContactId); if (receiverConnection == null) { await hubContext.Clients.Client(senderConnectionId).SendAsync(KeyConstants.addedContactOffline, newSenderContact); return(CreatedAtRoute("GetContact", new { userId = userId.ToString(), contactId = newSenderContact.ContactId }, newSenderContact)); } var receiverConnectionId = receiverConnection.ConnectionId; await hubContext.Clients.Client(senderConnectionId).SendAsync(KeyConstants.receiveNewContact, newSenderContact); await hubContext.Clients.Client(receiverConnectionId).SendAsync(KeyConstants.receiveNewContact, newReceiverContact); return(CreatedAtRoute("GetContact", new { userId = userId.ToString(), contactId = newSenderContact.ContactId }, newSenderContact)); }