public static string ImportParts(CarDealerContext context, string inputJson) { var parts = JsonConvert.DeserializeObject <List <Part> >(inputJson, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); //var correctResult = context.Parts // .Select(p => new // { // Suppliers = p.SupplierId // }); foreach (var item in parts) { if (item.SupplierId > context.Suppliers.Count()) { continue; } var supplier = context.Suppliers.First(s => s.Id == item.SupplierId); if (supplier.IsImporter) { context.Add(item); } } context.SaveChanges(); return($"Successfully imported {parts.Count}."); }
public static string ImportCars(CarDealerContext context, string inputJson) { var carsDto = JsonConvert.DeserializeObject <List <CarPartsDto> >(inputJson); var config = new MapperConfiguration(cfg => { cfg.CreateMap <CarPartsDto, Car>(); }); var mapper = config.CreateMapper(); HashSet <int> validPartsIds = context.Parts .Select(p => p.Id) .ToHashSet(); var validCars = new List <CarPartsDto>(); foreach (var car in carsDto) { bool isValid = false; foreach (var currId in car.PartsId) { if (validPartsIds.Contains(currId)) { isValid = true; } else { isValid = false; } } if (isValid) { validCars.Add(car); } } for (int i = 0; i < validCars.Count; i++) { var carTemp = mapper.Map <Car>(validCars[i]); foreach (var partId in validCars[i].PartsId.Distinct()) { carTemp.PartCars.Add(new PartCar { CarId = i + 1, PartId = partId }); } context.Add(carTemp); } context.SaveChanges(); return($"Successfully imported {validCars.Count}."); }
public static string ImportParts(CarDealerContext context, string inputJson) { var parts = JsonConvert.DeserializeObject <List <Part> >(inputJson); foreach (var item in parts) { if (context.Suppliers.Find(item.SupplierId) != null) { context.Add(item); } } var changes = context.SaveChanges(); return($"Successfully imported {changes}."); }
public static string ImportParts(CarDealerContext context, string inputJson) { var parts = JsonConvert.DeserializeObject <Part[]>(inputJson); var suppliers = context.Suppliers.ToArray(); int partscount = 0; foreach (var part in parts) { if (suppliers.Any(s => s.Id == part.SupplierId)) { partscount++; context.Add(part); } } context.SaveChanges(); string result = $"Successfully imported {partscount}."; return(result); }
public static string ImportParts(CarDealerContext context, string inputJson) { var parts = JsonConvert.DeserializeObject <List <Part> >(inputJson); var supplierIds = context.Suppliers .Select(x => x.Id) .ToList(); var count = 0; foreach (var part in parts) { if (supplierIds.Contains(part.SupplierId)) { context.Add(part); count++; } } context.SaveChanges(); return($"Successfully imported {count}."); }