Example #1
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));
        }
Example #2
0
        public async Task <IActionResult> Post(CustomerInsuranceModel model)
        {
            try
            {
                if (model.Customer == null)
                {
                    return(BadRequest("Segurado é obrigatório"));
                }
                if (model.Customer != null &&
                    (string.IsNullOrEmpty(model.Customer.Name) ||
                     string.IsNullOrEmpty(model.Customer.CPF)))
                {
                    return(BadRequest("Os dados do segurado são obrigatório (Nome e CPF)"));
                }
                if (model.Vehicle == null)
                {
                    return(BadRequest("Veículo é obrigatório"));
                }
                if (model.Vehicle != null &&
                    (string.IsNullOrEmpty(model.Vehicle.Brand) ||
                     string.IsNullOrEmpty(model.Vehicle.Model) ||
                     string.IsNullOrEmpty(model.VehiclePlate)))
                {
                    return(BadRequest("Os dados do veículo são obrigatório (Marca e Modelo)"));
                }
                var isValidVehicle = await _vehicle.GetByBrandModel(model.Vehicle.Brand, model.Vehicle.Model) != null;

                if (!isValidVehicle)
                {
                    return(NotFound("Veículo não encontrado"));
                }

                model = await _customerInsurance.Add(model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                return(BadRequest(ex));
            }

            return(Ok(model));
        }