public async Task <ChatGroupResponse> CreateChatGroup(ChatGroupDTO chatGroupDTO) { HttpResponseMessage response = await Http.PostAsJsonAsync($"/api/chatgroup", chatGroupDTO); ChatGroupResponse createRes = await response.Content.ReadFromJsonAsync <ChatGroupResponse>(); if (createRes.Success) { // Get new state await GetChatGroupUsersAndMessages(); } return(createRes); }
public async Task <ActionResult <ChatGroupResponse> > CreateChatGroup(ChatGroupDTO chatGroupDTO) { string userEmail = User.FindFirstValue(ClaimTypes.Email); User user = await _userManager.FindByEmailAsync(userEmail); if (user == null) { return(Forbid()); } ChatGroupResponse createRes = await _chatGroupService.CreateChatGroupAsync(chatGroupDTO); if (createRes.Success) { //Add user to the chat group await _chatGroupUserService.AddUserToChatGroupAsync(user, createRes.ChatGroupDTO.Uuid); } return(createRes); }
public async Task <ChatGroupResponse> CreateChatGroupAsync(ChatGroupDTO chatGroupDTO) { ChatGroupResponse chatGroupResponse = new ChatGroupResponse(); ChatGroup chatGroup = await _chatGroupRepository.FindByName(chatGroupDTO.ChatGroupName); if (chatGroup != null) { chatGroupResponse.Success = false; chatGroupResponse.ErrorMessage = $"Chat group with name '{chatGroupDTO.ChatGroupName}' already exists"; return(chatGroupResponse); } try { ChatGroup saveChatGroup = new ChatGroup { ChatGroupName = chatGroupDTO.ChatGroupName, }; //Save the chat group await _chatGroupRepository.AddChatGroupAsync(saveChatGroup); await _unitOfWork.CompleteAsync(); chatGroupDTO.Uuid = saveChatGroup.Uuid; chatGroupDTO.CreatedOn = saveChatGroup.CreatedOn; //ChatMessageDTO messageResource = _mapper.Map<Message, FromMessageDTO>(message); chatGroupResponse.Success = true; chatGroupResponse.ChatGroupDTO = chatGroupDTO; } catch (Exception e) { chatGroupResponse.ErrorMessage = "An error occurred when saving the chat group"; } return(chatGroupResponse); }