Ejemplo n.º 1
0
        public async Task <ChatGroupDTO> GetByUuidAsync(Guid uuid)
        {
            ChatGroup chatGroup = await _chatGroupRepository.GetByUuidAsync(uuid);

            ChatGroupDTO chatGroupDTO = new ChatGroupDTO()
            {
                Uuid          = chatGroup.Uuid,
                ChatGroupName = chatGroup.ChatGroupName
            };

            return(chatGroupDTO);
        }
Ejemplo n.º 2
0
        public async Task <ChatGroupDTO> GetAllChatAsync()
        {
            ChatGroup allChat = await _chatGroupRepository.GetAllChatAsync();

            ChatGroupDTO chatGroupDTO = new ChatGroupDTO();

            if (allChat != null)
            {
                chatGroupDTO.Uuid          = allChat.Uuid;
                chatGroupDTO.ChatGroupName = allChat.ChatGroupName;
            }
            return(chatGroupDTO);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        public async Task <ActionResult <RegisterResponse> > Register([FromBody] RegisterModel model)
        {
            User userExists = await _userManager.FindByEmailAsync(model.Email);

            if (userExists != null)
            {
                return(BadRequest(new RegisterResponse {
                    FailureMessage = "User already exists!"
                }));
            }

            User user = new User()
            {
                Email         = model.Email,
                SecurityStamp = Guid.NewGuid().ToString(),
                UserName      = model.UserName
            };

            IdentityResult createUserRes = await _userManager.CreateAsync(user, model.Password);

            if (!createUserRes.Succeeded)
            {
                return(BadRequest(new RegisterResponse {
                    ErrorList = createUserRes.Errors.ToList()
                }));
            }

            IdentityResult addRoleRes = await _userManager.AddToRoleAsync(user, Role.AuthenticatedUser);

            if (!addRoleRes.Succeeded)
            {
                return(BadRequest(new RegisterResponse {
                    ErrorList = createUserRes.Errors.ToList()
                }));
            }

            ChatGroupDTO allChat = await _chatGroupService.GetAllChatAsync();

            //Add user to all chat
            await _chatGroupUserService.AddUserToChatGroupAsync(user, allChat.Uuid);

            JwtSecurityToken token = await GetToken(user);

            return(Ok(new RegisterResponse
            {
                Token = new JwtSecurityTokenHandler().WriteToken(token),
                Expiration = token.ValidTo
            }));
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
 public ChatGroupData(ChatGroupDTO chatGroup)
 {
     ChatGroup = chatGroup;
     Users     = new List <Guid>();
     Messages  = new List <ChatMessageDTO>();
 }