Exemple #1
0
        public IActionResult AddUserPoints([FromBody] Points points)
        {
            var user = _userAppService.GetById(points.UserId);


            if (user == null)
            {
                return(NotFound("Usuario não encontrado"));
            }

            var userPoints = _pointsService.GetPointsByUserId(points.UserId);

            if (userPoints == null)
            {
                var userExtracts = new UserExtract
                {
                    InitialPoints     = 0,
                    PointsBalance     = points.TotalPoints,
                    TransactionDate   = DateTime.Now.Date,
                    TransactionPoints = points.TotalPoints,
                    TransactionType   = Domain.Enums.TransactionTypeEnum.Input,
                    UserId            = points.UserId
                };
                _userExtractService.AddUserExtract(userExtracts);
                _pointsService.AddPoints(points);
                return(Ok(points));
            }
            var userExtract = new UserExtract
            {
                InitialPoints     = userPoints.TotalPoints,
                PointsBalance     = points.TotalPoints + userPoints.TotalPoints,
                TransactionDate   = DateTime.Now.Date,
                TransactionPoints = points.TotalPoints,
                TransactionType   = Domain.Enums.TransactionTypeEnum.Input,
                UserId            = points.UserId
            };

            _userExtractService.AddUserExtract(userExtract);
            userPoints.TotalPoints += points.TotalPoints;
            _pointsService.UpdatePoints(userPoints);

            return(Ok(userPoints));
        }