public IActionResult RescueProducts([FromBody] Orders orders) { var userPoints = _pointsService.GetPointsByUserId(orders.UserId); var product = _productsAppService.GetById(orders.ProductId); if (userPoints.TotalPoints < product.RescuePoints) { return(BadRequest("O saldo de pontos é menor que o total de pontos do produto")); } var userExtract = new UserExtract { InitialPoints = userPoints.TotalPoints, PointsBalance = userPoints.TotalPoints - product.RescuePoints, TransactionDate = DateTime.Now.Date, TransactionPoints = product.RescuePoints, TransactionType = Domain.Enums.TransactionTypeEnum.Output, UserId = orders.UserId }; orders.CreatedAt = DateTime.Now.Date; _userExtractService.AddUserExtract(userExtract); _ordersService.AddOrder(orders); return(Ok(orders)); }
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)); }