Exemplo n.º 1
0
        public async Task <Contract> AddNewContract(ContractModel contract)
        {
            contract.StartDate = DateInDays(contract.StartDate);
            contract.EndDate   = DateInDays(contract.EndDate);
            var service = await _context.Services.FindAsync(contract.ServiceId);

            if (service == null)
            {
                throw new DataException($"Service with ID={contract.ServiceId} not found!");
            }

            var customer = await _context.Customers.FindAsync(contract.CustomerId);

            if (customer == null)
            {
                throw new DataException($"Customer with ID={contract.CustomerId} not found!");
            }


            var newContract = new Contract
            {
                Id           = Guid.NewGuid(),
                Customer     = customer,
                Service      = service,
                Discount     = contract.Discount,
                StartDate    = contract.StartDate,
                EndDate      = contract.EndDate,
                SpecialPrice = contract.SpecialPrice
            };

            await CheckOverlapingContracts(newContract);

            await _context.AddAsync <Contract>(newContract);

            await _context.SaveChangesAsync();

            return(newContract);
        }