public static void ImportCameras() { //Mapper.Initialize(cfg => //{ // cfg.CreateMap<CameraDto, CameraDslr>(); // cfg.CreateMap<CameraDto, CameraMirrorless>(); //}); IEnumerable <CameraDto> CamerasDto = ParseJson <CameraDto>(Constants.CamerasPath); List <Camera> cameras = new List <Camera>(); using (PhotographyWorkshopContext context = new PhotographyWorkshopContext()) { foreach (CameraDto cameraDto in CamerasDto) { if (cameraDto.Type == null || cameraDto.Make == null || cameraDto.Model == null || cameraDto.MinISO == null) { Console.WriteLine(Messages.InvalidDate); continue; } else { Camera cameraEntity = GetCamera(cameraDto); Console.WriteLine($"Successfully imported {cameraEntity.GetType().Name} {cameraEntity.Make} {cameraEntity.Model}"); cameras.Add(cameraEntity); } } context.Cameras.AddRange(cameras); context.SaveChanges(); } }
public static void ImportLenses() { IEnumerable <LensDto> lensesDto = ParseJson <LensDto>(Constants.LensPath); List <Lens> lenses = new List <Lens>(); using (PhotographyWorkshopContext context = new PhotographyWorkshopContext()) { foreach (LensDto lensDto in lensesDto) { Lens lensEntity = new Lens() { Make = lensDto.Make, FocalLength = int.Parse(lensDto.FocalLength), MaxAperture = float.Parse(lensDto.MaxAperture), CompatibleWith = lensDto.CompatibleWith }; lenses.Add(lensEntity); Console.WriteLine($"Successfully imported {lensEntity.Make} {lensEntity.FocalLength}mm f{lensEntity.MaxAperture}"); } context.Lenses.AddRange(lenses); context.SaveChanges(); } }
public static void ImportAccessories() { XDocument documentXml = XDocument.Load(Constants.AccessoriesPath); //IEnumerable<XElement> accessoriesListXml = documentXml.Root?.Elements(); IEnumerable <XElement> accessoriesListXml = documentXml.XPathSelectElements("accessories/accessory"); using (PhotographyWorkshopContext context = new PhotographyWorkshopContext()) { List <Accessory> accessoriesList = new List <Accessory>(); foreach (XElement accessoryXml in accessoriesListXml) { Accessory accessoryEntity = new Accessory() { Name = accessoryXml.Attribute("name")?.Value, Owner = GetRandomOwner(context) }; Console.WriteLine($"Successfully imported {accessoryEntity.Name}"); accessoriesList.Add(accessoryEntity); } context.Accessories.AddRange(accessoriesList); context.SaveChanges(); } }
public static void ImportPhotographers() { IEnumerable <PhotographerDto> photographerDtos = ParseJson <PhotographerDto>(Constants.PhotographersPath); List <Photographer> photographers = new List <Photographer>(); using (PhotographyWorkshopContext context = new PhotographyWorkshopContext()) { foreach (PhotographerDto photographerDto in photographerDtos) { if (photographerDto.FirstName == null || photographerDto.LastName == null) { Console.WriteLine(Messages.InvalidDate); continue; } string phone = String.Empty; if (photographerDto.Phone != null) { Regex regex = new Regex(@"\+\d{1,3}\/\d{8,10}"); if (regex.IsMatch(photographerDto.Phone)) { phone = photographerDto.Phone; } } Photographer photographerEntity = new Photographer() { FirstName = photographerDto.FirstName, LastName = photographerDto.LastName, Phone = phone }; bool alreadyPassedPrimaryCamera = false; photographerEntity.PrimaryCamera = GetRandomCamera(context, alreadyPassedPrimaryCamera); alreadyPassedPrimaryCamera = true; photographerEntity.SecondaryCamera = GetRandomCamera(context, alreadyPassedPrimaryCamera); foreach (var lensDtoId in photographerDto.Lenses) { Lens lensEntity = context.Lenses.FirstOrDefault(lens => lens.Id == lensDtoId); if (lensEntity != null) { if (CheckLensCompatibility(photographerEntity, lensEntity)) { photographerEntity.Lenses.Add(lensEntity); } } } Console.WriteLine($"Successfully imported {photographerEntity.FullName} | Lenses: {photographerEntity.Lenses.Count}"); photographers.Add(photographerEntity); } context.Photographers.AddRange(photographers); context.SaveChanges(); } }
public static void ImportWorkshops() { XDocument documentXml = XDocument.Load(Constants.WorkshopsPath); //IEnumerable<XElement> workshopsListXml = documentXml.Root?.Elements(); IEnumerable <XElement> workshopsListXml = documentXml.XPathSelectElements("workshops/workshop"); using (PhotographyWorkshopContext context = new PhotographyWorkshopContext()) { List <Workshop> workshopsList = new List <Workshop>(); foreach (XElement workshopXml in workshopsListXml) { string name = workshopXml.Attribute("name")?.Value; string location = workshopXml.Attribute("location")?.Value; string priceAsString = workshopXml.Attribute("price")?.Value; XElement trainerName = workshopXml.XPathSelectElement("trainer"); if (name == null || location == null || priceAsString == null || trainerName == null) { Console.WriteLine(Messages.InvalidDate); continue; } Photographer trainer = context.Photographers .FirstOrDefault(photographer => photographer.FirstName + " " + photographer.LastName == trainerName.Value); DateTime?startDate = null; DateTime?endDate = null; string startDateAString = workshopXml.Attribute("start-date")?.Value; if (startDateAString != null) { startDate = DateTime.Parse(startDateAString); } string endDateAsString = workshopXml.Attribute("end-date")?.Value; if (endDateAsString != null) { endDate = DateTime.Parse(endDateAsString); } var workshopEntity = new Workshop() { Name = name, StartDate = startDate, EndDate = endDate, Location = location, PricePerParticipant = decimal.Parse(priceAsString), Trainer = trainer }; IEnumerable <XElement> participantsListXml = workshopXml.XPathSelectElements("participants/participant"); foreach (XElement participantXml in participantsListXml) { string firstName = participantXml.Attribute("first-name")?.Value; string lastName = participantXml.Attribute("last-name")?.Value; if (firstName == null || lastName == null) { Console.WriteLine(Messages.InvalidDate); continue; } Photographer participant = context.Photographers .FirstOrDefault(photographer => photographer.FirstName == firstName && photographer.LastName == lastName); if (participant == null) { Console.WriteLine(Messages.InvalidDate); continue; } workshopEntity.Participants.Add(participant); } Console.WriteLine($"Successfully imported {workshopEntity.Name}"); workshopsList.Add(workshopEntity); } context.Workshops.AddRange(workshopsList); context.SaveChanges(); } }