public static void Init()
 {
     using (var context = new PhotographyWorkshopsContext())
     {
         //context.Database.Initialize(true);
         context.Workshops.Add(new Workshop()
         {
             Name                = "a",
             Location            = "a",
             PricePerParticipant = 1m,
             Trainer             = new Photographer()
             {
                 FirstName     = "a",
                 LastName      = "aa",
                 PrimaryCamera = new DSLRCamera()
                 {
                     Make   = "aaa",
                     Model  = "aaa",
                     MinISO = 100
                 },
                 SecondaryCamera = new DSLRCamera()
                 {
                     Make   = "aaaaaaa",
                     Model  = "aaaaaaab",
                     MinISO = 1000
                 }
             }
         });
         context.SaveChanges();
     }
 }
예제 #2
0
        public void ImportAccessories()
        {
            XDocument accessoriesXml = XDocument.Load(@"..\..\..\PhotographyWorkshops.Data\Imports\accessories.xml");

            List <Accessory> accessories = new List <Accessory>();
            StringBuilder    sb          = new StringBuilder();

            foreach (var acc in accessoriesXml.Root.Elements())
            {
                var nameEle = acc.Attribute("name");
                if (nameEle != null)
                {
                    Accessory newAcc = new Accessory
                    {
                        Name    = acc.Attribute("name").Value,
                        OwnerId = GetRandomOwnerId()
                    };
                    accessories.Add(newAcc);
                    sb.AppendLine($"Successfully imported {newAcc.Name}");
                }
            }

            using (var context = new PhotographyWorkshopsContext())
            {
                context.Accessories.AddRange(accessories);
                context.SaveChanges();
            }

            Console.WriteLine(sb.ToString());
        }
예제 #3
0
        public void ImportPhotographers()
        {
            //firstname & lastname -> required
            //phone to be validated
            List <PhotographerDTO> photographers = new List <PhotographerDTO>();

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

            photographers = JsonConvert.DeserializeObject <List <PhotographerDTO> >(json);

            StringBuilder sb = new StringBuilder();

            List <Photographer> photograpersWithLenses = new List <Photographer>();

            var context = new PhotographyWorkshopsContext();

            foreach (var ph in photographers)
            {
                if (ph.FirstName == null || ph.LastName == null)
                {
                    sb.AppendLine($"Error. Invalid data provided");
                }
                else
                {
                    if (ph.Phone != null)
                    {
                        if (!Regex.IsMatch(ph.Phone, @"^\+[\d]{1,3}\/[\d]{8,10}$"))
                        {
                            sb.AppendLine($"Error. Invalid data provided");
                            continue;
                        }
                    }

                    var photographer = new Photographer
                    {
                        FirstName       = ph.FirstName,
                        LastName        = ph.LastName,
                        Phone           = ph.Phone,
                        PrimaryCamera   = GetRandomCamera(context),
                        SecondaryCamera = GetRandomCamera(context)
                    };


                    photographer.Lenses = GetLensesFromIdArray(context, ph.Lenses, photographer.PrimaryCamera.Make, photographer.SecondaryCamera.Make);


                    photograpersWithLenses.Add(photographer);

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


            context.Photographers.AddRange(photograpersWithLenses);

            context.SaveChanges();


            Console.WriteLine(sb.ToString());
        }
예제 #4
0
        public void ImportLenses()
        {
            List <Lens> lenses = new List <Lens>();

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

            lenses = JsonConvert.DeserializeObject <List <Lens> >(json);

            using (var context = new PhotographyWorkshopsContext())
            {
                context.Lenses.AddRange(lenses);
                context.SaveChanges();
            }

            StringBuilder sb = new StringBuilder();

            if (lenses.Count > 0)
            {
                var last = lenses.Last();
                foreach (var lens in lenses)
                {
                    if (lens != last)
                    {
                        sb.AppendLine($"Successfully imported {lens.Make} {lens.FocalLength}mm f{lens.MaxAperture:F1}");
                    }
                    else
                    {
                        sb.Append($"Successfully imported {lens.Make} {lens.FocalLength}mm f{lens.MaxAperture:F1}");
                    }
                }
            }
            Console.WriteLine(sb.ToString());
        }
예제 #5
0
        public void ExportSameCamerasPhotographers()
        {
            using (var context = new PhotographyWorkshopsContext())
            {
                var photographers = context.Photographers
                                    .Where(p => p.PrimaryCamera.Make == p.SecondaryCamera.Make)
                                    .ToList();

                var xml = new XElement("photographers");

                foreach (var ph in photographers)
                {
                    var phNode = new XElement("photographer");
                    phNode.Add(new XAttribute("name", $"{ph.FirstName} {ph.LastName}"));
                    phNode.Add(new XAttribute("primary-camera", $"{ph.PrimaryCamera.Make} {ph.PrimaryCamera.Model}"));

                    if (ph.Lenses.Count > 0)
                    {
                        var lensesNode = new XElement("lenses");
                        foreach (var lens in ph.Lenses)
                        {
                            var lensNode = new XElement("lens");
                            lensNode.Value = $"{lens.Make} {lens.FocalLength}mm f{lens.MaxAperture}";
                            lensesNode.Add(lensNode);
                        }
                        phNode.Add(lensesNode);
                    }
                    xml.Add(phNode);
                }
                xml.Save(@"..\..\..\PhotographyWorkshops.Data\Exports\same-cameras-photographers.xml");
            }
        }
예제 #6
0
        public void ExportOrderedPhotographers()
        {
            using (var context = new PhotographyWorkshopsContext())
            {
                var photographers = context.Photographers
                                    .Select(p => new
                {
                    p.FirstName,
                    p.LastName,
                    p.Phone
                })
                                    .OrderBy(o => o.FirstName)
                                    .ThenByDescending(o => o.LastName)
                                    .ToList();

                JsonSerializerSettings settings = new JsonSerializerSettings
                {
                    Formatting       = Formatting.Indented,
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                };

                var json = JsonConvert.SerializeObject(photographers, settings);

                File.WriteAllText(@"..\..\..\PhotographyWorkshops.Data\Exports\photographers-ordered.json", json);
            }
        }
예제 #7
0
        public void ExportLandscapePhotographers()
        {
            using (var context = new PhotographyWorkshopsContext())
            {
                var photographers = context.Photographers
                                    .Where(p => p.PrimaryCamera.Type == Models.CameraType.DSLR && p.Lenses.All(l => l.FocalLength <= 30))
                                    .Select(p => new
                {
                    p.FirstName,
                    p.LastName,
                    CameraMake  = p.PrimaryCamera.Make,
                    LensesCount = p.Lenses.Count
                })
                                    .OrderBy(o => o.FirstName)
                                    .ToList();

                JsonSerializerSettings settings = new JsonSerializerSettings
                {
                    Formatting       = Formatting.Indented,
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                };

                var json = JsonConvert.SerializeObject(photographers, settings);

                File.WriteAllText(@"..\..\..\PhotographyWorkshops.Data\Exports\landscape-photogaphers.json", json);
            }
        }
예제 #8
0
 public static void InitDb()
 {
     using (var context = new PhotographyWorkshopsContext())
     {
         context.Database.Initialize(true);
     }
 }
예제 #9
0
        private int GetRandomOwnerId()
        {
            using (var context = new PhotographyWorkshopsContext())
            {
                var photographersIds = context.Photographers.Select(p => p.Id).ToList();

                return(photographersIds[rnd.Next(0, photographersIds.Count - 1)]);
            }
        }
예제 #10
0
        public void ExportWorkshopsByLocation()
        {
            using (var context = new PhotographyWorkshopsContext())
            {
                var workshopsBylocation = context.Workshops
                                          .GroupBy(g => g.Location, ws => ws, (location, workshops) => new
                {
                    Location  = location,
                    Workshops = workshops.Where(ws => ws.Participants.Count >= 5)
                })
                                          .Where(ws => ws.Workshops.Count() > 0)
                                          .Select(wsBylocation => new
                {
                    Location  = wsBylocation.Location,
                    WorkShops = wsBylocation.Workshops
                })
                                          .ToList();

                var xml = new XElement("locations");

                foreach (var location in workshopsBylocation)
                {
                    var locNode = new XElement("location");
                    locNode.Add(new XAttribute("name", $"{location.Location}"));
                    foreach (var w in location.WorkShops)
                    {
                        var wsNode = new XElement("workshop");
                        wsNode.Add(new XAttribute("name", $"{w.Name}"));
                        wsNode.Add(new XAttribute("total-profit", ((w.Participants.Count * w.PricePerParticipant) * 0.80M)));
                        var participantsNode = new XElement("participants");
                        participantsNode.Add(new XAttribute("count", w.Participants.Count));
                        foreach (var p in w.Participants)
                        {
                            var participantNode = new XElement("participant");
                            participantNode.Value = $"{p.FirstName} {p.LastName}";
                            participantsNode.Add(participantNode);
                        }
                        wsNode.Add(participantsNode);
                        locNode.Add(wsNode);
                    }
                    xml.Add(locNode);
                }

                xml.Save(@"..\..\..\PhotographyWorkshops.Data\Exports\workshops-by-location.xml");
            }
        }
예제 #11
0
        private ICollection <Lens> GetLensesFromIdArray(PhotographyWorkshopsContext context, int[] lenses, string makePrimary, string makeSecondary)
        {
            List <Lens> lensesToadd = new List <Lens>();

            foreach (var id in lenses)
            {
                if (context.Lenses.Any(l => l.Id == id))
                {
                    var lens = context.Lenses.FirstOrDefault(l => l.Id == id);

                    if (lens.CompatibleWith == makePrimary || lens.CompatibleWith == makeSecondary)
                    {
                        lensesToadd.Add(lens);
                    }
                }
            }

            return(lensesToadd);
        }
예제 #12
0
        private Camera GetRandomCamera(PhotographyWorkshopsContext context)
        {
            var dslrcameras       = context.DSLRCameras.ToList();
            var mirrorlesscameras = context.MirrorlessCameras.ToList();



            int randomCameraType = rnd.Next(0, 1);

            if (randomCameraType == 0)
            {
                int rndDslr = rnd.Next(0, dslrcameras.Count - 1);
                return(dslrcameras[rndDslr]);
            }
            else
            {
                int rndMrrls = rnd.Next(0, mirrorlesscameras.Count - 1);
                return(mirrorlesscameras[rndMrrls]);
            }
        }
예제 #13
0
        public void ImportWorkshops()
        {
            //workshopName, location, price, trainer -> required
            XDocument       workshopsXml = XDocument.Load(@"..\..\..\PhotographyWorkshops.Data\Imports\workshops.xml");
            StringBuilder   sb           = new StringBuilder();
            List <Workshop> workshops    = new List <Workshop>();

            using (var context = new PhotographyWorkshopsContext())
            {
                foreach (var ws in workshopsXml.Root.Elements())
                {
                    string       wsName    = null;
                    string       location  = null;
                    decimal      price     = 0;
                    Photographer trainer   = null;
                    DateTime?    startDate = new DateTime();
                    DateTime?    endDate   = new DateTime();

                    var wsNameAtr      = ws.Attribute("name");
                    var wsLocationAtr  = ws.Attribute("location");
                    var wsPriceAtr     = ws.Attribute("price");
                    var wsTrainer      = ws.Element("trainer");
                    var wsStartDateAtr = ws.Attribute("start-date");
                    var wsEndDateAtr   = ws.Attribute("end-date");

                    var wsParticipantsEle = ws.Element("participants");

                    if (wsNameAtr != null && wsLocationAtr != null && wsPriceAtr != null && wsTrainer != null)
                    {
                        wsName   = wsNameAtr.Value;
                        location = wsLocationAtr.Value;
                        price    = decimal.Parse(wsPriceAtr.Value);
                        trainer  = GetTrainerByFullName(context, wsTrainer.Value);

                        Workshop newWorkshop = new Workshop
                        {
                            Name                = wsName,
                            Location            = location,
                            PricePerParticipant = price,
                            Trainer             = trainer
                        };

                        if (wsStartDateAtr != null)
                        {
                            startDate             = DateTime.Parse(wsStartDateAtr.Value);
                            newWorkshop.StartDate = startDate;
                        }

                        if (wsEndDateAtr != null)
                        {
                            endDate             = DateTime.Parse(wsEndDateAtr.Value);
                            newWorkshop.EndDate = endDate;
                        }

                        if (wsParticipantsEle != null)
                        {
                            foreach (var participant in wsParticipantsEle.Elements())
                            {
                                string firstName = participant.Attribute("first-name").Value;
                                string lastName  = participant.Attribute("last-name").Value;

                                newWorkshop.Participants.Add(GetParticipant(context, firstName, lastName));
                            }
                        }

                        workshops.Add(newWorkshop);
                        sb.AppendLine($"Successfully imported {newWorkshop.Name}");
                    }
                    else
                    {
                        sb.AppendLine($"Error. Invalid data provided");
                    }
                }
                context.Workshops.AddRange(workshops);
                context.SaveChanges();
            }
            Console.WriteLine(sb.ToString());
        }
예제 #14
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());
        }
예제 #15
0
 private Photographer GetTrainerByFullName(PhotographyWorkshopsContext context, string trainerFullname)
 {
     return(context.Photographers.FirstOrDefault(p => (p.FirstName + " " + p.LastName) == trainerFullname));
 }
예제 #16
0
 private Photographer GetParticipant(PhotographyWorkshopsContext context, string firstName, string lastName)
 {
     return(context.Photographers.FirstOrDefault(p => p.FirstName == firstName && p.LastName == lastName));
 }