Ejemplo n.º 1
0
        public ApiResult <Card> BindCardToAccount(string cardNumber, string accountNumber)
        {
            // Basic Validations
            if (string.IsNullOrWhiteSpace(cardNumber))
            {
                return(ApiResult <Card> .UpdateFailed(
                           Constants.ApiResultCode.BadRequest, "Card Number cannot be empty"));
            }

            if (string.IsNullOrWhiteSpace(accountNumber))
            {
                return(ApiResult <Card> .UpdateFailed(
                           Constants.ApiResultCode.BadRequest, "Account Number cannot be empty"));
            }

            // get ref to card
            var cardResult = GetCardbyNumber(cardNumber);

            if (!cardResult.IsSuccessful())
            {
                return(cardResult);
            }
            var card = cardResult.Data;

            // get ref to account
            var account = _dbContext.Set <Account>()
                          .Where(a => a.AccountId == accountNumber)
                          .SingleOrDefault();

            if (account == null)
            {
                return(ApiResult <Card> .UpdateFailed(
                           Constants.ApiResultCode.BadRequest, "Account Number does not exist"));
            }
            ;

            // combine account to card and vice versa
            account.Cards.Add(card);
            card.Accounts.Add(account);

            // update database
            try {
                _dbContext.SaveChanges();
            }
            catch (Exception ex) {
                return(ApiResult <Card> .UpdateFailed(
                           ApiResultCode.InternalServerError, $"Failed to update database: Details: {ex.Message}"));
            }

            return(new ApiResult <Card>()
            {
                Data = card
            });
        }
Ejemplo n.º 2
0
        public IQueryable <Customer> Search(SearchCustomerOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            // SELECT FROM CUSTOMER
            var q = _dbContext.Set <Customer>()
                    .AsQueryable();

            // SELECT FROM CUSTOMER WHERE CustomerId = options.CustomerId
            if (options.CustomerId != null)
            {
                q = q.Where(c => c.CustomerId == options.CustomerId);
            }

            // SELECT FROM CUSTOMER WHERE CustomerId = options.CustomerId
            // AND VatNumber = options.VatNumber
            if (!string.IsNullOrWhiteSpace(options.VatNumber))
            {
                q = q.Where(c => c.VatNumber == options.VatNumber);
            }

            if (!string.IsNullOrWhiteSpace(options.Firstname))
            {
                q = q.Where(c => c.Firstname == options.Firstname);
            }

            if (!string.IsNullOrWhiteSpace(options.Lastname))
            {
                q = q.Where(c => c.Lastname == options.Lastname);
            }

            if (options.CountryCodes.Any())
            {
                q = q.Where(c => options.CountryCodes.Contains(
                                c.CountryCode));
            }

            if (options.TrackResults != null &&
                !options.TrackResults.Value)
            {
                q = q.AsNoTracking();
            }

            if (options.Skip != null)
            {
                q = q.Skip(options.Skip.Value);
            }

            q = q.Take(options.MaxResults ?? 500);

            return(q);
        }
Ejemplo n.º 3
0
        public void Card_Register_Success()
        {
            var customer = new Customer()
            {
                Firstname = "Dimitris",
                Lastname  = "Pnevmatikos",
                VatNumber = "117008855",
                Email     = "*****@*****.**",
                IsActive  = true
            };

            var account = new Account()
            {
                Balance      = 1000M,
                CurrencyCode = "EUR",
                State        = Constants.AccountState.Active,
                AccountId    = "GR123456789121"
            };

            customer.Accounts.Add(account);
            var cardNumber = GenerateCardNumber();

            var card = new Card()
            {
                Active     = true,
                CardNumber = cardNumber,
                CardType   = Constants.CardType.Debit
            };

            account.Cards.Add(card);

            _dbContext.Add(customer);
            _dbContext.SaveChanges();

            var customerFromDb = _dbContext.Set <Customer>()
                                 .Where(c => c.VatNumber == "117008855")
                                 .Include(c => c.Accounts)
                                 .ThenInclude(a => a.Cards)
                                 .SingleOrDefault();

            var customerCard = customerFromDb.Accounts
                               .SelectMany(a => a.Cards)
                               .Where(c => c.CardNumber == cardNumber)
                               .SingleOrDefault();

            Assert.NotNull(customerCard);
            Assert.Equal(Constants.CardType.Debit, customerCard.CardType);
            Assert.True(customerCard.Active);
        }
Ejemplo n.º 4
0
        public IActionResult Index()
        {
            var accounts = _dbContext.Set <Account>()
                           .Include(a => a.Customer)
                           .Select(a => new {
                AccountId   = a.AccountId,
                Description = a.Description,
                Customer    = new {
                    FirstName = a.Customer.Firstname,
                    LastName  = a.Customer.Lastname
                }
            })
                           .ToList();

            return(Json(accounts));
        }
Ejemplo n.º 5
0
        public void Card_Register_Success()
        {
            var customer = _customerTests.RegisterCustomer_Success(
                Constants.Country.GreekCountryCode);

            var account = new Account()
            {
                Balance      = 1000M,
                CurrencyCode = "EUR",
                State        = Constants.AccountState.Active,
                AccountId    = $"GR{DateTimeOffset.Now:ssfffffff}"
            };

            customer.Accounts.Add(account);
            _dbContext.SaveChanges();

            var card = new Card()
            {
                Active     = true,
                CardNumber = $"4111111111111{DateTimeOffset.Now:fff}",
                CardType   = Constants.CardType.Debit
            };

            account.Cards.Add(card);

            _dbContext.Add(card);
            _dbContext.SaveChanges();

            var customerFromDb = _dbContext.Set <Customer>()
                                 .Where(c => c.VatNumber == customer.VatNumber)
                                 .Include(c => c.Accounts)
                                 .ThenInclude(a => a.Cards)
                                 .SingleOrDefault();

            var customerCard = customerFromDb.Accounts
                               .SelectMany(a => a.Cards)
                               .Where(c => c.CardNumber == card.CardNumber)
                               .SingleOrDefault();

            Assert.NotNull(customerCard);
            Assert.Equal(Constants.CardType.Debit, customerCard.CardType);
            Assert.True(customerCard.Active);
        }
Ejemplo n.º 6
0
        public void Card_Checkout_Success()
        {
            var card = _dbContext.Set <Card>()
                       .Where(c => c.Active && c.Expiration > DateTime.Now && c.Accounts.Where(a => a.State == AccountState.Active && a.Balance > 100).Any())
                       .FirstOrDefault();

            Assert.NotNull(card);

            //var account = card.Accounts.Where(a => a.State == Constants.AccountState.Active && a.Balance > 100).SingleOrDefault();
            //Assert.NotNull(account);

            var res = _cards.CheckOut(
                new Services.Options.CheckoutCardOptions {
                CardNumber      = card.CardNumber,
                ExpirationMonth = card.Expiration.Month.ToString(),
                ExpirationYear  = card.Expiration.Year.ToString(),
                Amount          = 100
            }
                );

            Assert.Equal(res.Code, ApiResultCode.Success);
        }