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.")); }