Exemplo n.º 1
0
        public async Task <IActionResult> Block([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 block yourself."));
            }

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

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

            // Insert a new blockedUser.
            try
            {
                var result = await _blockedUserService.BlockUser(blockingUser, blockedUser);

                if (result is default(int))
                {
                    await _activityLogService.LogInvalidBlockedUserActivityAsync(new ActivityLog()
                    {
                        UserId  = blockingUser.Id,
                        Message = string.Format("{0} user not blocked by {1} user"
                                                , blockedUser.Id, blockingUser.Id)
                    });

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

                return(Ok("User not blocked."));
            }

            await _activityLogService.LogBlockedUserActivityAsync(new ActivityLog()
            {
                UserId  = blockingUser.Id,
                Message = string.Format("{0} user blocked by {1} user ✔"
                                        , blockedUser.Id, blockingUser.Id)
            });

            return(Ok("User blocked ✔\nMessages coming from blocked user will not be showed up to you."));
        }
Exemplo n.º 2
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."));
            }
        }