Exemplo n.º 1
0
 public static ConsultingContext Context()
 {
     if (_context == null)
     {
         lock (syncLock)
         {
             if (_context == null)
             {
                 var optionsBuilder = new DbContextOptionsBuilder <ConsultingContext>();
                 optionsBuilder.UseSqlServer(
                     @"Data Source=DESKTOP-LT88502\FATIMASQL2017;Initial Catalog=Consulting;Integrated Security=True");
                 _context = new ConsultingContext(optionsBuilder.Options);
             }
         }
     }
     return(_context);
 }
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            //ConsultingContext _context = new ConsultingContext();
            ConsultingContext _context = (ConsultingContext)validationContext.GetService(typeof(ConsultingContext));

            var results = new List <ValidationResult>();

            double dbHourlyRate = _context.Consultant
                                  .Where(w => w.ConsultantId == ConsultantId)
                                  .FirstOrDefault().HourlyRate;

            if (_context.Consultant.Where(w => w.ConsultantId == ConsultantId).Any())
            {
                if (0 > HoursWorked || HoursWorked > 24)
                {
                    yield return(new ValidationResult("must be greater than zero and total no more than 24", new[] { nameof(HoursWorked) }));
                }

                if (HourlyRate == 0.0)
                {
                    HourlyRate = dbHourlyRate;
                }
                else if (HourlyRate < 0.0 || HourlyRate > 1.5 * dbHourlyRate)
                {
                    results.Add(new ValidationResult("Hourly rate should be greater than 0 and less than 1.5 Consultant hourly rate!"));
                    yield return(new ValidationResult("Hourly rate should be greater than 0 and less than 1.5 Consultant hourly rate", new[] { nameof(HourlyRate) }));
                }

                TotalChargeBeforeTax = HourlyRate * HoursWorked;

                var customerId   = _context.Contract.Where(w => w.ContractId == ContractId).FirstOrDefault().CustomerId;
                var provinceCode = _context.Customer.Where(w => w.CustomerId == customerId).FirstOrDefault().ProvinceCode;
                var provincdTax  = _context.Province.Where(w => w.ProvinceCode == provinceCode).FirstOrDefault().ProvincialTax;

                ProvincialTax = TotalChargeBeforeTax * provincdTax;
            }

            yield return(ValidationResult.Success);
        }