Ejemplo n.º 1
0
        public static void Start()
        {
            Console.Write("Enter your hashtag: ");
            string input = Console.ReadLine();

            input = TagTransformer.Transform(input);
            db.Tags.Add(new Tag
            {
                Name = input
            });

            Console.WriteLine("Transformed: " + input);
            try
            {
                db.SaveChanges();
                Console.WriteLine($"Tag {input} was added to the database!");
            }
            catch (DbEntityValidationException ex)
            {
                Console.Write("DbEntityValidationException: ");
                foreach (var dbEntityValidationResult in ex.EntityValidationErrors)
                {
                    foreach (var validationError in dbEntityValidationResult.ValidationErrors)
                    {
                        Console.Write(validationError.ErrorMessage);
                        Console.WriteLine();
                    }
                }
            }
        }
        private static void CreateLensesIfNotExist(PhotographyContext context, IEnumerable <XElement> xLenses, Manufacturer manufacturer)
        {
            foreach (var xLens in xLenses)
            {
                var lensModel = xLens.Attribute("model").Value;
                var lensType  = xLens.Attribute("type").Value;
                var lensPrice = xLens.Attribute("price");

                var lens = context.Lenses.FirstOrDefault(x => x.Model == lensModel);
                if (lens != null)
                {
                    Console.WriteLine("Existing lens: {0}", lensModel);
                }
                else
                {
                    lens = new Lens
                    {
                        Model = lensModel,
                        Type  = lensType,
                        Price = lensPrice != null?decimal.Parse(lensPrice.Value) : default(decimal?),
                                    ManufacturerId = manufacturer.Id
                    };

                    context.Lenses.Add(lens);
                    context.SaveChanges();
                    Console.WriteLine("Created lens: {0}", lensModel);
                }
            }
        }
        private static Manufacturer CreateManufacturerIfNotExists(PhotographyContext context, XElement xManufacturer)
        {
            Manufacturer manufacturer             = null;
            var          xElementManufacturerName = xManufacturer.Element("manufacturer-name");

            if (xElementManufacturerName != null)
            {
                var manufacturerName = xElementManufacturerName.Value;
                manufacturer = context.Manufacturers.FirstOrDefault(x => x.Name == manufacturerName);
                if (manufacturer != null)
                {
                    Console.WriteLine("Existing manufacturer: {0}", manufacturerName);
                }
                else
                {
                    manufacturer = new Manufacturer
                    {
                        Name = manufacturerName,
                    };
                    context.Manufacturers.Add(manufacturer);
                    context.SaveChanges();
                    Console.WriteLine("Created manufacturer: {0}", manufacturerName);
                }
            }

            return(manufacturer);
        }
Ejemplo n.º 4
0
        public int SaveChanges()
        {
            try
            {
                #region WRITE LOG

                //var changes = _context.ChangeTracker.Entries()
                //    .Select(x => new UowChangeLog
                //    {
                //        OriginalValues = x.State != EntityState.Added ? x.OriginalValues.PropertyNames.ToDictionary(c => c, c => x.OriginalValues[c]) : null,
                //        CurrentValues = x.CurrentValues.PropertyNames.ToDictionary(c => c, c => x.CurrentValues[c]),
                //        State = x.State,
                //        DateInserted = DateTime.Now
                //    })
                //    .FirstOrDefault();

                //WriteUOWLogs(changes);

                #endregion WRITE LOG

                return(_context.SaveChanges());
            }
            catch (Exception ex)
            {
                // catch DbEntityValidationException errors
                var msg = ex.Message;
                throw;
            }
        }
        private static void ImportAccesories(PhotographyContext context)
        {
            XDocument xDocument = XDocument.Load(AccessoriesPath);
            var       suppliers = xDocument.XPathSelectElements("accessories/accessory");
            var       rnd       = new Random();

            foreach (var supplier in suppliers)
            {
                ImportAccesorie(supplier, context, rnd);
            }

            context.SaveChanges();
        }
        private static void ImportLenses(PhotographyContext context)
        {
            var jsoon        = File.ReadAllText(LensPath);
            var lensesEntity = JsonConvert.DeserializeObject <IEnumerable <LensDto> >(jsoon);


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


                var lensEntityAsJson = new Lens
                {
                    Make           = lens.Make,
                    FocalLenght    = lens.FocalLength,
                    MaxAperture    = lens.MaxAperture,
                    CompatibleWith = lens.CompatibleWith
                };


                try
                {
                    context.Lenses.Add(lensEntityAsJson);
                    context.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    context.Lenses.Remove(lensEntityAsJson);
                    context.SaveChanges();
                }

                Console.WriteLine($"Successfully imported {lens.Make} {lens.FocalLength}mm f{lens.MaxAperture}.");
            }
        }
        private static void ImportPhotographers(PhotographyContext context)
        {
            var json = File.ReadAllText(PhotograogersPath);
            var photographersEntity = JsonConvert.DeserializeObject <IEnumerable <PhotographerDto> >(json);

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


                var newPhotograph = new Photographer
                {
                    FirstName = photographer.FirstName,
                    LastName  = photographer.LastName,
                    Phone     = photographer.Phone
                };

                try
                {
                    context.Photographers.Add(newPhotograph);
                    context.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    context.Photographers.Remove(newPhotograph);
                }

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

            context.SaveChanges();
        }
        private static void ProcessRequest(EquipmentRequest request)
        {
            var context      = new PhotographyContext();
            var manufacturer = context.Manufacturers.FirstOrDefault(x => x.Name == request.Manufacturer);

            var cameraIds = context.Cameras
                            .Where(x => x.ManufacturerId == manufacturer.Id)
                            .Select(x => x.Id)
                            .ToList();
            var lensIds = context.Lenses
                          .Where(x => x.ManufacturerId == manufacturer.Id)
                          .Select(x => x.Id)
                          .ToList();
            var rnd = new Random();

            for (int i = 0; i < request.GenerateCount; i++)
            {
                var equipment  = new Equipment();
                var lensRand   = rnd.Next(lensIds.Count);
                var cameraRand = rnd.Next(cameraIds.Count);
                equipment.LensId   = lensIds[lensRand];
                equipment.CameraId = cameraIds[cameraRand];

                context.Equipments.Add(equipment);
                context.SaveChanges();

                var equipmentDb = context.Equipments
                                  .Include(x => x.Camera)
                                  .Include(x => x.Lens)
                                  .FirstOrDefault(x => x.Id == equipment.Id);

                Console.WriteLine("Equipment added: {0} (Camera: {1} - Lens: {2})",
                                  manufacturer.Name,
                                  equipmentDb.Camera.Model,
                                  equipmentDb.Lens.Model);
            }
        }
        private static void ImportCameras(PhotographyContext context)
        {
            var json          = File.ReadAllText(CamerasParh);
            var camerasEntity = JsonConvert.DeserializeObject <IEnumerable <CameraDto> >(json);

            foreach (var camera in camerasEntity)
            {
                if (camera.Make == null || camera.Model == null || camera.Type == null || camera.MinIso < 100)

                {
                    Console.WriteLine("Error. Invalid data provided");
                    continue;
                }

                if (camera.Type == "DSLR")
                {
                    var newdslrCamera = new DslrCamera
                    {
                        Make            = camera.Make,
                        Model           = camera.Model,
                        MinIso          = camera.MinIso,
                        MaxIso          = camera.MaxIso,
                        IsFullFrame     = camera.IsFullFrame,
                        MaxShutterSpeed = camera.MaxShutterSpeed,
                    };

                    try
                    {
                        context.Cameras.Add(newdslrCamera);
                        context.SaveChanges();
                    }
                    catch (DbEntityValidationException db)
                    {
                        context.Cameras.Remove(newdslrCamera);
                        context.SaveChanges();
                    }

                    Console.WriteLine($"Successfully imported {camera.Type} {camera.Make} {camera.Model}");
                }
                else
                {
                    var mirrorlesCamera = new MirrorlessCamera()
                    {
                        Make               = camera.Make,
                        Model              = camera.Model,
                        MinIso             = camera.MinIso,
                        MaxIso             = camera.MaxIso,
                        IsFullFrame        = camera.IsFullFrame,
                        MaxVideoResolution = camera.MaxVideoResolution,
                        MaxFrameRate       = camera.MaxFrameRate
                    };

                    try
                    {
                        context.Cameras.Add(mirrorlesCamera);
                        context.SaveChanges();
                    }
                    catch (DbEntityValidationException db)
                    {
                        context.Cameras.Remove(mirrorlesCamera);
                        Console.WriteLine($"Successfully imported {camera.Type} {camera.Make} {camera.Model}");
                    }
                }
                context.SaveChanges();
            }
        }