Ejemplo n.º 1
0
        private static void Importhotographers(PhotographyWorkshopsContext context)
        {
            string json = File.ReadAllText(PhotographersPath);
            var    photographersDtos = JsonConvert.DeserializeObject <IEnumerable <PhotographerDto> >(json);

            foreach (var photographersDto in photographersDtos)
            {
                if (photographersDto.FirstName == null || photographersDto.LastName == null || photographersDto.Phone == null || photographersDto.Lenses.Count == 0)
                {
                    Console.WriteLine(Error);
                    continue;
                }

                Photographer photographer = new Photographer()
                {
                    FirstName = photographersDto.FirstName,
                    LastName  = photographersDto.LastName,
                    Phone     = photographersDto.Phone
                };

                if (photographer.FirstName == null || photographer.LastName == null || photographer.Phone == null)
                {
                    Console.WriteLine(Error);
                    continue;
                }

                context.Photographers.Add(photographer);

                Console.WriteLine($"Successfully imported {photographer.FirstName} {photographer.LastName} | Lenses: {photographer.Lenses.Count}");
            }

            context.SaveChanges();
        }
Ejemplo n.º 2
0
        private static void ImportCameras(PhotographyWorkshopsContext context)
        {
            string json        = File.ReadAllText(CamerasPath);
            var    camerasDtos = JsonConvert.DeserializeObject <IEnumerable <CameraDto> >(json);

            foreach (var camerasDto in camerasDtos)
            {
                if (camerasDto.Make == null || camerasDto.MinIso == 0 || camerasDto.Model == null)
                {
                    Console.WriteLine(Error);
                    continue;
                }

                Camera camera = new Camera()
                {
                    CameraType      = camerasDto.Type,
                    Make            = camerasDto.Make,
                    Model           = camerasDto.Model,
                    MaxIso          = camerasDto.MaxIso,
                    MinIso          = camerasDto.MinIso,
                    MaxShutterSpeed = camerasDto.MaxShutterSpeed,
                };

                context.Cameras.Add(camera);
                Console.WriteLine($"Successfully imported {camera.CameraType} {camera.Make} {camera.Model}");
            }

            context.SaveChanges();
        }
        private static void ExportPhotographersWithSameCameraMakeToXML(PhotographyWorkshopsContext context)
        {
            var photographersWithSameCameraMake = context.Photographers
                                                  .Where(c => c.PrimaryCamera.Make == c.SecondaryCamera.Make);

            var xmlDocument      = new XDocument();
            var xmlPhotographers = new XElement("photographers");

            foreach (var photographer in photographersWithSameCameraMake)
            {
                var photographerNode = new XElement("photographer");
                photographerNode.Add(new XAttribute("name", photographer.FirstName + " " + photographer.LastName));
                photographerNode.Add(new XAttribute("primary-camera", photographer.PrimaryCamera.Make + " " + photographer.PrimaryCamera.Model));

                var xmlLenses = new XElement("lenses");

                foreach (var lens in photographer.Lenses)
                {
                    var lense = new XElement("lens", $"{lens.Make} {lens.FocalLength}mm f{lens.MaxAperture}");
                    xmlLenses.Add(lense);
                }

                photographerNode.Add(xmlLenses);
                xmlPhotographers.Add(photographerNode);
            }

            xmlDocument.Add(xmlPhotographers);
            xmlDocument.Save($"../../same-cameras-photographers.xml");

            Console.WriteLine(xmlDocument);
        }
        private static void ImportLensesFromJSON(PhotographyWorkshopsContext context)
        {
            var json   = File.ReadAllText(ImportLensesPath);
            var lenses = JsonConvert.DeserializeObject <IEnumerable <LensDTO> >(json);

            foreach (var lens in lenses)
            {
                if ((lens.Make == null) ||
                    (lens.FocalLength == null) ||
                    (lens.MaxAperture == null) ||
                    (lens.CompatibleWith == null))
                {
                    Console.WriteLine("Error. Invalid data provided");
                    continue;
                }

                var lensEntity = new Lens()
                {
                    Make           = lens.Make,
                    FocalLength    = IfNoIntValueReturnNull(lens.FocalLength),
                    MaxAperture    = IfNoFloatValueReturnNull(lens.MaxAperture),
                    CompatibleWith = lens.CompatibleWith
                };

                context.Lenses.Add(lensEntity);
                Console.WriteLine($"Successfully imported {lens.Make} {lens.FocalLength}mm f{lens.MaxAperture}");
            }

            context.SaveChanges();
        }
Ejemplo n.º 5
0
        private static void ImportLenses(PhotographyWorkshopsContext context)
        {
            string json       = File.ReadAllText(LensesPath);
            var    lensesDtos = JsonConvert.DeserializeObject <IEnumerable <LensDto> >(json);

            foreach (var lensesDto in lensesDtos)
            {
                if (lensesDto.Make == null || lensesDto.FocalLength == 0 || lensesDto.MaxAperture == 0 || lensesDto.CompatibleWith == null)
                {
                    Console.WriteLine(Error);
                    continue;
                }

                Lens lens = new Lens()
                {
                    Make           = lensesDto.Make,
                    FocalLength    = lensesDto.FocalLength,
                    MaxAperture    = lensesDto.MaxAperture,
                    CompatibleWith = lensesDto.CompatibleWith
                };

                context.Lenses.Add(lens);
                Console.WriteLine($"Successfully imported {lens.Make} {lens.FocalLength}mm f{lens.MaxAperture}");
            }

            context.SaveChanges();
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            PhotographyWorkshopsContext context = new PhotographyWorkshopsContext();

            OrderedPhotographers(context);
            LandscapePhotographers(context);
        }
        private static void ImportAccessoriesFromXML(PhotographyWorkshopsContext context)
        {
            var    xml         = XDocument.Load(ImportAccessoriesPath);
            var    accessories = xml.XPathSelectElements("accessories/accessory");
            Random rng         = new Random();

            foreach (var accessory in accessories)
            {
                var accessoryName = accessory.Attribute("name");

                if ((String.IsNullOrWhiteSpace(accessoryName.Value)) ||
                    (accessoryName == null))
                {
                    Console.WriteLine("Error. Invalid data provided");
                    continue;
                }

                int randomPhotographerId = rng.Next(1, context.Photographers.Count());

                var accessoryEntity = new Accessory()
                {
                    Name  = accessoryName.Value,
                    Owner = context.Photographers.Where(i => i.Id == randomPhotographerId).SingleOrDefault()
                };

                context.Accessories.Add(accessoryEntity);
                Console.WriteLine($"Successfully imported {accessoryName.Value}");
            }

            context.SaveChanges();
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            PhotographyWorkshopsContext context = new PhotographyWorkshopsContext();

            //ImportAccessoires(context);
            ImportWorkshops(context);
        }
Ejemplo n.º 9
0
        public static void Main()
        {
            PhotographyWorkshopsContext context = new PhotographyWorkshopsContext();

            var photographers = context.Photographers.Where(
                camera => camera.PrimaryCamera.Make == camera.SecondaryCamera.Make)
                                .Select(photographer => new
            {
                Name          = photographer.FirstName + " " + photographer.LastName,
                PrimaryCamera = photographer.PrimaryCamera.Make + " " + photographer.PrimaryCamera.Model,
                Lenses        = photographer.Lenses
            });

            var xmlDocument = new XElement("photographers");

            foreach (var photographer in photographers)
            {
                XElement photographerEl = new XElement("photographer");
                photographerEl.Add(new XAttribute("name", photographer.Name));
                photographerEl.Add(new XAttribute("primary-camera", photographer.PrimaryCamera));

                XElement lensesEl = new XElement("lenses");

                foreach (var victim in photographer.Lenses)
                {
                }

                xmlDocument.Save("../../../datasets/same-cameras-photographers.xml");
            }
        }
Ejemplo n.º 10
0
        static void Main()
        {
            PhotographyWorkshopsContext context = new PhotographyWorkshopsContext();

            //ImportLenses(context);
            //ImportCameras(context);
            Importhotographers(context);
        }
        private static void ImportCamerasFromJSON(PhotographyWorkshopsContext context)
        {
            var json    = File.ReadAllText(ImportCamerasPath);
            var cameras = JsonConvert.DeserializeObject <IEnumerable <CameraDTO> >(json);

            foreach (var camera in cameras)
            {
                if ((camera.Type == null) ||
                    (camera.Make == null) ||
                    (camera.Model == null) ||
                    (camera.MinISO == null))
                {
                    Console.WriteLine("Error. Invalid data provided");
                    continue;
                }

                if (int.Parse(camera.MinISO) < 100)
                {
                    camera.MinISO = null;
                }

                if (camera.Type == "DSLR")
                {
                    var cameraEntity = new Camera()
                    {
                        Type            = camera.Type,
                        Make            = camera.Make,
                        Model           = camera.Model,
                        MinISO          = IfNoIntValueReturnNull(camera.MinISO),
                        MaxISO          = IfNoIntValueReturnNull(camera.MaxISO),
                        MaxShutterSpeed = IfNoIntValueReturnNull(camera.MaxShutterSpeed)
                    };

                    context.Cameras.Add(cameraEntity);
                    Console.WriteLine($"Successfully imported {camera.Type} {camera.Make} {camera.Model}");
                }
                else if (camera.Type == "Mirrorless")
                {
                    var cameraEntity = new Camera()
                    {
                        Type               = camera.Type,
                        Make               = camera.Make,
                        Model              = camera.Model,
                        IsFullFrame        = IfNoBoolValueReturnNull(camera.IsFullFrame),
                        MinISO             = IfNoIntValueReturnNull(camera.MinISO),
                        MaxISO             = IfNoIntValueReturnNull(camera.MaxISO),
                        MaxVideoResolution = camera.MaxVideoResolution,
                        MaxFrameRate       = IfNoIntValueReturnNull(camera.MaxFrameRate)
                    };

                    context.Cameras.Add(cameraEntity);
                    Console.WriteLine($"Successfully imported {camera.Type} {camera.Make} {camera.Model}");
                }
            }

            context.SaveChanges();
        }
Ejemplo n.º 12
0
        private static void OrderedPhotographers(PhotographyWorkshopsContext context)
        {
            var photographers = context.Photographers.Select(photographer => new
            {
                FirstName = photographer.FirstName,
                LastName  = photographer.LastName,
                Phone     = photographer.Phone
            })
                                .OrderBy(firstname => firstname.FirstName)
                                .ThenByDescending(lastname => lastname.LastName);

            var json = JsonConvert.SerializeObject(photographers, Formatting.Indented);

            File.WriteAllText("../../../datasets/photographers-ordered.json", json);
        }
Ejemplo n.º 13
0
        private static void LandscapePhotographers(PhotographyWorkshopsContext context)
        {
            var photographers = context.Photographers.Select(photographer => new
            {
                FirstName   = photographer.FirstName,
                LastName    = photographer.LastName,
                CameraMake  = photographer.PrimaryCamera.Make,
                LensesCount = photographer.Lenses.Count
            })
                                .Where(camera => camera.CameraMake == "DSLR")
                                .OrderBy(firstame => firstame.FirstName);

            var json = JsonConvert.SerializeObject(photographers, Formatting.Indented);

            File.WriteAllText("../../../datasets/landscape-photogaphers.json", json);
        }
        private static void ImportPhotographersFromJSON(PhotographyWorkshopsContext context)
        {
            var    json          = File.ReadAllText(ImportPhotographersPath);
            var    photographers = JsonConvert.DeserializeObject <IEnumerable <PhotographerDTO> >(json);
            Random rng           = new Random();

            foreach (var photographer in photographers)
            {
                if ((photographer.FirstName == null) ||
                    (photographer.LastName == null) ||
                    (photographer.Phone == null) ||
                    (photographer.Lenses == null))
                {
                    Console.WriteLine("Error. Invalid data provided");
                    continue;
                }

                HashSet <Lens> lenses = new HashSet <Lens>();

                foreach (var lensId in photographer.Lenses)
                {
                    var lens = context.Lenses.Where(i => i.Id == lensId).SingleOrDefault();

                    if (lens == null)
                    {
                        continue;
                    }

                    lenses.Add(lens);
                }

                var photographerEntity = new Photographer()
                {
                    FirstName       = photographer.FirstName,
                    LastName        = photographer.LastName,
                    Phone           = photographer.Phone,
                    Lenses          = lenses,
                    PrimaryCamera   = context.Cameras.Find(rng.Next(1, context.Cameras.Count())),
                    SecondaryCamera = context.Cameras.Find(rng.Next(1, context.Cameras.Count()))
                };

                context.Photographers.Add(photographerEntity);
                Console.WriteLine($"Successfully imported {photographer.FirstName} {photographer.LastName} | {lenses.Count()}");
            }

            context.SaveChanges();
        }
        private static void ExportOrderedPhotographersToJSON(PhotographyWorkshopsContext context)
        {
            var photographersOrdered = context.Photographers
                                       .OrderBy(fn => fn.FirstName)
                                       .ThenByDescending(ln => ln.LastName)
                                       .Select(p => new
            {
                FirstName = p.FirstName,
                LastName  = p.LastName,
                Phone     = p.Phone
            });

            var json = JsonConvert.SerializeObject(photographersOrdered, Formatting.Indented);

            File.WriteAllText($"../../photographers-ordered.json", json);
            Console.WriteLine(json);
        }
Ejemplo n.º 16
0
        private static void ImportWorkshops(PhotographyWorkshopsContext context)
        {
            XDocument xmlDocument = XDocument.Load(WorkshopsPath);

            var workshopsXmls = xmlDocument.XPathSelectElements("workshops/workshop");

            foreach (var workshopsXml in workshopsXmls)
            {
                var             workshopName        = workshopsXml.Attribute("name").Value;
                var             workshopStartDate   = DateTime.Parse(workshopsXml.Attribute("start-date").Value);
                var             workshopEndDate     = DateTime.Parse(workshopsXml.Attribute("end-date").Value);
                var             workshopLocation    = workshopsXml.Attribute("location").Value;
                var             workshopPrice       = decimal.Parse(workshopsXml.Attribute("price").Value);
                var             trainer             = workshopsXml.Element("trainer").Value;
                List <XElement> participants        = new List <XElement>();
                var             participant         = workshopsXml.XPathSelectElement("participants/participant");
                var             participantFullName = participant.Attribute("first-name") + " " +
                                                      participant.Attribute("last-name");

                participants.Add(participant);

                if (workshopName == null || workshopPrice == null || workshopLocation == null || trainer == null)
                {
                    Console.WriteLine(Error);
                    continue;
                }

                Workshop workshop = new Workshop()
                {
                    Name                = workshopName,
                    StartDate           = workshopStartDate,
                    EndDate             = workshopEndDate,
                    Location            = workshopLocation,
                    PricePerParticipant = workshopPrice,
                };

                context.Workshops.Add(workshop);
                Console.WriteLine($"Successfully imported {workshop.Name}");
            }

            context.SaveChanges();
        }
        private static void ExportLandscapePhotographersToJSON(PhotographyWorkshopsContext context)
        {
            var landscapePhotographers = context.Photographers
                                         .OrderBy(fn => fn.FirstName)
                                         .Where(pc => pc.PrimaryCamera.Type == "DSLR")
                                         .Where(l => l.Lenses.All(fl => fl.FocalLength <= 30f))
                                         .Where(lc => lc.Lenses.Count() > 0)
                                         .Select(p => new
            {
                FirstName   = p.FirstName,
                LastName    = p.LastName,
                CameraMake  = p.PrimaryCamera.Make,
                LensesCount = p.Lenses.Count
            });

            var json = JsonConvert.SerializeObject(landscapePhotographers, Formatting.Indented);

            File.WriteAllText($"../../landscape-photogaphers.json", json);
            Console.WriteLine(json);
        }
        static void Main(string[] args)
        {
            var context = new PhotographyWorkshopsContext();

            // JSON Imports
            ImportCamerasFromJSON(context);
            ImportLensesFromJSON(context);
            ImportPhotographersFromJSON(context);

            // XML Imports
            ImportAccessoriesFromXML(context);
            //ImportWorkshopsFromXML(context); // Throw Exception

            // JSON Exports
            ExportOrderedPhotographersToJSON(context);
            ExportLandscapePhotographersToJSON(context);

            // XML Exports
            ExportPhotographersWithSameCameraMakeToXML(context);
            //ExportWorkshopsByLocationToXML(context); // Unfinished
        }
Ejemplo n.º 19
0
        private static void ImportAccessoires(PhotographyWorkshopsContext context)
        {
            XDocument xmlDocument = XDocument.Load(AccessoiresPath);

            var accessoriesXmls = xmlDocument.XPathSelectElements("accessories/accessory");

            foreach (var accessoriesXml in accessoriesXmls)
            {
                var accessoryName = accessoriesXml.Attribute("name").Value;

                Accessory accessory = new Accessory()
                {
                    Name = accessoryName
                };

                context.Accessories.Add(accessory);

                Console.WriteLine($"Successfully imported {accessory.Name}");
            }

            context.SaveChanges();
        }
        private static void ExportWorkshopsByLocationToXML(PhotographyWorkshopsContext context)
        {
            var workshopsByLocation = context.Workshops
                                      .Where(p => p.Participants.Count >= 5)
                                      .GroupBy(l => l.Location);


            var xmlDocument  = new XDocument();
            var xmlLocations = new XElement("locations");

            foreach (var location in workshopsByLocation)
            {
                var locationNode = new XElement("location");
                locationNode.Add(new XAttribute("name", location));

                // Unfinished
            }

            xmlDocument.Add(xmlLocations);
            xmlDocument.Save($"../../ workshops-by-location.xml");

            Console.WriteLine(xmlDocument);
        }
        private static void ImportWorkshopsFromXML(PhotographyWorkshopsContext context)
        {
            var xml       = XDocument.Load(ImportWorkshopsPath);
            var workshops = xml.XPathSelectElements("workshops/workshop");

            foreach (var workshop in workshops)
            {
                var workshopName      = workshop.Attribute("name");
                var workshopStartDate = workshop.Attribute("start-date");
                var workshopEndDate   = workshop.Attribute("end-date");
                var workshopLocation  = workshop.Attribute("location");
                var workshopPrice     = workshop.Attribute("price");
                var workshopTrainer   = workshop.Element("trainer");

                if ((workshopName == null) ||
                    (workshopLocation == null) ||
                    (workshopPrice == null) ||
                    (workshopTrainer == null))
                {
                    Console.WriteLine("Error. Invalid data provided");
                    continue;
                }

                string[] workshopTrainerNames     = workshopTrainer.Value.Trim().Split(' ').ToArray();
                string   workshopTrainerFirstName = workshopTrainerNames[0].ToString();
                string   workshopTrainerLastName  = workshopTrainerNames[1].ToString();

                var workshopParticipants            = workshop.XPathSelectElements("participants/participant");
                HashSet <Photographer> participants = new HashSet <Photographer>();

                foreach (var workshopParticipant in workshopParticipants)
                {
                    string firstName = workshopParticipant.Attribute("first-name").Value;
                    string lastName  = workshopParticipant.Attribute("last-name").Value;

                    var participant = context.Photographers
                                      .Where(fn => fn.FirstName == firstName)
                                      .Where(ln => ln.LastName == lastName)
                                      .FirstOrDefault();

                    if (participant == null)
                    {
                        continue;
                    }

                    participants.Add(participant);
                }

                var workshopEntity = new Workshop()
                {
                    Name                = workshopName.Value,
                    StartDate           = IfNoDateTimeValueReturnNull(workshopStartDate.Value),
                    EndDate             = IfNoDateTimeValueReturnNull(workshopEndDate.Value),
                    Location            = workshopLocation.Value,
                    PricePerParticipant = decimal.Parse(workshopPrice.Value),
                    Trainer             = context.Photographers.Where(n => n.FirstName == workshopTrainerFirstName)
                                          .Where(ln => ln.LastName == workshopTrainerLastName).FirstOrDefault(),
                    Participants = participants
                };

                context.Workshops.Add(workshopEntity);
                Console.WriteLine($"Successfully imported {workshopName.Value}");
            }

            context.SaveChanges();
        }