Exemple #1
0
        static void Main(string[] args)
        {
            var directoryToSaveTo = new DirectoryInfo("../../../manufacturers-and-cameras.json");

            var context = new PhotographySystemEntities();
            var manufacturers = context.Manufacturers
                .Select(m => new
                {
                    manufacturer = m.Name,
                    cameras = m.Cameras
                        .Select(c => new
                        {
                            model = c.Model,
                            price = c.Price
                        })
                        .OrderBy(c => c.model)
                })
                .OrderBy(m => m.manufacturer)
                .ToList();

            var serializer = new JavaScriptSerializer();
            var json = serializer.Serialize(manufacturers);
            Console.WriteLine("Saved to {0}", directoryToSaveTo.FullName);
            File.WriteAllText(directoryToSaveTo.FullName, json);
        }
Exemple #2
0
        static void Main(string[] args)
        {
            var directoryToSaveTo = new DirectoryInfo("../../../photographs.xml");

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

                })
                .OrderBy(p => p.Title)
                .ToList();

            var xml = new XDocument();
            var root = new XElement("photographs");
            xml.Add(root);
            foreach (var photograph in photographs)
            {
                var photoNode = new XElement("photograph");
                photoNode.SetAttributeValue("title", photograph.Title);
                photoNode.Add(new XElement("category", photograph.CategoryName));
                photoNode.Add(new XElement("link", photograph.Link));
                var equipmentNode = new XElement("equipment");
                var camera = new XElement("camera", photograph.Equipment.Camera.Model);
                camera.SetAttributeValue("megapixels", photograph.Equipment.Camera.Megapixels);
                equipmentNode.Add(camera);
                var lens = new XElement("lens", photograph.Equipment.Lens.Model);
                if (photograph.Equipment.Lens.Price != null)
                {
                    lens.SetAttributeValue("price", photograph.Equipment.Lens.Price);
                }

                equipmentNode.Add(lens);
                photoNode.Add(equipmentNode);
                root.Add(photoNode);
            }

            Console.WriteLine("Saved to {0}", directoryToSaveTo.FullName);
            xml.Save(directoryToSaveTo.FullName);
        }
Exemple #3
0
        static void Main(string[] args)
        {
            var context = new PhotographySystemEntities();
            var models = context.Cameras
                .Select(c => new
                {
                    Manufacturer = c.Manufacturer.Name,
                    Model = c.Model
                })
                .OrderBy(c => c.Manufacturer + " " + c.Model)
                .ToList();

            models.ForEach(m =>
            {
                Console.WriteLine("{0} {1}", m.Manufacturer, m.Model);
            });
        }
Exemple #4
0
        static void Main(string[] args)
        {
            var context = new PhotographySystemEntities();
            var models  = context.Cameras
                          .Select(c => new
            {
                Manufacturer = c.Manufacturer.Name,
                Model        = c.Model
            })
                          .OrderBy(c => c.Manufacturer + " " + c.Model)
                          .ToList();

            models.ForEach(m =>
            {
                Console.WriteLine("{0} {1}", m.Manufacturer, m.Model);
            });
        }
Exemple #5
0
        private static void ProcessLenses(PhotographySystemEntities context, XElement lense, Manufacturer manufacturer)
        {
            var modelNode = lense.Attribute("model");
            if (modelNode == null)
            {
                throw new ArgumentException("Model is mandatory for lenses!");
            }

            var model = modelNode.Value;
            var typeNode = lense.Attribute("type");
            if (typeNode == null)
            {
                throw new ArgumentException("Type is required for lenses!");
            }

            var type = typeNode.Value;
            decimal? price = null;
            var priceNode = lense.Attribute("price");
            if (priceNode != null)
            {
                price = decimal.Parse(priceNode.Value);
            }

            Lens processedLense = context.Lenses.FirstOrDefault(l => l.Model == model);
            if (processedLense == null)
            {
                processedLense = new Lens()
                {
                    Model = model,
                    Type = type,
                    Price = price,
                    Manufacturer = manufacturer
                };

                context.Lenses.AddOrUpdate(processedLense);
                context.SaveChanges();
                Console.WriteLine("Created lens: {0}", processedLense.Model);
            }
            else
            {
                Console.WriteLine("Existing lens: {0}", processedLense.Model);
            }
        }
Exemple #6
0
        public static void Main(string[] args)
        {
            var context = new PhotographySystemEntities();

            var xml = XDocument.Load("../../../manufacturers-and-lenses.xml");
            var manufacturers = xml.Root.Elements();
            var processCounter = 0;
            foreach (var node in manufacturers)
            {
                Console.WriteLine("Processing manufacturer #{0} ...", ++processCounter);
                try
                {
                    var manufacturer = ProcessManufacturer(context, node);
                    var lensesNode = node.Element("lenses");
                    if (lensesNode != null)
                    {
                        var lenses = lensesNode.Elements();
                        foreach (var lense in lenses)
                        {
                            try
                            {
                                ProcessLenses(context, lense, manufacturer);
                            }
                            catch (ArgumentException ex)
                            {
                                Console.Error.WriteLine("Processing lens terminated: {0}", ex.Message);
                            }
                        }
                    }

                    Console.WriteLine();
                }
                catch (ArgumentException ex)
                {
                    Console.Error.WriteLine("Processing manufacture terminated: {0}", ex.Message);
                }
            }
        }
Exemple #7
0
        private static Manufacturer ProcessManufacturer(PhotographySystemEntities context, XElement manufacturer)
        {
            var manufacturerNameNode = manufacturer.Element("manufacturer-name");
            if (manufacturerNameNode == null)
            {
                throw new ArgumentException("Manufacturer name is mandatory!");
            }

            var name = manufacturerNameNode.Value;
            var processedManufacturer = context.Manufacturers.FirstOrDefault(m => m.Name == name);
            if (processedManufacturer == null)
            {
                processedManufacturer = new Manufacturer()
                {
                    Name = name
                };

                context.Manufacturers.Add(processedManufacturer);
                context.SaveChanges();
                Console.WriteLine("Created manufacturer: {0}", processedManufacturer.Name);
            }
            else
            {
                Console.WriteLine("Existing manufacturer: {0}", processedManufacturer.Name);
            }

            return processedManufacturer;
        }
        public static void Main(string[] args)
        {
            var context = new PhotographySystemEntities();
            var rng = new Random();
            var doc = XDocument.Load("../../../generate-equipments.xml");
            var generate = doc.Root.Elements();

            var processCount = 0;
            foreach (var element in generate)
            {
                Console.WriteLine("Processing request #{0} ...", ++processCount);
                var generateCountNode = element.Attribute("generate-count");
                var generateCunt = 10;
                if (generateCountNode != null)
                {
                    generateCunt = int.Parse(generateCountNode.Value);
                }

                var manufacturerNode = element.Element("manufacturer");
                var manufacturer = "Nikon";
                if (manufacturerNode != null)
                {
                    manufacturer = manufacturerNode.Value;
                }

                var cameras = context.Cameras
                    .Where(c => c.Manufacturer.Name == manufacturer)
                    .Select(c => c.Id)
                    .ToList();

                var lens = context.Lenses
                   .Where(c => c.Manufacturer.Name == manufacturer)
                   .Select(c => c.Id)
                   .ToList();

                var users = context.Users.Select(u => u.Id).ToList();

                var photos = context.Photographs.Select(p => p.Id).ToList();

                for (int i = 0; i < generateCunt; i++)
                {
                    var userNumber = rng.Next(0, users.Count);
                    var usersList = new List<User>();
                    for (int j = 0; j < userNumber; j++)
                    {
                        usersList.Add(context.Users.Find(users[rng.Next(0, users.Count)]));
                    }

                    var photoNumber = rng.Next(0, users.Count);
                    var photosList = new List<Photograph>();
                    for (int j = 0; j < photoNumber; j++)
                    {
                        photosList.Add(context.Photographs.Find(photos[rng.Next(0, photos.Count)]));
                    }

                    var equipment = new Equipment()
                    {
                        Camera = context.Cameras.Find(cameras[rng.Next(0, cameras.Count())]),
                        Lens = context.Lenses.Find(lens[rng.Next(0, lens.Count())]),
                        Users = usersList,
                        Photographs = photosList
                    };

                    context.Equipments.Add(equipment);
                    context.SaveChanges();
                    Console.WriteLine("Equipment added: {0} (Camera: {1} - Lens: {2})",
                        manufacturer,
                        equipment.Camera.Model,
                        equipment.Lens.Model);
                }

                Console.WriteLine();
            }
        }