public async Task <UserPointsUpdateDto> UpdatePoints(UserPointsUpdateRequest request, string userId) { var user = _dataContext.Users.FirstOrDefault(x => x.UserId == userId); if (user is null) { return new UserPointsUpdateDto { Errors = new[] { "User not found" } } } ; if (request.Action.Equals("reduce")) { if (user.Points - request.Points < 0) { return new UserPointsUpdateDto { Errors = new[] { "User don't have enough points" } } } ; user.Points -= request.Points; await _dataContext.SaveChangesAsync(); return(new UserPointsUpdateDto { Success = true }); } else if (request.Action.Equals("add")) { user.Points += request.Points; await _dataContext.SaveChangesAsync(); return(new UserPointsUpdateDto { Success = true }); } else { return(new UserPointsUpdateDto { Errors = new[] { "Action should be \"add\" or \"reduce\" only" } }); } }
public async Task <IActionResult> Points([FromBody] UserPointsUpdateRequest request, string userId) { if (request.Action is null || request.Points == default) { return(BadRequest("Please check your request, {action} or {points}, value can't be null")); } var result = await _userService.UpdatePoints(request, userId); if (result.Errors != null && result.Errors.Contains("User not found")) { return(NotFound(string.Join(", ", result.Errors))); } if (!result.Success) { return(BadRequest(string.Join(", ", result.Errors))); } return(NoContent()); }