private static void AddLensToManufacturer(
     Lens lens, Manufacturer manufacturer, PhotographySystemEntities context)
 {
     if (!manufacturer.Lenses.Contains(lens))
     {
         manufacturer.Lenses.Add(lens);
         context.SaveChanges();
     }
 }
        private static void ImportLensesIfNotExist(
            IEnumerable<XElement> xmlLenses, Manufacturer manufacturer, PhotographySystemEntities context)
        {
            foreach (var xmlLense in xmlLenses)
            {
                XAttribute modelAttr = xmlLense.Attribute("model");
                XAttribute typeAttr = xmlLense.Attribute("type");
                XAttribute pricelAttr = xmlLense.Attribute("price");

                if (modelAttr == null || typeAttr == null)
                {
                    throw new ArgumentException("Model and type of lens is requiered.");
                }

                string model = modelAttr.Value;
                string type = typeAttr.Value;
                decimal? price = null;
                if (pricelAttr != null)
                {
                    price = decimal.Parse(pricelAttr.Value);
                }

                var lensInDb = context.Lenses
                    .FirstOrDefault(l => l.Model == model);

                if (lensInDb == null)
                {
                    Lens newLens = new Lens();
                    newLens.Manufacturer = manufacturer;
                    newLens.Model = model;
                    newLens.Type = type;
                    if (price != null)
                    {
                        newLens.Price = price;
                    }

                    context.Lenses.Add(newLens);
                    context.SaveChanges();
                    Console.WriteLine("Created lens: {0}", model);
                    AddLensToManufacturer(newLens, manufacturer, context);
                }
                else
                {
                    Console.WriteLine("Existing lens: {0}", model);
                    AddLensToManufacturer(lensInDb, manufacturer, context);
                }
            }
        }
        private static void GenerateRndEquipment(
            int generateCount, string manufacturerName, PhotographySystemEntities context)
        {
            int count = 0;

            while (count < generateCount)
            {
                Camera newEquipmentCamera = context.Cameras
                    .Where(c => c.Manufacturer.Name == manufacturerName)
                    .OrderBy(c => Guid.NewGuid()).Take(1).FirstOrDefault();

                Lens newEquipmentLens = context.Lenses
                    .Where(l => l.Manufacturer.Name == manufacturerName)
                    .OrderBy(l => Guid.NewGuid()).Take(1).FirstOrDefault();

                var equipmentInDb = context.Equipments
                    .FirstOrDefault(e => e.Camera.Model == newEquipmentCamera.Model &&
                        e.Lens.Model == newEquipmentLens.Model);

                if (equipmentInDb == null)
                {
                    Equipment newEquipment = new Equipment();
                    newEquipment.Camera = newEquipmentCamera;
                    newEquipment.Lens = newEquipmentLens;
                    context.Equipments.Add(newEquipment);
                    context.SaveChanges();
                    Console.WriteLine(
                        "Equipment added: {0} (Camera: {1} - Lens: {2})",
                        manufacturerName,
                        newEquipmentCamera.Model,
                        newEquipmentLens.Model);

                    count++;
                }
            }
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            var context = new PhotographySystemEntities();

            //1.ListManufacturerModel
            //var cameras = context.Cameras
            //    .OrderBy(c => c.Manufacturer.Name + " " + c.Model)
            //    .Select(c => c.Manufacturer.Name + " " + c.Model);

            //foreach (var camera in cameras)
            //{
            //    Console.WriteLine(camera);
            //}

            //2.ExportManufacturerAndCameraAsJSON
            //var manufacturers = context.Manufacturers
            //    .OrderBy(m => m.Name)
            //    .Select(m => new
            //    {
            //        manufacturer = m.Name,
            //        cameras = m.Cameras
            //            .OrderBy(c => c.Model)
            //            .Select(c => new 
            //            {
            //                c.Model,
            //                c.Price
            //            })
            //    });

            //string json = JsonConvert.SerializeObject(manufacturers, Newtonsoft.Json.Formatting.Indented);
            //File.WriteAllText("../../manufacturers-and-cameras.json", json);

            //3.ExportPhotographsAsXML
            //var photographs = context.Photographs
            //    .OrderBy(p => p.Title)
            //    .Select(p => new
            //    {
            //        p.Title ,
            //        CategoryName = p.Category.Name,
            //        p.Link,
            //        CameraMegapixels = p.Equipment.Camera.Megapixels,
            //        Camera = p.Equipment.Camera.Manufacturer.Name + " " + p.Equipment.Camera.Model,
            //        Lens = p.Equipment.Lens.Model,
            //        LensPrice = p.Equipment.Lens.Price
            //    });

            //var settings = new XmlWriterSettings()
            //{
            //    Indent = true,
            //    NewLineChars = "\n"
            //};

            //using (var writer = XmlWriter.Create("../../photographs.xml", settings))
            //{
            //    writer.WriteStartDocument();
            //    writer.WriteStartElement("photographs");

            //    foreach (var photograph in photographs)
            //    {
            //        writer.WriteStartElement("photograph");
            //        writer.WriteAttributeString("title", photograph.Title);
            //        writer.WriteStartElement("category");
            //        writer.WriteString(photograph.CategoryName);
            //        writer.WriteEndElement();
            //        writer.WriteStartElement("link");
            //        writer.WriteString(photograph.Link);
            //        writer.WriteEndElement();
            //        writer.WriteStartElement("equipment");
            //        writer.WriteStartElement("camera");
            //        writer.WriteAttributeString("megapixels", photograph.CameraMegapixels.ToString());
            //        writer.WriteString(photograph.Camera);
            //        writer.WriteEndElement();
            //        writer.WriteStartElement("lens");

            //        if (photograph.LensPrice != null)
            //        {
            //            writer.WriteAttributeString("price", Math.Round((decimal)photograph.LensPrice, 2).ToString());
            //        }

            //        writer.WriteString(photograph.Lens);
            //        writer.WriteEndElement();
            //        writer.WriteEndElement();
            //        writer.WriteEndElement();
            //    }

            //    writer.WriteEndElement();
            //    writer.WriteEndDocument();
            //}

            //4.ImportManufacturerAndLensesFromXML
            var xmlDocument = XDocument.Load("../../manufacturers-and-lenses.xml");
            var manufacturers =
                from manufacturer in xmlDocument.Descendants("manufacturer")
                select new
                {
                    Name = manufacturer.Element("manufacturer-name").Value,
                    Lenses = manufacturer.Element("lenses") == null ? null :
                        from lens in manufacturer.Descendants("lens")
                        select new
                        {
                            Model = lens.Attribute("model").Value,
                            Type = lens.Attribute("type").Value,
                            Price = lens.Attribute("price") == null ? null : lens.Attribute("price").Value
                        }
                };
            int manufacturerNumber = 1;

            foreach (var manufacturer in manufacturers)
            {
                Console.WriteLine("Prcessing manufacturer #{0}", manufacturerNumber);

                if (!context.Manufacturers.Any(m => m.Name == manufacturer.Name))
                {
                    context.Manufacturers.Add(new Manufacturer()
                    {
                        Name = manufacturer.Name
                    });
                    Console.WriteLine("Created manufacturer: {0}", manufacturer.Name);
                }
                else
                {
                    Console.WriteLine("Existing manufacturer: {0}", manufacturer.Name);
                }

                context.SaveChanges();

                foreach (var lens in manufacturer.Lenses)
                {
                    if (!context.Lenses.Any(l => l.Model == lens.Model))
                    {
                        if (lens.Price != null)
	                    {
                            context.Lenses.Add(new Lens()
                            {
                                Model = lens.Model,
                                Type = lens.Type,
                                Price = decimal.Parse(lens.Price),
                                ManufacturerId = context.Manufacturers.Where(m => m.Name == manufacturer.Name).FirstOrDefault().Id
                            });
                        }
                        else
                        {
                            context.Lenses.Add(new Lens()
                            {
                                Model = lens.Model,
                                Type = lens.Type,
                                Price = null,
                                ManufacturerId = context.Manufacturers.Where(m => m.Name == manufacturer.Name).FirstOrDefault().Id
                            });
                        }

                        Console.WriteLine("Created lens: {0}", lens.Model);
                    }
                    else
                    {
                        Console.WriteLine("Existing lens: {0}", lens.Model);
                    }
                }

                manufacturerNumber++;
            }

            context.SaveChanges();
        }
        private static Manufacturer ImportManucturerIfNotExtists(
            XElement xmlManufacturer, PhotographySystemEntities context)
        {
            XElement xmlManufacturerName = xmlManufacturer.Element("manufacturer-name");
            if (xmlManufacturerName == null)
            {
                throw new ArgumentException("Manufacturer name is required.");
            }

            string manufacturerName = xmlManufacturerName.Value;
            var manufacturerFromDb = context.Manufacturers
                .FirstOrDefault(m => m.Name == manufacturerName);

            if (manufacturerFromDb == null)
            {
                Manufacturer newManufacturer = new Manufacturer();
                newManufacturer.Name = manufacturerName;
                context.Manufacturers.Add(newManufacturer);
                context.SaveChanges();
                Console.WriteLine("Created manufacturer: {0}", manufacturerName);
                return newManufacturer;
            }

            Console.WriteLine("Existing manufacturer: {0}", manufacturerName);
            return manufacturerFromDb;
        }