public static List <GuestListDTO> GetGuestLists() { using (var context = new WeddingsPlannerContext()) { var guestLists = context.Weddings .Include("Bride") .Include("Bridegroom") .Include("Invitations") .ToList() .Select(w => new GuestListDTO() { Bride = GetFullName(w.Bride), Bridegroom = GetFullName(w.Bride), Agency = new AgencyShortDTO() { Name = w.Agency.Name, Town = w.Agency.Town }, InvitedGuests = w.Invitations.Count, BrideGuests = w.Invitations.Where(i => i.Family == Family.Bride).Count(), BridegroomGuests = w.Invitations.Where(i => i.Family == Family.Bridegroom).Count(), AtendingGuests = w.Invitations.Where(i => i.IsAttending == true).Count(), Guests = w.Invitations.Select(i => i.Guest.FullName).ToList() }).ToList(); return(guestLists); } }
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 ExportGuestList() { using (WeddingsPlannerContext context = new WeddingsPlannerContext()) { List <GuestsListDto> guestsList = context.Weddings .Select(wedding => new GuestsListDto { Bride = wedding.Bride.FirstName + " " + wedding.Bride.MiddleNameInitial + " " + wedding.Bride.LastName, Bridegroom = wedding.Bridegroom.FirstName + " " + wedding.Bridegroom.MiddleNameInitial + " " + wedding.Bridegroom.LastName, AgencyDto = new AgencyDto { Name = wedding.Agency.Name, Town = wedding.Agency.Town }, InvitedGuests = wedding.Invitations.Count, BrideGuests = wedding.Invitations.Count(inv => inv.Family == Family.Bride), BridegroomGuests = wedding.Invitations.Count(inv => inv.Family == Family.Bridegroom), AttendingGuests = wedding.Invitations.Count(inv => inv.IsAttending), GuestsNames = wedding.Invitations .Where(inv => inv.IsAttending) .Select(inv => inv.Guest.FirstName + " " + inv.Guest.MiddleNameInitial + " " + inv.Guest.LastName) .ToList() }) .OrderByDescending(wedding => wedding.InvitedGuests) .ThenBy(wedding => wedding.AttendingGuests) .ToList(); ExportJsonToFolder(guestsList, Constants.GuestsListPath); } }
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 ExportVenuesInSofia() { using (WeddingsPlannerContext context = new WeddingsPlannerContext()) { // With 'Serializer' List <VenueDto> venuesInSofia = context.Venues .Where(venue => venue.Weddings.Count >= 3 && venue.Town != "Sofia") .Select(venue => new VenueDto { Name = venue.Name, Capacity = venue.Capacity, WeddingsCount = venue.Weddings.Count }) .OrderBy(venue => venue.Capacity) .ToList(); ExportXmlToFolder(venuesInSofia, Constants.SofiaVenuesPath); // Without 'Serializer' //Venues which are not in Sofia because there are no venues in Sofia in the database //var venuesInSofia = context.Venues // .Where(venue => venue.Weddings.Count >= 3 && venue.Town != "Sofia") // .OrderBy(venue => venue.Capacity); //XDocument documentNode = new XDocument(); //XElement venuesNode = new XElement("venues"); //venuesNode.SetAttributeValue("town", "Sofia"); //foreach (Venue venueInSofia in venuesInSofia) //{ // XElement venueNode = new XElement("venue"); // venueNode.SetAttributeValue("name", venueInSofia.Name); // venueNode.SetAttributeValue("capacity", venueInSofia.Capacity); // venueNode.SetElementValue("weddings-count", venueInSofia.Weddings.Count); // venuesNode.Add(venueNode); //} //documentNode.Add(venuesNode); //documentNode.Save(Constants.SofiaVenuesPath); } }
public static List <AgencieDTO> GetAgencies() { using (var context = new WeddingsPlannerContext()) { var agencies = context.Agencies .Select(a => new AgencieDTO() { Name = a.Name, EmployeesCount = a.EmployeesCount, Town = a.Town }) .OrderByDescending(a => a.EmployeesCount) .ThenBy(a => a.Name) .ToList(); return(agencies); } }
public static void ExportOrderedAgencies() { using (WeddingsPlannerContext context = new WeddingsPlannerContext()) { List <OrderedAgenciesDto> orderedAgencies = context.Agencies .OrderByDescending(agency => agency.EmployeesCount) .ThenBy(agency => agency.Name) .Select(agency => new OrderedAgenciesDto { Name = agency.Name, Count = agency.EmployeesCount, Town = agency.Town }) .ToList(); ExportJsonToFolder(orderedAgencies, Constants.AgenciesOrderedPath); } }
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 ExportAgenciesByTown() { using (WeddingsPlannerContext context = new WeddingsPlannerContext()) { // With 'Serializer' List <TownDto> agenciesByTown = context.Agencies .Where(agency => agency.Town.Length >= 6) .GroupBy(agency => agency.Town, agency => agency, (town, agencies) => new { Town = town, Agencies = agencies.Where(agency => agency.Weddings.Count >= 2) }) .Select(gr => new TownDto() { Name = gr.Town, AgenciesDtos = gr.Agencies.Select(agency => new AgencyInTownDto() { Name = agency.Name, Profit = agency.Weddings .Sum(wedding => wedding.Invitations .Where(inv => (inv.Present as Cash) != null) .Sum(inv => (inv.Present as Cash).Amount) ) * 0.2m, Weddings = agency.Weddings.Select(wedding => new WeddingDto() { Cash = (wedding.Invitations .Where(inv => (inv.Present as Cash) != null) .Sum(inv => (decimal?)(inv.Present as Cash).Amount) ?? 0.0m), Present = wedding.Invitations.Count(inv => (inv.Present as Gift) != null), Bride = wedding.Bride.FirstName + " " + wedding.Bride.MiddleNameInitial + " " + wedding.Bride.LastName, Bridegroom = wedding.Bridegroom.FirstName + " " + wedding.Bridegroom.MiddleNameInitial + " " + wedding.Bridegroom.LastName, Guests = wedding.Invitations.Where(inv => inv.IsAttending).Select(guest => new GuestDto() { Family = guest.Family.ToString(), Name = guest.Guest.FirstName + " " + guest.Guest.MiddleNameInitial + " " + guest.Guest.LastName, }).ToList() }).ToList() }).ToList() }).ToList(); ExportXmlToFolder(agenciesByTown, Constants.AgenciesByTownPath); // Without 'Serializer' //var agenciesByTown = context.Agencies // .Where(agency => agency.Town.Length >= 6) // .GroupBy(agency => agency.Town, agency => agency, (town, agencies) => new // { // Town = town, // Agencies = agencies.Where(agency => agency.Weddings.Count >= 2) // }); //XDocument documentNode = new XDocument(); //XElement townsNode = new XElement("towns"); //foreach (var agencyByTown in agenciesByTown) //{ // XElement townNode = new XElement("town"); // townNode.SetAttributeValue("name", agencyByTown.Town); // XElement agenciesNode = new XElement("agencies"); // foreach (Agency agency in agencyByTown.Agencies) // { // XElement agencyNode = new XElement("agency"); // agencyNode.SetAttributeValue("name", agency.Name); // agencyNode.SetAttributeValue("profit", GetAgencyProfit(agency.Weddings)); // foreach (Wedding wedding in agency.Weddings) // { // XElement weddingNode = new XElement("wedding"); // weddingNode.SetAttributeValue("cash", GetWeddingCash(wedding.Invitations)); // weddingNode.SetAttributeValue("presents", GetWeddingGiftsCount(wedding.Invitations)); // weddingNode.SetElementValue("bride", wedding.Bride.FullName); // weddingNode.SetElementValue("bridegroom", wedding.Bridegroom.FullName); // XElement guestsNode = new XElement("guests"); // foreach (Invitation invitation in wedding.Invitations.Where(inv => inv.IsAttending)) // { // XElement guestNode = new XElement("guest"); // guestNode.SetAttributeValue("family", invitation.Family); // guestNode.SetValue(invitation.Guest.FullName); // guestsNode.Add(guestNode); // } // weddingNode.Add(guestsNode); // agencyNode.Add(weddingNode); // } // agenciesNode.Add(agencyNode); // } // townNode.Add(agenciesNode); // townsNode.Add(townNode); //} //documentNode.Add(townsNode); //documentNode.Save(Constants.AgenciesByTownPath); } }
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); } } } } }