Beispiel #1
0
        public void ImportCameras()
        {
            List <CameraDTO> cameras = new List <CameraDTO>();

            string json = File.ReadAllText(@"..\..\..\PhotographyWorkshops.Data\Imports\cameras.json");

            cameras = JsonConvert.DeserializeObject <List <CameraDTO> >(json);

            //Type, make, model, minIso -> required

            List <DSLRCamera>       dslrCameras       = new List <DSLRCamera>();
            List <MirrorlessCamera> mirrorlessCameras = new List <MirrorlessCamera>();
            StringBuilder           sb = new StringBuilder();

            foreach (var cam in cameras)
            {
                if (cam.Type != "DSLR" && cam.Type != "Mirrorless" || (cam.Make == null || cam.Model == null || cam.MinISO < 100))
                {
                    sb.AppendLine($"Error. Invalid data provided");
                }
                else
                {
                    if (cam.Type == "DSLR")
                    {
                        CameraType type = new CameraType();
                        Enum.Parse(type.GetType(), cam.Type);
                        dslrCameras.Add(new DSLRCamera
                        {
                            IsFullFrame     = cam.IsFullFrame,
                            Make            = cam.Make,
                            MaxIso          = cam.MaxISO,
                            MaxShutterSpeed = cam.MaxShutterSpeed,
                            MinIso          = cam.MinISO,
                            Model           = cam.Model,
                            Type            = type
                        });
                    }
                    else
                    {
                        CameraType type = new CameraType();
                        Enum.Parse(type.GetType(), cam.Type);
                        mirrorlessCameras.Add(new MirrorlessCamera
                        {
                            IsFullFrame        = cam.IsFullFrame,
                            Make               = cam.Make,
                            MaxIso             = cam.MaxISO,
                            MinIso             = cam.MinISO,
                            MaxFrameRate       = cam.MaxFrameRate,
                            MaxVideoResolution = cam.MaxVideoResolution,
                            Model              = cam.Model,
                            Type               = type
                        });
                    }

                    sb.AppendLine($"Successfully imported {cam.Type} {cam.Make} {cam.Model}");
                }
            }

            using (var context = new PhotographyWorkshopsContext())
            {
                context.DSLRCameras.AddRange(dslrCameras);
                context.MirrorlessCameras.AddRange(mirrorlessCameras);
                context.SaveChanges();
            }

            Console.WriteLine(sb.ToString());
        }