Exemplo n.º 1
0
        public async Task <IActionResult> UnBlock([FromBody] CreateBlockedUserModel model)
        {
            // Get authorized user.
            var blockingUser = await GetCurrentUserAsync();

            if (blockingUser is null ||
                blockingUser is default(User))
            {
                return(Unauthorized("No authorized user found.\nPlease log in by using your credentials."));
            }

            if (model is null)
            {
                return(BadRequest("Model cannot be null."));
            }

            if (string.IsNullOrEmpty(model.BlockedUserName))
            {
                return(BadRequest("BlockedUserName cannot be null."));
            }

            if (string.Equals(model.BlockedUserName, blockingUser.Username))
            {
                return(BadRequest("You cannot un block yourself."));
            }

            var blockedUser = await _userService.GetUserByUsernameAsync(model.BlockedUserName);

            if (blockedUser is null)
            {
                return(NotFound("User not found."));
            }

            // Remove blockedUser.
            try
            {
                var result = await _blockedUserService.GetBlockedUserByBlockingUserIdAsync(blockedUser.Id, blockingUser.Id);

                if (result is null ||
                    result is default(BlockedUser))
                {
                    return(NotFound("Blocked User not found."));
                }

                await _blockedUserService.DeleteBlockedUserAsync(result);

                return(Ok("BlockedUser removed ✔"));
            }
            catch (Exception ex)
            {
                await _logService.LogErrorAsync(new CreateLogModel()
                {
                    UserId    = blockingUser.Id,
                    Title     = "BlockedUser Error",
                    Message   = "Error happened in BlockedUser Controller, UnBlock function",
                    Exception = ex
                });

                return(Ok("BlockedUser not removed."));
            }
        }