Ejemplo n.º 1
0
        public AddDonationResult AddDonation(string toUserName, string fromUserId, decimal total, string targetCode)
        {
            var userId = _applicationUserDataService.Value.Query().Where(x => x.UserName.ToLower() == toUserName.ToLower()).Select(x => x.Id).FirstOrDefault();
            var result = new AddDonationResult();

            if (userId == null)
            {
                result.Errors.Add("Check username/target code");
            }
            else
            {
                AddDonationForUserId(userId, fromUserId, total, targetCode, result);
            }
            return(result);
        }
Ejemplo n.º 2
0
        private AddDonationResult AddDonationForUserId(string userId, string fromUserId, decimal total, string targetCode, AddDonationResult addDonationResult = null)
        {
            const string CommonErrorMessage = "Please, check username or target code.";

            var result = addDonationResult ?? new AddDonationResult();

            result.Status = Common.Enums.OperationResultType.Failed;
            if (userId != fromUserId)
            {
                var fromUserExists = _applicationUserDataService.Value.Query().Any(x => x.Id == fromUserId);
                if (fromUserExists)
                {
                    var target = _userTargetDataService.Value.Query().FirstOrDefault(x => x.ApplicationUserId == userId &&
                                                                                     x.Code == targetCode);
                    if (target != null)
                    {
                        _userTargetDonationDataService.Value.Create(new Entities.UserTargetDonation
                        {
                            DonationFromUserId = fromUserId,
                            DonationTotal      = total,
                            UserTargetId       = target.Id
                        });
                        _unitOfWork.Value.SaveChanges();
                        result.Status = Common.Enums.OperationResultType.Success;
                        result.WidgetNotificationResult = new WidgetNotificationResult
                        {
                            Donation = total,
                            Code     = target.Code,
                        };
                        result.TargetUserId = target.ApplicationUserId;
                        result.WalletDestintaionUserNotificationResult = new WalletBalanceChangedResponse(total, WalletOperationType.Income);
                        result.WalletSenderUserNotificationResult      = new WalletBalanceChangedResponse(total, WalletOperationType.Outcome);
                    }
                }
            }
            else
            {
                result.Errors.Add("You cannot donate to yourself.");
            }

            if (result.Status == Common.Enums.OperationResultType.Failed && !result.Errors.Any())
            {
                result.Errors.Add(CommonErrorMessage);
            }
            return(result);
        }