public CardDTO Create(CardDTO card)
 {
     return(mapper.Map <CardDTO>(cardsRepository.Create(mapper.Map <Card>(card))));
 }
Ejemplo n.º 2
0
        public async Task <Unit> Handle(CreateCardCommand request, CancellationToken cancellationToken)
        {
            int customerId = await _userRepository.GetCustomerId();

            int  dispositionId    = 0;
            bool dispositionCheck = false;

            List <Disposition> dispositions = await _dispositionRepository.GetListByCustomerId(customerId);

            List <Disposition> tempDis = dispositions.Where(d => d.AccountId == request.AccountId && d.CustomerId == customerId).ToList();

            if (tempDis.Count > 0)
            {
                foreach (var td in tempDis)
                {
                    if (await _cardRepository.GetSingleByDispositionId(td.DispositionId) == null)
                    {
                        dispositionId    = td.DispositionId;
                        dispositionCheck = true;
                    }
                }

                if (!dispositionCheck)
                {
                    Disposition disposition = new Disposition
                    {
                        CustomerId = customerId,
                        AccountId  = request.AccountId,
                        Type       = tempDis[0].Type
                    };

                    dispositionId = await _dispositionRepository.Create(disposition);
                }
            }
            else
            {
                Disposition disposition = new Disposition
                {
                    CustomerId = customerId,
                    AccountId  = request.AccountId,
                    Type       = tempDis[0].Type
                };

                dispositionId = await _dispositionRepository.Create(disposition);
            }

            Random        rnd       = new Random();
            StringBuilder cvv2      = new StringBuilder();
            StringBuilder ccnumber  = new StringBuilder();
            Card          cardCheck = new Card();

            for (int i = 1; i <= 3; i++)
            {
                cvv2.Append(rnd.Next(0, 10).ToString());
            }

            do
            {
                switch (request.Brand)
                {
                case "MasterCard":
                    ccnumber.Append(rnd.Next(51, 56).ToString());

                    for (int i = 1; i <= 14; i++)
                    {
                        ccnumber.Append(rnd.Next(0, 10).ToString());
                    }

                    break;

                case "Visa":
                    ccnumber.Append("4");

                    for (int i = 1; i <= 15; i++)
                    {
                        ccnumber.Append(rnd.Next(0, 10).ToString());
                    }
                    break;

                default:

                    break;
                }

                cardCheck = await _cardRepository.GetSingleByCcnumber(ccnumber.ToString());
            } while (cardCheck != null);

            Card card = new Card
            {
                DispositionId = dispositionId,
                Type          = request.Type,
                Issued        = DateTime.Today,
                Cctype        = request.Brand,
                Ccnumber      = ccnumber.ToString(),
                Cvv2          = cvv2.ToString(),
                ExpM          = DateTime.Now.Month,
                ExpY          = DateTime.Now.AddYears(3).Year,
                Granted       = false,
                Blocked       = false
            };

            await _cardRepository.Create(card);

            return(Unit.Value);
        }