public async Task <Result> BlockUserAsync(int userId, BlockUserReq request) { if (!await _dbContext.Users.AnyAsync(x => x.Id == userId)) { return(Result.Failure(new[] { "The user must be delete from system. Pls contact to the manager." })); } // var userWillBlock = await _dbContext.Users.Where(x => x.Username == request.Username).FirstOrDefaultAsync(); if (userWillBlock == null) { return(Result.Failure(new[] { "The user to be blocked does not exist in the system" })); } var blockedUser = new Blocking { BlockedUserId = userWillBlock.Id, ObstructionistUserId = userId, Status = true }; _dbContext.Blockings.Add(blockedUser); var result = await _dbContext.SaveChangesAsync(new CancellationToken()); if (result > 0) { return(Result.Success(new[] { "Successful" })); } else { return(Result.Failure(new[] { "Error" })); } }
public async Task <Result> BlockRemoveAsync(int userId, BlockUserReq request) { if (!await _dbContext.Users.AnyAsync(x => x.Id == userId)) { return(Result.Failure(new[] { "The user must be delete from system. Pls contact to the manager." })); } // var userWillBlock = await _dbContext.Users.Where(x => x.Username == request.Username).FirstOrDefaultAsync(); if (userWillBlock == null) { return(Result.Failure(new[] { "The user to be blocked does not exist in the system" })); } // var blockedUser = await _dbContext.Blockings.Where(x => !x.IsDeleted && x.ObstructionistUserId == userId && x.BlockedUserId == userWillBlock.Id).FirstOrDefaultAsync(); if (blockedUser == null) { return(Result.Failure(new[] { "There is no user to blocked" })); } // blockedUser.IsDeleted = true; blockedUser.DateOfDelete = DateTime.Now; _dbContext.Blockings.Update(blockedUser); var result = await _dbContext.SaveChangesAsync(new CancellationToken()); if (result > 0) { return(Result.Success(new[] { "Successful" })); } else { return(Result.Failure(new[] { "Error" })); } }
public async Task <IActionResult> BlockRemoval([FromBody] BlockUserReq request) { var response = await _blockingService.BlockRemoveAsync(_currentUser.UserId, request); if (!response.Succeeded) { return(BadRequest(Result.Failure(response.Messages))); } // return(Ok(response)); }