Пример #1
0
        public async Task <VehicleModel> Add(VehicleModel model)
        {
            var domain = _mapper.Map <Infrastructure.Models.Vehicle>(model);

            _dbContext.Vehicles.Add(domain);
            await _dbContext.SaveChangesAsync();

            return(_mapper.Map <VehicleModel>(domain));
        }
Пример #2
0
        public async Task <CustomerModel> Add(CustomerModel model)
        {
            var domain = _mapper.Map <Infrastructure.Models.Customer>(model);

            _dbContext.Customers.Add(domain);
            await _dbContext.SaveChangesAsync();

            return(_mapper.Map <CustomerModel>(domain));
        }
Пример #3
0
        public async Task <CustomerInsuranceModel> Add(CustomerInsuranceModel model)
        {
            var domain = _mapper.Map <Infrastructure.Models.CustomerInsurance>(model);

            domain.Vehicle = await _dbContext.Vehicles.FirstOrDefaultAsync(x => x.Brand == model.Vehicle.Brand && x.Model == model.Vehicle.Model);

            var customer = await _dbContext.Customers.FirstOrDefaultAsync(x => x.CPF == domain.Customer.CPF);

            if (customer == null)
            {
                _dbContext.Customers.Add(domain.Customer);
            }
            else
            {
                domain.Customer = customer;
            }

            domain.VehicleID  = domain.Vehicle.ID;
            domain.CustomerID = domain.Customer.ID;

            var riskRate       = ((domain.Vehicle.Value * 5) / (domain.Vehicle.Value * 2)) / 100;
            var riskValue      = riskRate * domain.Vehicle.Value;
            var pureInsurance  = riskValue * (1 + _settingConfiguration.SafetyMargin);
            var insuranceValue = pureInsurance + (pureInsurance * _settingConfiguration.Profit);

            domain.InsuredValue = insuranceValue;

            var existingDomain = await _dbContext.CustomersInsurance.FirstOrDefaultAsync(x => x.VehiclePlate == domain.VehiclePlate && x.CustomerID == domain.CustomerID && x.VehicleID == domain.VehicleID);

            if (existingDomain != null)
            {
                domain = existingDomain;
                domain.InsuredValue = insuranceValue;
                _dbContext.CustomersInsurance.Update(domain);
            }
            else
            {
                _dbContext.CustomersInsurance.Add(domain);
            }

            await _dbContext.SaveChangesAsync();

            return(_mapper.Map <CustomerInsuranceModel>(domain));
        }