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 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 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 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(); } }
private static Photographer GetRandomOwner(PhotographyWorkshopContext context) { Random rnd = new Random(); int randomId = rnd.Next(1, context.Photographers.Count() + 1); Photographer photographer = context.Photographers.Find(randomId); return(photographer); }
public static void ExportOrderedPhotograpers() { using (PhotographyWorkshopContext context = new PhotographyWorkshopContext()) { List <OrderedPhotographerDto> photographers = context.Photographers .OrderBy(photographer => photographer.FirstName) .ThenByDescending(photographer => photographer.LastName) .Select(photographer => new OrderedPhotographerDto { FirstName = photographer.FirstName, LastName = photographer.LastName, Phone = (photographer.Phone == "" ? "null" : photographer.Phone) }) .ToList(); ExportJsonToFolder(Constants.ExportOrderedPhotographers, photographers); } }
private static Camera GetRandomCamera(PhotographyWorkshopContext context, bool alreadyPassedPrimaryCamera) { Random rnd = new Random(); int randomId = rnd.Next(1, context.Cameras.Count() + 1); if (alreadyPassedPrimaryCamera) { if (randomId > 10) { randomId -= 10; } else { randomId += 10; } } Camera camera = context.Cameras.Find(randomId); return(camera); }
public static void ExportLandscapePhotographers() { using (PhotographyWorkshopContext context = new PhotographyWorkshopContext()) { List <LandscapePhotographerDto> photographers = context.Photographers .Where(photographer => photographer.PrimaryCamera is CameraDslr && photographer.Lenses.Count > 0 && photographer.Lenses.All(lens => lens.FocalLength <= 30)) .OrderBy(photographer => photographer.FirstName) .Select(photographer => new LandscapePhotographerDto { FirstName = photographer.FirstName, LastName = photographer.LastName, CameraMake = photographer.PrimaryCamera.Make, LensesCount = photographer.Lenses.Count }) .ToList(); ExportJsonToFolder(Constants.ExportLandscapePhotographers, photographers); } }
public static void ExportWorkshopsByLocation() { using (PhotographyWorkshopContext context = new PhotographyWorkshopContext()) { //// With 'Serializer' List <LocationDto> workshopsByLocation = context.Workshops .Where(workshop => workshop.Participants.Count >= 5) .GroupBy(workshop => workshop.Location, workshop => workshop, (location, workshops) => new { Location = location, Workshops = workshops }) .Where(ws => ws.Workshops.Any()) .Select(wsByLocation => new LocationDto { Name = wsByLocation.Location, WorkshopsDtos = wsByLocation.Workshops.Select(ws => new WorkshopDto { Name = ws.Name, //TotalProfit = CalculateTotalProfit(ws) - methods are't working in LINQ TotalProfit = (ws.Participants.Count * ws.PricePerParticipant) - ((ws.Participants.Count * ws.PricePerParticipant) * 0.2m), ParticipantDto = new ParticipantDto { ParticipantCount = ws.Participants.Count, Names = ws.Participants.Select(part => part.FirstName + " " + part.LastName).ToList() } }).ToList() }) .ToList(); ExportXmlToFolder(workshopsByLocation, Constants.ExportWorkshopsByLocation, "locations"); //// Without 'Serializer' // Without the Select clause the result is the same //var workshopsByLocation = context.Workshops // .Where(workshop => workshop.Participants.Count >= 5) // .GroupBy(workshop => workshop.Location, workshop => workshop, (location, workshops) => new // { // Location = location, // Workshops = workshops // }) // .Where(ws => ws.Workshops.Any()) // .Select(wsByLocation => new // { // wsByLocation.Location, // wsByLocation.Workshops, // }); // XDocument documentXml = new XDocument(); // XElement locationsListXml = new XElement("locations"); // foreach (var location in workshopsByLocation) // { // XElement locationXml = new XElement("location"); // locationXml.SetAttributeValue("name", location.Location); // foreach (Workshop workshop in location.Workshops) // { // XElement workshopXml = new XElement("workshop"); // workshopXml.SetAttributeValue("name", workshop.Name); // workshopXml.SetAttributeValue("total-profit", CalculateTotalProfit(workshop)); // XElement participantsListXml = new XElement("participants"); // participantsListXml.SetAttributeValue("count", workshop.Participants.Count); // foreach (Photographer participant in workshop.Participants) // { // XElement pariticipantXml = new XElement("participant"); // pariticipantXml.SetValue(participant.FullName); // participantsListXml.Add(pariticipantXml); // } // workshopXml.Add(participantsListXml); // locationXml.Add(workshopXml); // } // locationsListXml.Add(locationXml); // } // documentXml.Add(locationsListXml); // documentXml.Save("../../../datasets/workshops-by-location.xml"); } }
public static void ExportPhotographersWithSameCameraMake() { using (PhotographyWorkshopContext context = new PhotographyWorkshopContext()) { //// With 'Serialize' List <SameCameMakePhotographersDto> photographers = context.Photographers .Where(photographer => photographer.PrimaryCamera.Make == photographer.SecondaryCamera.Make) .Select(photographer => new SameCameMakePhotographersDto { Name = photographer.FirstName + " " + photographer.LastName, PrimaryCamera = photographer.PrimaryCamera.Make + " " + photographer.PrimaryCamera.Model, Lenses = photographer.Lenses .Select(lens => lens.Make + " " + lens.FocalLength + "mm f" + lens.MaxAperture) .ToList() }) .ToList(); ExportXmlToFolder(photographers, Constants.ExportPhotographersWithSameCameraMake, "photographers"); //// Without 'Serializer' //var photographers = context.Photographers // .Where(photographer => photographer.PrimaryCamera.Make == photographer.SecondaryCamera.Make) // .Select(photographer => new // { // Name = photographer.FirstName + " " + photographer.LastName, // PrimaryCamera = photographer.PrimaryCamera.Make + " " + photographer.PrimaryCamera.Model, // Lenses = photographer.Lenses.Select(lens => new // { // lens.Make, // lens.FocalLength, // lens.MaxAperture // }) // }) // .ToList(); //XDocument documentXml = new XDocument(); //XElement photographersListXml = new XElement("photographers"); //foreach (var photographer in photographers) //{ // XElement photographerXml = new XElement("photographer"); // photographerXml.SetAttributeValue("name", photographer.Name); // photographerXml.SetAttributeValue("primary-camera", photographer.PrimaryCamera); // if (photographer.Lenses.Any()) // { // XElement lensesListXml = new XElement("lenses"); // foreach (var lens in photographer.Lenses) // { // XElement lensXml = new XElement("lens"); // lensXml.Value = lens.Make + " " + lens.FocalLength + " " + lens.MaxAperture; // lensesListXml.Add(lensXml); // } // photographerXml.Add(lensesListXml); // } // photographersListXml.Add(photographerXml); //} //documentXml.Add(photographersListXml); //documentXml.Save("../../../datasets/same-cameras-photographers.xml"); } }
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(); } }