public static OtherPropertyModel ToViewModel(this OtherProperty entity)
        {
            if (entity == null)
            {
                return(null);
            }

            OtherPropertyModel model = new OtherPropertyModel
            {
                Id              = entity.Id,
                Identifier      = entity.Identifier,
                Description     = entity.Description,
                Type            = entity.Type,
                IsManuallyAdded = entity.IsManuallyAdded
            };

            return(model);
        }
Пример #2
0
        public async Task <OtherPropertyModel> AddOtherPropertyAsync(OtherPropertyModel model)
        {
            if (model == null)
            {
                throw new NullReferenceException("OtherPropertyModel is null");
            }
            OtherProperty entry = new OtherProperty
            {
                Identifier      = model.Identifier,
                Description     = model.Description,
                Type            = model.Type,
                IsManuallyAdded = model.IsManuallyAdded
                                  // Todo: implement ICreatable, change related props type to non-nullable
            };

            await _context.OtherProperty.AddAsync(entry);

            await _context.SaveChangesAsync();

            return(entry.ToViewModel());
        }
        public async Task <IActionResult> Create([FromBody] OtherPropertyModel model)
        {
            if (!ModelState.IsValid)
            {
                string msg = $"{_localizer.GetString("InvalidModel").Value} {nameof(OtherPropertyModel)}!<br />{ModelState.GetErrors()}";
                Log.Information($"OtherPropertyController/Create/{CurrentUserId}/{CurrentUserUsername} - {msg}");
                return(BadRequest(msg));
            }

            try
            {
                OtherPropertyModel entity = await _service.AddOtherPropertyAsync(model);

                string msg = $"{_localizer.GetString("Create success").Value}! (Id: {entity.Id})";
                Log.Information($"OtherPropertyController/Create/{CurrentUserId}/{CurrentUserUsername} - {msg}");
                return(Ok(entity));
            }
            catch (Exception ex)
            {
                Log.Error(ex, $"ERROR creating otherProperty by user {CurrentUserId}/{CurrentUserUsername}");
                return(BadRequest("Error creating otherProperty"));
            }
        }
        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;
                }
            }
        }