public async Task <bool> RegisterTransportCard(int id, DiscountRegistrationTypeEnum discountType, string discountId)
        {
            var transportCard = await GetCardDetailById(id);

            if (!string.IsNullOrWhiteSpace(transportCard.DiscountId))
            {
                throw new Exception("Card has already been registered");
            }

            if (transportCard.PurchaseDate.AddMonths(6) < new DateTime())
            {
                throw new Exception("Card already passed the qualified registration date. Card should be registered not later than 6 months upon purchase.");
            }

            var cardFromDiscountId = await _context.TransportCards.FirstOrDefaultAsync(t => t.DiscountId == discountId);

            if (cardFromDiscountId != null)
            {
                throw new Exception("PWD or Senior ID presented is already in used.");
            }

            if (discountType == DiscountRegistrationTypeEnum.PWD)
            {
                if (!_pwdIdFormat.IsMatch(discountId))
                {
                    throw new Exception("Invalid PWD ID Format");
                }
            }
            else
            {
                if (!_seniorIdFormat.IsMatch(discountId))
                {
                    throw new Exception("Invalid Senior ID Format");
                }
            }

            transportCard.DiscountRegistrationType = discountType;
            transportCard.DiscountId = discountId;

            return(await _context.SaveChangesAsync() > 0 ? true : throw new Exception("Registration failed"));
        }
Ejemplo n.º 2
0
 public async Task <bool> RegisterTransportCard(int id, DiscountRegistrationTypeEnum discountType, string discountId)
 {
     return(await _service.RegisterTransportCard(id, discountType, discountId));
 }