コード例 #1
0
        public async Task <DistraintCreateModel> AddAsync(DistraintCreateModel model)
        {
            if (model == null)
            {
                throw new NullReferenceException("DistraintCreateModel is null");
            }

            using (var tran = _context.Database.BeginTransaction())
            {
                try
                {
                    var distraint = model.ToEntity();

                    if (model.IsNewProperty)
                    {
                        OtherPropertyModel newProperty = await _propertyService.AddOtherPropertyAsync(model.NewOtherProperty);

                        distraint.PropertyIdOtherProperty = newProperty?.Id;
                    }
                    else if (model.PropertyIdVehicle == -1 && !String.IsNullOrEmpty(model.VehicleProperty.RegistrationNumber))
                    {
                        Vehicle vehicle = await _propertyService.AddOrUpdateVehicleAsync(model.VehicleProperty);

                        distraint.PropertyIdVehicle = vehicle.Id;
                    }
                    else if (model.PropertyIdAircraft == -1 && !String.IsNullOrEmpty(model.AircraftProperty.MsnserialNumber))
                    {
                        Aircraft aircraft = await _propertyService.AddOrUpdateAircraftAsync(model.AircraftProperty);

                        distraint.PropertyIdAircraft = aircraft.Id;
                    }
                    else if (model.PropertyIdVessel == -1 && !String.IsNullOrEmpty(model.VesselProperty.RegistrationData?.RegistrationNumber))
                    {
                        Vessel vessel = await _propertyService.AddOrUpdateVesselAsync(model.VesselProperty);

                        distraint.PropertyIdVessel = vessel.Id;
                    }

                    if (distraint.IsInFavourOfPerson)
                    {
                        if (model.InFavourOfPerson != null && !String.IsNullOrWhiteSpace(model.InFavourOfPerson.Identifier))
                        {
                            RegixPersonModel person = await _personService.AddRegixPersonAsync(model.InFavourOfPerson);

                            distraint.InFavourOfPersonId  = person.Id;
                            distraint.InFavourOfCompany   = null;
                            distraint.InFavourOfCompanyId = null;
                        }
                        else
                        {
                            throw new Exception("In favour of person missing data!");
                        }
                    }
                    else
                    {
                        if (model.InFavourOfCompany != null && !String.IsNullOrWhiteSpace(model.InFavourOfCompany.Uic))
                        {
                            RegixCompanyModel company = await _companyService.AddRegixCompanyAsync(model.InFavourOfCompany);

                            distraint.InFavourOfCompanyId = company.Id;
                            distraint.InFavourOfPerson    = null;
                            distraint.InFavourOfPersonId  = null;
                        }
                        else
                        {
                            throw new Exception("In favour of company missing data!");
                        }
                    }

                    if (distraint.IsDebtorPerson)
                    {
                        if (model.DebtorPerson != null && !String.IsNullOrWhiteSpace(model.DebtorPerson.Identifier))
                        {
                            RegixPersonModel person = await _personService.AddRegixPersonAsync(model.DebtorPerson);

                            distraint.DebtorPersonId  = person.Id;
                            distraint.DebtorCompany   = null;
                            distraint.DebtorCompanyId = null;
                        }
                        else
                        {
                            throw new Exception("Debtor person missing data!");
                        }
                    }
                    else
                    {
                        if (model.DebtorCompany != null && !String.IsNullOrWhiteSpace(model.DebtorCompany.Uic))
                        {
                            RegixCompanyModel company = await _companyService.AddRegixCompanyAsync(model.DebtorCompany);

                            distraint.DebtorCompanyId = company.Id;
                            distraint.DebtorPerson    = null;
                            distraint.DebtorPersonId  = null;
                        }
                        else
                        {
                            throw new Exception("Debtor company missing data!");
                        }
                    }


                    await _context.Distraint.AddAsync(distraint);

                    await _context.SaveChangesAsync();

                    tran.Commit();

                    model.Id = distraint.Id;
                    return(model);
                }
                catch (Exception e)
                {
                    tran.Rollback();
                    throw e;
                }
            }
        }