public static void UpdateEntity(this Data.Aircraft entity, AircraftViewModel model) { if (model == null || entity == null) { return; } entity.Id = model.Id; entity.ProducerName = model.ProducerName; entity.ProducerNameEn = model.ProducerNameEn; entity.ProducerCountryCode = model.ProducerCountryCode; entity.ProducerCountryName = model.ProducerCountryName; entity.AirCategoryCode = model.AirCategoryCode; entity.AirCategoryName = model.AirCategoryName; entity.Icao = model.Icao; entity.MsnserialNumber = model.MsnserialNumber; entity.ModelName = model.ModelName; entity.ModelNameEn = model.ModelNameEn; entity.AircraftDebt = new List <AircraftDebt>(); if (model.Debts != null && model.Debts.Any()) { entity.AircraftDebt = model.Debts .Select(x => x.ToEntity()).ToList(); } entity.AircraftRegistration = new List <AircraftRegistration>(); if (model.Registrations != null && model.Registrations.Any()) { entity.AircraftRegistration = model.Registrations .Select(x => x.ToEntity()).ToList(); } return; }
public async Task <Data.Aircraft> AddOrUpdateAircraftAsync(AircraftViewModel model) { if (model == null) { throw new NullReferenceException("AircraftViewModel is null"); } Data.Aircraft entity = null; var existing = await _context.Aircraft .Include(x => x.AircraftDebt) .Include(x => x.AircraftRegistration) .Include(x => x.AircraftRegistration).ThenInclude(x => x.AircraftRegistrationOwnerPerson) .Include(x => x.AircraftRegistration).ThenInclude(x => x.AircraftRegistrationOwnerEntity) .Include(x => x.AircraftRegistration).ThenInclude(x => x.AircraftRegistrationOperatorPerson) .Include(x => x.AircraftRegistration).ThenInclude(x => x.AircraftRegistrationOperatorEntity) .Where(x => x.MsnserialNumber.ToLower() == model.MsnserialNumber.ToLower() && x.AircraftExtension != null && x.AircraftExtension.Deactivated == false) .ToListAsync(); if (!existing.Any()) { entity = model.ToEntity(); entity.Id = 0; entity.AircraftExtension.AircraftId = entity.Id; await _context.Aircraft.AddAsync(entity); } else { foreach (Data.Aircraft item in existing) { item.AircraftExtension.Deactivated = true; _context.AircraftDebt.RemoveRange(item.AircraftDebt); foreach (Data.AircraftRegistration reg in item.AircraftRegistration) { _context.AircraftRegistrationOperatorPerson.RemoveRange(reg.AircraftRegistrationOperatorPerson); _context.AircraftRegistrationOperatorEntity.RemoveRange(reg.AircraftRegistrationOperatorEntity); _context.AircraftRegistrationOwnerPerson.RemoveRange(reg.AircraftRegistrationOwnerPerson); _context.AircraftRegistrationOwnerEntity.RemoveRange(reg.AircraftRegistrationOwnerEntity); } _context.AircraftRegistration.RemoveRange(item.AircraftRegistration); } existing[0].UpdateEntity(model); existing[0].AircraftExtension.Deactivated = false; existing[0].AircraftExtension.UpdatedAt = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); existing[0].AircraftExtension.RequestId = model.ExtensionRequestId; _context.Aircraft.Update(existing[0]); entity = existing[0]; } await _context.SaveChangesAsync(); return(entity); }
public static void UpdateEntity(this Data.Aircraft entity, RegiX.Client.ResponseModels.Aircraft model) { if (model == null || entity == null) { return; } entity.ProducerName = model.Producer?.Name; entity.ProducerNameEn = model.Producer?.NameEn; entity.ProducerCountryCode = model.Producer?.CountryCode; entity.ProducerCountryName = model.Producer?.CountryName; entity.AirCategoryCode = model.AirCategory?.Code; entity.AirCategoryName = model.AirCategory?.Name; entity.Icao = model.ICAO; entity.MsnserialNumber = model.MSNSerialNumber; entity.ModelName = model.BGModelName; entity.ModelNameEn = model.ENModelName; entity.AircraftRegistration = new List <AircraftRegistration>(); entity.AircraftRegistration = CreateAircraftRegistrations(entity.Id, model); entity.AircraftDebt = CreateAircraftDebts(entity.Id, model); return; }
private async Task <List <Data.Aircraft> > SaveAircrafts(AircraftsResponse aircraftsResponse, long?messageId, PropertySearchRequestModel searchModel, bool useSearchIdentifier) { List <Data.Aircraft> newAircrafts = new List <Data.Aircraft>(); List <Data.Aircraft> existingAircrafts = new List <Data.Aircraft>(); for (int i = 0; i < aircraftsResponse.Aircraft.Length; i++) { string msnToUse = aircraftsResponse.Aircraft[i].MSNSerialNumber; if (useSearchIdentifier) { msnToUse = searchModel.IdentifierTypeCode.ToUpper() == "MSN" ? searchModel.Identifier : searchModel.Identifier + "_" + i.ToString(); } var existing = await _context.Aircraft .Include(x => x.AircraftExtension) .Include(x => x.AircraftRegistration) .Include(x => x.AircraftRegistration).ThenInclude(a => a.AircraftRegistrationOperatorEntity) .Include(x => x.AircraftRegistration).ThenInclude(a => a.AircraftRegistrationOperatorPerson) .Include(x => x.AircraftRegistration).ThenInclude(a => a.AircraftRegistrationOwnerEntity) .Include(x => x.AircraftRegistration).ThenInclude(a => a.AircraftRegistrationOwnerPerson) .Include(x => x.AircraftDebt) .Where(x => x.MsnserialNumber == msnToUse && x.AircraftExtension != null && x.AircraftExtension.Deactivated == false) .ToListAsync(); if (!existing.Any()) { Data.Aircraft entity = aircraftsResponse.Aircraft[i].ToEntity(); entity.MsnserialNumber = msnToUse; entity.AircraftExtension = new AircraftExtension { AircraftId = entity.Id, UpdatedAt = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc), RequestId = messageId, Deactivated = false }; if (entity != null) { newAircrafts.Add(entity); } } else { foreach (Data.Aircraft item in existing) { item.AircraftExtension.Deactivated = true; foreach (Data.AircraftRegistration reg in item.AircraftRegistration) { _context.AircraftRegistrationOperatorEntity.RemoveRange(reg.AircraftRegistrationOperatorEntity); _context.AircraftRegistrationOperatorPerson.RemoveRange(reg.AircraftRegistrationOperatorPerson); _context.AircraftRegistrationOwnerPerson.RemoveRange(reg.AircraftRegistrationOwnerPerson); _context.AircraftRegistrationOwnerEntity.RemoveRange(reg.AircraftRegistrationOwnerEntity); } _context.AircraftRegistration.RemoveRange(item.AircraftRegistration); _context.AircraftDebt.RemoveRange(item.AircraftDebt); } existing[0].UpdateEntity(aircraftsResponse.Aircraft[i]); existing[0].MsnserialNumber = msnToUse; existing[0].AircraftExtension.Deactivated = false; existing[0].AircraftExtension.UpdatedAt = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); existing[0].AircraftExtension.RequestId = messageId; existingAircrafts.AddRange(existing); } } if (newAircrafts.Count > 0) { _context.Aircraft.AddRange(newAircrafts); } if (existingAircrafts.Count > 0) { _context.Aircraft.UpdateRange(existingAircrafts); } await _context.SaveChangesAsync(); return(newAircrafts.Concat(existingAircrafts).ToList()); }