Ejemplo n.º 1
0
        public async Task <IActionResult> Create([FromBody] DistraintCreateModel model)
        {
            if (!ModelState.IsValid)
            {
                string msg = $"{_localizer.GetString("Invalid DistraintCreateModel").Value}!<br />{ModelState.GetErrors()}";
                Log.Information($"DistraintController/Create/{CurrentUserId}/{CurrentUserUsername} - {msg}");
                return(BadRequest(msg));
            }

            try
            {
                var distraint = await _distraintService.AddAsync(model);

                string msg = $"{_localizer.GetString("Create success").Value}! (Id: {distraint.Id})";
                Log.Information($"DistraintController/Create/{CurrentUserId}/{CurrentUserUsername} - {msg}");
                return(Ok(distraint.Id));
            }
            catch (Exception ex)
            {
                Log.Error(ex, $"ERROR creating distraint by user {CurrentUserId}/{CurrentUserUsername}");
                return(BadRequest("Error creating distraint"));
            }
        }
        public static Distraint ToEntity(this DistraintCreateModel model)
        {
            if (model == null)
            {
                return(null);
            }

            DateTime  now    = DateTime.SpecifyKind(DateTime.Now.ToUniversalTime(), DateTimeKind.Utc);
            Distraint entity = new Distraint
            {
                PropertyIdVehicle          = model.PropertyIdVehicle,
                PropertyIdAircraft         = model.PropertyIdAircraft,
                PropertyIdVessel           = model.PropertyIdVessel,
                PropertyIdAgriForMachinery = model.PropertyIdAgriForMachinery,
                PropertyIdRealEstate       = model.PropertyIdRealEstate,
                PropertyIdOtherProperty    = model.PropertyIdOtherProperty,
                PropertyTypeCode           = model.PropertyTypeCode,
                StatusCode      = model.StatusCode,
                InFavourOf      = model.InFavourOf,
                SuitNumber      = model.SuitNumber,
                Debtor          = model.Debtor,
                CreatedOn       = now,
                CreatedBy       = model.CreatedBy,
                Location        = model.Location,
                EnforcementDate = model.EnforcementDate.HasValue == true?DateTime.SpecifyKind(model.EnforcementDate.Value, DateTimeKind.Utc) : default(DateTime?),
                                      EnforcedBy          = model.EnforcedBy,
                                      EnforcedAt          = now,
                                      InFavourOfPersonId  = model.InFavourOfPerson?.Id,
                                      DebtorPersonId      = model.DebtorPerson?.Id,
                                      InFavourOfCompanyId = model.InFavourOfCompany?.Id,
                                      DebtorCompanyId     = model.DebtorCompany?.Id,
                                      IsInFavourOfPerson  = model.IsInFavourOfPerson,
                                      IsDebtorPerson      = model.IsDebtorPerson
            };

            return(entity);
        }
        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;
                }
            }
        }