public static void StoreVenues(IEnumerable <XElement> venuesXml)
        {
            using (var context = new WeddingsPlannerContext())
            {
                List <Venue> venues = new List <Venue>();

                foreach (var v in venuesXml)
                {
                    var currentVenue = new Venue()
                    {
                        Name     = v.Attribute("name").Value,
                        Capacity = int.Parse(v.Element("capacity").Value),
                        Town     = v.Element("town").Value
                    };

                    venues.Add(currentVenue);
                    Console.WriteLine($"Successfully imported {currentVenue.Name}");
                }
                context.Venues.AddRange(venues);
                context.SaveChanges();

                Random rnd         = new Random();
                var    venuesCount = context.Venues.Count();

                foreach (var w in context.Weddings)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        w.Venues.Add(context.Venues.Find(rnd.Next(1, venuesCount)));
                    }
                }
                context.SaveChanges();
            }
        }
        public static void ImportVenues()
        {
            using (WeddingsPlannerContext context = new WeddingsPlannerContext())
            {
                XDocument documentNode = XDocument.Load(Constants.VenuesPath);

                IEnumerable <XElement> venuesNode = documentNode.XPathSelectElements("venues/venue");

                foreach (XElement venueNode in venuesNode)
                {
                    string venueNodeName         = venueNode.Attribute("name")?.Value;
                    int    venueNodeNameCapacity = int.Parse(venueNode.Element("capacity").Value);
                    string venueNodeTown         = venueNode.Element("town")?.Value;

                    VenueDto venueDto = new VenueDto()
                    {
                        Name     = venueNodeName,
                        Capacity = venueNodeNameCapacity,
                        Town     = venueNodeTown
                    };

                    Venue venueEntity = new Venue()
                    {
                        Name     = venueDto.Name,
                        Capacity = venueDto.Capacity,
                        Town     = venueDto.Town
                    };

                    try
                    {
                        context.Venues.Add(venueEntity);
                        context.SaveChanges();
                        Console.WriteLine($"Successfully imported {venueEntity.Name}");
                    }
                    catch (DbEntityValidationException)
                    {
                        context.Venues.Remove(venueEntity);
                        Console.WriteLine(Messages.InvalidData);
                    }
                }

                Random random = new Random();
                foreach (Wedding wedding in context.Weddings.ToList())
                {
                    int   randomId = random.Next(1, context.Venues.Count() + 1);
                    Venue venue    = context.Venues.Find(randomId);

                    wedding.Venues.Add(venue);

                    randomId = random.Next(1, context.Venues.Count() + 1);
                    venue    = context.Venues.Find(randomId);
                    wedding.Venues.Add(venue);
                }

                context.SaveChanges();
            }
        }
        public static void StoreWeddings(IEnumerable <WeddingDTO> weddings)
        {
            using (var context = new WeddingsPlannerContext())
            {
                foreach (var w in weddings)
                {
                    var bride      = context.People.FirstOrDefault(p => p.FirstName + " " + p.MiddleNameInitial + " " + p.LastName == w.Bride);
                    var bridegroom = context.People.FirstOrDefault(p => p.FirstName + " " + p.MiddleNameInitial + " " + p.LastName == w.Bridegroom);
                    var agency     = context.Agencies.FirstOrDefault(a => a.Name == w.Agency);

                    if (bride == null || bridegroom == null || agency == null)
                    {
                        Console.WriteLine("Error. Invalid data provided.");
                        continue;
                    }

                    var currentWedding = new Wedding()
                    {
                        Bride      = bride,
                        Bridegroom = bridegroom,
                        Agency     = agency,
                        Date       = w.Date
                    };

                    if (w.Guests != null)
                    {
                        foreach (var g in w.Guests)
                        {
                            var guest =
                                context.People.FirstOrDefault(
                                    p => p.FirstName + " " + p.MiddleNameInitial + " " + p.LastName == g.Name);
                            if (guest != null)
                            {
                                currentWedding.Invitations.Add(new Invitation()
                                {
                                    Family      = g.Family,
                                    IsAttending = g.RSVP,
                                    Guest       = guest
                                });
                            }
                        }
                    }

                    try
                    {
                        context.Weddings.Add(currentWedding);
                        context.SaveChanges();
                        Console.WriteLine($"Successfully imported wedding of {currentWedding.Bride.FirstName} and {currentWedding.Bridegroom.FirstName}");
                    }
                    catch (DbEntityValidationException)
                    {
                        Console.WriteLine("Error. Invalid data provided.");
                    }
                }
            }
        }
        public static void ImportAgencies()
        {
            using (WeddingsPlannerContext context = new WeddingsPlannerContext())
            {
                IEnumerable <AgencyDto> agenciesListDto = ParseJson <AgencyDto>(Constants.AgenciesPath);

                foreach (AgencyDto agencyDto in agenciesListDto)
                {
                    Agency agencyEntity = new Agency()
                    {
                        Name           = agencyDto.Name,
                        EmployeesCount = agencyDto.EmployeesCount,
                        Town           = agencyDto.Town
                    };

                    context.Agencies.Add(agencyEntity);
                    context.SaveChanges();
                    Console.WriteLine($"Successfully imported {agencyEntity.Name}!");
                }
            }
        }
        public static void StoreAgencies(IEnumerable <AgencieDTO> agencies)
        {
            using (var context = new WeddingsPlannerContext())
            {
                foreach (var agencie in agencies)
                {
                    Agency currentAgencie = new Agency()
                    {
                        Name           = agencie.Name,
                        EmployeesCount = agencie.EmployeesCount,
                        Town           = agencie.Town
                    };

                    context.Agencies.Add(currentAgencie);

                    Console.WriteLine($"Successfully imported {agencie.Name}");
                }

                context.SaveChanges();
            }
        }
        public static void StorePeople(IEnumerable <PersonDTO> people)
        {
            using (var context = new WeddingsPlannerContext())
            {
                foreach (var person in people)
                {
                    if (person.FirstName == null ||
                        person.MiddleInitial == null ||
                        person.LastName == null)
                    {
                        Console.WriteLine("Error. Invalid data provided");
                        continue;
                    }

                    var currentPerson = new Person()
                    {
                        FirstName         = person.FirstName,
                        MiddleNameInitial = person.MiddleInitial,
                        LastName          = person.LastName,
                        Gender            = person.Gender,
                        BirthDate         = person.BirthDay,
                        Phone             = person.Phone,
                        Email             = person.Email
                    };

                    try
                    {
                        context.People.Add(currentPerson);
                        context.SaveChanges();
                        Console.WriteLine($"Successfully imported {person.FirstName} {person.MiddleInitial} {person.LastName}");
                    }
                    catch (DbEntityValidationException)
                    {
                        context.People.Remove(currentPerson);
                        Console.WriteLine("Error. Invalid data provided");
                    }
                }
            }
        }
        public static void ImportPeople()
        {
            using (WeddingsPlannerContext context = new WeddingsPlannerContext())
            {
                IEnumerable <PersonDto> personsListDto = ParseJson <PersonDto>(Constants.PeoplePath);

                List <Person> people = new List <Person>();

                foreach (PersonDto personDto in personsListDto)
                {
                    // We can make the checks for FirstName, MiddleName LastName Length and Email Pattern here,
                    // add everything to a List<Person> and save it in the Database with context.SaveChanges() and no errors

                    // OR

                    // In order to use all the attribute validations which we have implemented in the models,
                    // we have to use a try-catch construction with Context.SaveChanges so can the attributes to work as expected;

                    if (personDto.FirstName == null || personDto.FirstName.Length > 60)
                    {
                        Console.WriteLine(Messages.InvalidData);
                        continue;
                    }

                    if (personDto.MiddleInitial == null || personDto.MiddleInitial.Length != 1)
                    {
                        Console.WriteLine(Messages.InvalidData);
                        continue;
                    }

                    if (personDto.LastName == null || personDto.LastName.Length < 2)
                    {
                        Console.WriteLine(Messages.InvalidData);
                        continue;
                    }

                    Regex regex = new Regex(@"^[a-zA-Z0-9]+@[a-z]{1,}.[a-z]{1,}$");
                    if (personDto.Email != null && !regex.IsMatch(personDto.Email))
                    {
                        Console.WriteLine(Messages.InvalidData);
                        continue;
                    }

                    Gender gender;
                    bool   isGenderValid = Enum.TryParse(personDto.Gender.ToString(), out gender);

                    if (!isGenderValid)
                    {
                        gender = Gender.NotSpecified;
                    }

                    Person personEntity = new Person()
                    {
                        FirstName         = personDto.FirstName,
                        MiddleNameInitial = personDto.MiddleInitial,
                        LastName          = personDto.LastName,
                        Gender            = gender,
                        BirthDate         = personDto.Birthday,
                        Phone             = personDto.Phone,
                        Email             = personDto.Email
                    };

                    people.Add(personEntity);
                    Console.WriteLine($"Successfully imported {personEntity.FullName}!");
                }

                context.People.AddRange(people);
                context.SaveChanges();

                // We have to use this syntax in order all the models attributes to work as expected:
                //
                //try
                //{
                //    context.People.Add(personEntity);
                //    context.SaveChanges();
                //    Console.WriteLine($"Successfully imported {personEntity.FullName}!");
                //}
                //catch (DbEntityValidationException)
                //{
                //    context.People.Remove(personEntity);
                //    Console.WriteLine(Messages.InvalidData);
                //}
            }
        }
        public static void ImportWeddings()
        {
            using (WeddingsPlannerContext context = new WeddingsPlannerContext())
            {
                IEnumerable <WeddingDto> weddingsListDto = ParseJson <WeddingDto>(Constants.WeddingsPath);

                foreach (WeddingDto weddingDto in weddingsListDto)
                {
                    Person bride = context.People.FirstOrDefault(person =>
                                                                 person.FirstName + " " + person.MiddleNameInitial + " " + person.LastName == weddingDto.Bride);
                    Person bridesgroom = context.People.FirstOrDefault(person =>
                                                                       person.FirstName + " " + person.MiddleNameInitial + " " + person.LastName == weddingDto.Bridegroom);
                    Agency agency = context.Agencies.FirstOrDefault(ag => ag.Name == weddingDto.Agency);

                    if (bride == null || bridesgroom == null || weddingDto.Date == null || agency == null)
                    {
                        Console.WriteLine(Messages.InvalidData);
                        continue;
                    }

                    Wedding weddingEntity = new Wedding()
                    {
                        Bride      = bride,
                        Bridegroom = bridesgroom,
                        Date       = weddingDto.Date.Value,
                        Agency     = agency
                    };

                    foreach (GuestDto guestDto in weddingDto.Guests)
                    {
                        Person guest = context.People.FirstOrDefault(person =>
                                                                     person.FirstName + " " + person.MiddleNameInitial + " " + person.LastName == guestDto.Name);

                        Family family;
                        bool   isGenderValid = Enum.TryParse(guestDto.Family.ToString(), out family);

                        if (!isGenderValid)
                        {
                            Console.WriteLine(Messages.InvalidData);
                            continue;
                        }

                        if (guest != null)
                        {
                            Invitation invitation = new Invitation()
                            {
                                Guest       = guest,
                                IsAttending = guestDto.RSVP,
                                Family      = family
                            };

                            weddingEntity.Invitations.Add(invitation);
                        }
                    }

                    try
                    {
                        context.Weddings.Add(weddingEntity);
                        context.SaveChanges();
                        Console.WriteLine($"Successfully imported wedding of {bride.FirstName} and {bridesgroom.FirstName}");
                    }
                    catch (DbEntityValidationException)
                    {
                        context.Weddings.Remove(weddingEntity);
                        Console.WriteLine(Messages.InvalidData);
                    }
                }
            }
        }
        public static void StorePresents(IEnumerable <XElement> presentsXml)
        {
            using (var context = new WeddingsPlannerContext())
            {
                foreach (var p in presentsXml)
                {
                    var type            = p.Attribute("type");
                    var invitationIdXml = p.Attribute("invitation-id");
                    if (type == null || invitationIdXml == null)
                    {
                        Console.WriteLine("Error. Invalid data provided");
                        continue;
                    }

                    var invitationId = int.Parse(invitationIdXml.Value);
                    if (invitationId < 0 || invitationId > context.Invitations.Count())
                    {
                        Console.WriteLine("Error. Invalid data provided");
                        continue;
                    }

                    if (type.Value == "cash")
                    {
                        var amount = p.Attribute("amount");
                        if (amount == null)
                        {
                            Console.WriteLine("Error. Invalid data provided");
                            continue;
                        }

                        Cash present = new Cash()
                        {
                            CashAmount = decimal.Parse(amount.Value)
                        };

                        var invitation = context.Invitations.Find(invitationId);
                        invitation.Present = present;
                        try
                        {
                            context.SaveChanges();
                            Console.WriteLine($"Succesfully imported gift from {invitation.Guest.FullName}");
                        }
                        catch (DbEntityValidationException)
                        {
                            Console.WriteLine("Error. Invalid data provided");
                        }
                    }
                    else if (type.Value == "gift")
                    {
                        var nameXml = p.Attribute("present-name");
                        if (nameXml == null)
                        {
                            Console.WriteLine("Error. Invalid data provided");
                            continue;
                        }

                        var presentSize = p.Attribute("size");
                        var size        = PresentSize.NotSpecified;
                        if (presentSize != null)
                        {
                            if (!Enum.TryParse(presentSize.Value, out size))
                            {
                                Console.WriteLine("Error. Invalid data provided");
                                continue;
                            }
                        }

                        var gift = new Gift()
                        {
                            Size = size,
                            Name = nameXml.Value
                        };

                        var invitation = context.Invitations.Find(invitationId);
                        invitation.Present = gift;
                        try
                        {
                            context.SaveChanges();
                            Console.WriteLine($"Succesfully imported gift from {invitation.Guest.FullName}");
                        }
                        catch (DbEntityValidationException)
                        {
                            Console.WriteLine("Error. Invalid data provided");
                        }
                    }
                }
            }
        }
        public static void ImportPresents()
        {
            using (WeddingsPlannerContext context = new WeddingsPlannerContext())
            {
                XDocument documentNode = XDocument.Load(Constants.PresentsPath);

                IEnumerable <XElement> presentsNode = documentNode.XPathSelectElements("presents/present");

                foreach (XElement presentNode in presentsNode)
                {
                    string type   = presentNode.Attribute("type")?.Value;
                    string id     = presentNode.Attribute("invitation-id")?.Value;
                    string name   = presentNode.Attribute("present-name")?.Value;
                    string size   = presentNode.Attribute("size")?.Value;
                    string amount = presentNode.Attribute("amount")?.Value;

                    if (type == null || id == null)
                    {
                        Console.WriteLine(Messages.InvalidData);
                        continue;
                    }

                    int invitationId     = int.Parse(presentNode.Attribute("invitation-id").Value);
                    int invitationsCount = context.Invitations.Count();

                    if (invitationId < 0 || invitationId > invitationsCount)
                    {
                        Console.WriteLine(Messages.InvalidData);
                        continue;
                    }

                    Invitation invitation = context.Invitations.FirstOrDefault(inv => inv.Id == invitationId);
                    if (invitation == null)
                    {
                        Console.WriteLine(Messages.InvalidData);
                        continue;
                    }

                    if (type == "cash")
                    {
                        if (amount == null)
                        {
                            Console.WriteLine(Messages.InvalidData);
                            continue;
                        }

                        PresentDto cashDto = new PresentDto()
                        {
                            Amount       = decimal.Parse(amount),
                            InvitationId = invitationId
                        };

                        Cash cashEntity = new Cash()
                        {
                            Amount     = cashDto.Amount,
                            Invitation = context.Invitations.Find(cashDto.InvitationId)
                        };

                        try
                        {
                            context.Presents.Add(cashEntity);
                            invitation.PresentId = cashEntity.InvitationId;
                            context.SaveChanges();
                            Console.WriteLine($"Succesfully imported cash from {invitation.Guest.FullName}!");
                        }
                        catch (DbEntityValidationException)
                        {
                            context.Presents.Remove(cashEntity);
                            Console.WriteLine(Messages.InvalidData);
                        }
                    }
                    else if (type == "gift")
                    {
                        if (name == null)
                        {
                            Console.WriteLine(Messages.InvalidData);
                            continue;
                        }

                        PresentSize presentSize = PresentSize.NotSpecified;

                        if (size != null)
                        {
                            bool isPresentSizeValid = Enum.TryParse(size, out presentSize);
                            if (!isPresentSizeValid)
                            {
                                Console.WriteLine(Messages.InvalidData);
                                continue;
                            }
                        }

                        PresentDto giftDto = new PresentDto()
                        {
                            InvitationId = invitationId,
                            PresentName  = name,
                            Size         = presentSize
                        };

                        Gift giftEntity = new Gift()
                        {
                            Name        = giftDto.PresentName,
                            Invitation  = context.Invitations.Find(giftDto.InvitationId),
                            PresentSize = giftDto.Size
                        };

                        try
                        {
                            context.Presents.Add(giftEntity);
                            invitation.PresentId = giftEntity.InvitationId;
                            context.SaveChanges();
                            Console.WriteLine($"Succesfully imported gift from {invitation.Guest.FullName}!");
                        }
                        catch (DbEntityValidationException)
                        {
                            context.Presents.Remove(giftEntity);
                            Console.WriteLine(Messages.InvalidData);
                        }
                    }
                }
            }
        }