コード例 #1
0
        public OutputResultDto CreateDonation(CreateDonationInput input)
        {
            var package = _packageRepository.FirstOrDefault(p => p.Id == input.PackageId);

            if (package.Status != Status.Pending)
            {
                return(new OutputResultDto {
                    Message = "Package cannot be paired twice"
                });
            }
            if (_donationRepository.FirstOrDefault(p => p.Id == input.PackageId) != null)
            {
                return(new OutputResultDto {
                    Message = "Package cannot be paired twice"
                });
            }
            var ticket = _ticketRepository.FirstOrDefault(t => t.Id == input.TicketId);

            _donationRepository.Insert(new Donation
            {
                PackageId  = input.PackageId,
                Status     = Status.Pending,
                Amount     = ticket.Amount,
                Date       = Clock.Now,
                FromUserId = package.UserId,
                TicketId   = ticket.Id,
                ToUserId   = ticket.UserId
            });

            ticket.Status     = Status.AwaitingPayment;
            ticket.DonationId = input.PackageId;
            _ticketRepository.Update(ticket);

            if (ticket.Amount < 20000)
            {
                //create a reward donation
                var nextRewardTicket = GetNextRewardTicketToMatch();

                var ticketToMatch = nextRewardTicket.Success ? nextRewardTicket.Data : GetSystemTicket();
                _rewardDonationRepository.Insert(new ReferralRewardDonation
                {
                    PackageId  = input.PackageId,
                    Status     = Status.Pending,
                    Amount     = ticket.Amount,
                    Date       = Clock.Now,
                    FromUserId = package.UserId,
                    TicketId   = ticketToMatch.Id,
                    ToUserId   = ticketToMatch.UserId
                });
            }


            //mark package as paired to avoid multi pairing of a donation
            package.Status = Status.Paired;
            _packageRepository.Update(package);
            return(new OutputResultDto {
                Message = "Donation created for the given ticket and package", Success = true
            });
        }