Esempio n. 1
0
        public async Task <IActionResult> SendMessageAsync([FromBody] SendMessageParams messageInfo)
        {
            try
            {
                var contactUser = await userProvider.GetUserWithUserNameAsync(messageInfo.ContactUserName);

                var senderUser = await userProvider.GetCurrentUser();

                if (contactUser == null)
                {
                    return(NotFound(Response <string> .GetError(null, $"There is no user with name: '{messageInfo.ContactUserName}'")));
                }

                var isUserBlocked = userService.IsUserAlreadyBlocked(contactUser, senderUser);
                if (isUserBlocked)
                {
                    return(BadRequest(Response <string> .GetError(null, "User restricted for sending message to contact user")));
                }

                var result = messageService.CreateMessageInfo(messageInfo, senderUser, contactUser);
                if (result != null)
                {
                    return(Ok(Response <CreateMessageInfoResponse> .GetSuccess(result)));
                }
                else
                {
                    return(BadRequest(Response <string> .GetError(null, "Message could not send")));
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, ex.Message);
                return(StatusCode(500, Response <string> .GetError(null, "An error occured")));
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> BlockUserAsync([FromBody] BlockUserParams blockUserParams)
        {
            try
            {
                var blockingUser = await userProvider.GetCurrentUser();

                var blockedUser = await userProvider.GetUserWithEmailAsync(blockUserParams.UserEmail);

                if (blockedUser == null)
                {
                    return(NotFound(Response <string> .GetError(null, $"There is no user with email: '{blockUserParams.UserEmail}'")));
                }

                var isUserAlreadyBlocked = userService.IsUserAlreadyBlocked(blockingUser, blockedUser);
                if (isUserAlreadyBlocked)
                {
                    return(BadRequest(Response <string> .GetError(null, $"User already blocked with email: '{blockUserParams.UserEmail}'")));
                }

                var result = userService.BlockUser(blockingUser, blockedUser);
                if (result != null)
                {
                    return(Ok(Response <BlockUserResponse> .GetSuccess(result)));
                }
                else
                {
                    return(BadRequest(Response <string> .GetError(null, "User could not blocked")));
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, ex.Message);
                return(StatusCode(500, Response <string> .GetError(null, "An error occured")));
            }
        }