public static AgriculturalMachineryViewModel ToViewModel(this AgriculturalMachinery entity)
        {
            if (entity == null)
            {
                return(null);
            }

            AgriculturalMachineryViewModel model = new AgriculturalMachineryViewModel
            {
                Id = entity.Id,
                RegistrationNumber = entity.RegistrationNumber,
                FrameNumber        = entity.FrameNumber,
                Type = entity.Type,
            };

            return(model);
        }
Exemplo n.º 2
0
        public async Task CreateAsync(AgriculturalMachineryModel createModel, string userId)
        {
            Person  person  = null;
            Company company = null;
            Address address = null;

            if (createModel.Person != null)
            {
                person = new Person
                {
                    FirstName                = createModel.Person.FirstName,
                    MiddleName               = createModel.Person.MiddleName,
                    LastName                 = createModel.Person.LastName,
                    IdentificationNumber     = createModel.Person.IdentificationNumber,
                    IdentificationNumberType = createModel.Person.IdentificationType,
                    Phone  = createModel.Person.Phone,
                    Email  = createModel.Person.Email,
                    UserId = userId
                };

                SetCreateStamp(person, userId);

                address = new Address
                {
                    RegionId       = createModel.Person.Address.RegionId,
                    MunicipalityId = createModel.Person.Address.MunicipalityId,
                    CityId         = createModel.Person.Address.CityId,
                    StreetAddress  = createModel.Person.Address.StreetAddress
                };
            }
            else if (createModel.Person == null)
            {
                company = new Company
                {
                    Name = createModel.Company.Name,
                    Eik  = createModel.Company.EIK
                };

                address = new Address
                {
                    RegionId       = createModel.Company.Address.RegionId,
                    MunicipalityId = createModel.Company.Address.MunicipalityId,
                    CityId         = createModel.Company.Address.CityId,
                    StreetAddress  = createModel.Company.Address.StreetAddress
                };
            }


            var machine = new AgriculturalMachinery
            {
                RegistrationNumber = createModel.RegistrationNumber,
                FrameNumber        = createModel.FrameNumber,
                Type = createModel.Type
            };

            if (person != null)
            {
                machine.Owner  = person;
                person.Address = address;
                address.Person.Add(person);
                person.AgriculturalMachinery.Add(machine);
                await _context.Address.AddAsync(address);

                await _context.Person.AddAsync(person);
            }
            else if (person == null)
            {
                machine.Company = company;
                company.Address = address;
                address.Company.Add(company);
                company.AgriculturalMachinery.Add(machine);
                await _context.Address.AddAsync(address);

                await _context.Company.AddAsync(company);
            }

            SetCreateStamp(machine, userId);
            await _context.AgriculturalMachinery.AddAsync(machine);

            await _context.SaveChangesAsync();
        }