public static void InitDb() { using (var context = new WeddingsPlannerContext()) { context.Database.Initialize(true); } }
public static void GuestList() { using (var context = new WeddingsPlannerContext()) { var guests = context.Weddings .Select(w => new { Bride = w.Bride.FirstName + " " + w.Bride.MiddleInitial + " " + w.Bride.LastName, Bridegroom = w.Bridegroom.FirstName + " " + w.Bridegroom.MiddleInitial + " " + w.Bridegroom.LastName, Agency = new { Name = w.Agency.Name, Town = w.Agency.Town }, InvitedGuests = w.Invitations.Count, BrideGuests = w.Invitations.Where(i => i.Family == Models.enums.Family.Bride).Count(), BridegroomGuests = w.Invitations.Where(i => i.Family == Models.enums.Family.Bridegroom).Count(), AttendingGuests = w.Invitations.Where(i => i.IsAttending == true).Count(), Guests = w.Invitations.Where(i => i.IsAttending == true).Select(g => g.Guest.FirstName + " " + g.Guest.MiddleInitial + " " + g.Guest.LastName) }) .OrderByDescending(o => o.InvitedGuests) .ThenBy(o => o.AttendingGuests) .ToList(); JsonSerializerSettings settings = new JsonSerializerSettings { Formatting = Formatting.Indented, ContractResolver = new CamelCasePropertyNamesContractResolver() }; var json = JsonConvert.SerializeObject(guests, settings); File.WriteAllText(@"..\..\..\WeddingsPlanner.Data\Exports\guests.json", json); } }
public static void OrderedAgencies() { using (var context = new WeddingsPlannerContext()) { var agencies = context.Agencies .Select(a => new { Name = a.Name, Count = a.EmployeesCount, Town = a.Town }) .OrderByDescending(o => o.Count) .ThenBy(o => o.Name) .ToList(); JsonSerializerSettings settings = new JsonSerializerSettings { Formatting = Formatting.Indented, ContractResolver = new CamelCasePropertyNamesContractResolver() }; var json = JsonConvert.SerializeObject(agencies, settings); File.WriteAllText(@"..\..\..\WeddingsPlanner.Data\Exports\agencies-ordered.json", json); } }
public UnitOfWork(WeddingsPlannerContext context) { this.context = context; this.Agencies = new AgencyRepository(context); this.Invitations = new InvitationRepository(context); this.People = new PersonRepository(context); this.Venues = new VenueRepository(context); this.Presents = new PresentRepository(context); this.Weddings = new WeddingRepository(context); }
public static void AgenciesByTown() { using (var context = new WeddingsPlannerContext()) { var agenciesByTown = context.Agencies .GroupBy(a => a.Town, at => at, (town, agencies) => new { Town = town, Agencies = agencies.Where(a => a.OrganizedWeddings.Count >= 2) }) .Where(a => a.Town.Length >= 6) .ToList(); var xml = new XElement("towns"); foreach (var at in agenciesByTown) { var townEle = new XElement("town"); townEle.Add(new XAttribute("name", at.Town)); var agenciesEle = new XElement("agencies"); foreach (var a in at.Agencies) { decimal profit = a.OrganizedWeddings .Sum(w => w.Invitations.Where(i => (i.Present as Cash) != null).Sum(i => (i.Present as Cash).CashAmount) * 0.2M); var agencyEle = new XElement("agency"); agencyEle.Add(new XAttribute("name", a.Name)); agencyEle.Add(new XAttribute("profit", profit)); foreach (var w in a.OrganizedWeddings) { var wedEle = new XElement("wedding"); wedEle.Add(new XAttribute("cash", w.Invitations.Where(i => i.Present as Cash != null).Sum(i => (i.Present as Cash).CashAmount))); wedEle.Add(new XAttribute("presents", w.Invitations.Where(i => i.Present as Gift != null).Select(g => g.Present as Gift).Where(g => g.Size != Models.enums.GiftSize.NotSpecified).Count())); wedEle.Add(new XElement("bride", w.Bride.FullName)); wedEle.Add(new XElement("bridegroom", w.Bridegroom.FullName)); var guestsEle = new XElement("guests"); foreach (var g in w.Invitations) { var guestEle = new XElement("guest"); guestEle.Add(new XAttribute("family", g.Family)); guestEle.Value = g.Guest.FirstName; guestsEle.Add(guestEle); } wedEle.Add(guestsEle); agencyEle.Add(wedEle); } agenciesEle.Add(agencyEle); } townEle.Add(agenciesEle); xml.Add(townEle); } xml.Save(@"..\..\..\WeddingsPlanner.Data\Exports\agencies-by-town.xml"); } }
private static Agency GetAgency(WeddingsPlannerContext context, string agency) { if (context.Agencies.Any(a => a.Name == agency)) { return(context.Agencies.FirstOrDefault(a => a.Name == agency)); } else { return(null); } }
private static Invitation GetInvitation(WeddingsPlannerContext context, int invId) { if (context.Invitations.Any(i => i.Id == invId)) { return(context.Invitations.FirstOrDefault(i => i.Id == invId)); } else { return(null); } }
public static void ImportAgencies() { List <Agency> agencies = new List <Agency>(); string json = File.ReadAllText(@"..\..\..\WeddingsPlanner.Data\Imports\agencies.json"); agencies = JsonConvert.DeserializeObject <List <Agency> >(json); using (var context = new WeddingsPlannerContext()) { context.Agencies.AddRange(agencies); context.SaveChanges(); } StringBuilder sb = new StringBuilder(); foreach (var agency in agencies) { sb.AppendLine($"Successfully imported {agency.Name}"); } Console.WriteLine(sb.ToString()); }
public static void VenuesSofia() { using (var context = new WeddingsPlannerContext()) { var venues = context.Venues .Where(v => v.Town == "Sofia" && v.Weddings.Count >= 3) .OrderBy(o => o.Capacity) .ToList(); var xml = new XElement("venues"); xml.Add(new XAttribute("town", "Sofia")); foreach (var v in venues) { var venueEle = new XElement("venue"); venueEle.Add(new XAttribute("name", v.Name)); venueEle.Add(new XAttribute("capacity", v.Capacity)); venueEle.Add(new XElement("weddings-count", v.Weddings.Count)); xml.Add(venueEle); } xml.Save(@"..\..\..\WeddingsPlanner.Data\Exports\sofia-venues.xml"); } }
public static void Init() { var context = new WeddingsPlannerContext(); //context.Database.Initialize(true); }
public static void ImportPresents() { //presentType, invitationId => required //if Cash amount must be specified //if gift name is required XDocument presentsXml = XDocument.Load(@"..\..\..\WeddingsPlanner.Data\Imports\presents.xml"); List <Present> presents = new List <Present>(); StringBuilder sb = new StringBuilder(); using (var context = new WeddingsPlannerContext()) { foreach (var p in presentsXml.Root.Elements()) { var typeAtr = p.Attribute("type"); var invIdAtr = p.Attribute("invitation-id"); if (typeAtr == null || invIdAtr == null) { sb.AppendLine($"Error. Invalid data provided"); } else { string type = typeAtr.Value; int invId = int.Parse(invIdAtr.Value); if (type == "cash") { var amountAtr = p.Attribute("amount"); if (amountAtr != null) { decimal amount = decimal.Parse(amountAtr.Value); Invitation inv = GetInvitation(context, invId); if (inv == null) { sb.AppendLine($"Error. Invalid data provided"); continue; } Present present = new Cash { Invitation = inv, CashAmount = amount }; presents.Add(present); sb.AppendLine($"Succesfully imported {type} from {inv.Guest.FullName}"); } else { sb.AppendLine($"Error. Invalid data provided"); } } else if (type == "gift") { var nameAtr = p.Attribute("present-name"); if (nameAtr != null) { string name = nameAtr.Value; Invitation inv = GetInvitation(context, invId); if (inv == null) { sb.AppendLine($"Error. Invalid data provided"); continue; } GiftSize size = GiftSize.NotSpecified; var sizeAtr = p.Attribute("size"); if (sizeAtr != null) { if (!Enum.TryParse(sizeAtr.Value, out size)) { sb.AppendLine($"Error. Invalid data provided"); continue; } } Present present = new Gift { Name = name, Invitation = inv, Size = size }; presents.Add(present); sb.AppendLine($"Succesfully imported {type} from {inv.Guest.FullName}"); } else { sb.AppendLine($"Error. Invalid data provided"); } } else { sb.AppendLine($"Error. Invalid data provided"); } } } context.Presents.AddRange(presents); context.SaveChanges(); Console.WriteLine(sb.ToString()); } }
public static void ImportPeople() { //firstname MiddleInit lastname -> required //gender to be validated => if null SEt to Not Specified List <PersonDTO> people = new List <PersonDTO>(); string json = File.ReadAllText(@"..\..\..\WeddingsPlanner.Data\Imports\people.json"); people = JsonConvert.DeserializeObject <List <PersonDTO> >(json); StringBuilder sb = new StringBuilder(); List <Person> newPeople = new List <Person>(); foreach (var personDTO in people) { if (personDTO.FirstName == null || personDTO.LastName == null || personDTO.MiddleInitial == null || personDTO.MiddleInitial.Length != 1 || personDTO.FirstName.Length < 1 || personDTO.FirstName.Length > 60 || personDTO.LastName.Length < 2) { sb.AppendLine($"Error. Invalid data provided"); } else { if (personDTO.Email != null) { if (!Regex.IsMatch(personDTO.Email, @"^[a-zA-Z0-9]+\@[a-z]+\.[a-z]+$")) { sb.AppendLine($"Error. Invalid data provided"); continue; } } Gender gender = Gender.NotSpecified; if (personDTO.Gender != null) { Enum.TryParse(personDTO.Gender, out gender); } var newPerson = new Person { FirstName = personDTO.FirstName, LastName = personDTO.LastName, MiddleInitial = personDTO.MiddleInitial, Gender = gender, Birthdate = personDTO.Birthday, Phone = personDTO.Phone, Email = personDTO.Email }; newPeople.Add(newPerson); sb.AppendLine($"Successfully imported {newPerson.FirstName} {newPerson.MiddleInitial} {newPerson.LastName}"); } } using (var context = new WeddingsPlannerContext()) { context.People.AddRange(newPeople); context.SaveChanges(); } Console.WriteLine(sb.ToString()); }
private static bool PersonExists(WeddingsPlannerContext context, string fullName) { return(context.People.Any(p => p.FirstName + " " + p.MiddleInitial + " " + p.LastName == fullName)); }
private static Person GetPerson(WeddingsPlannerContext context, string fullname) { return(context.People.FirstOrDefault(p => p.FirstName + " " + p.MiddleInitial + " " + p.LastName == fullname)); }
public static void ImportWeddingsAndInvitations() { //Bride, bridegroom, agency, date => required using (var context = new WeddingsPlannerContext()) { List <WeddingDTO> weddings = new List <WeddingDTO>(); string json = File.ReadAllText(@"..\..\..\WeddingsPlanner.Data\Imports\weddings.json"); weddings = JsonConvert.DeserializeObject <List <WeddingDTO> >(json); StringBuilder sb = new StringBuilder(); List <Wedding> newWeddings = new List <Wedding>(); foreach (var w in weddings) { if (w.Bride == null || w.Bridegroom == null || w.Agency == null || w.Date == null) { sb.AppendLine($"Error. Invalid data provided"); } else { if (!PersonExists(context, w.Bride) || !PersonExists(context, w.Bridegroom)) { sb.AppendLine($"Error. Invalid data provided"); continue; } Wedding newWedding = new Wedding { Agency = GetAgency(context, w.Agency), Bride = GetPerson(context, w.Bride), Bridegroom = GetPerson(context, w.Bridegroom), Date = DateTime.Parse(w.Date.Value.ToString()) }; foreach (var guest in w.Guests) { if (guest.Name != null) { if (PersonExists(context, guest.Name)) { Invitation inv = new Invitation { Guest = GetPerson(context, guest.Name), IsAttending = guest.RSVP, Family = guest.Family, Wedding = newWedding }; newWedding.Invitations.Add(inv); } } } newWeddings.Add(newWedding); string brideName = w.Bride.Split().FirstOrDefault(); string groomName = w.Bridegroom.Split().FirstOrDefault(); sb.AppendLine($"Successfully imported wedding of {brideName} and {groomName}"); } } context.Weddings.AddRange(newWeddings); context.SaveChanges(); Console.WriteLine(sb.ToString()); } }
public static void ImportVenues() { XDocument venuesXml = XDocument.Load(@"..\..\..\WeddingsPlanner.Data\Imports\venues.xml"); List <Venue> venues = new List <Venue>(); StringBuilder sb = new StringBuilder(); foreach (var ven in venuesXml.Root.Elements()) { string venueName = null; int capacity = 0; string town = null; var nameAtr = ven.Attribute("name"); if (nameAtr != null) { venueName = ven.Attribute("name").Value; } var capEle = ven.Element("capacity"); if (capEle != null) { capacity = int.Parse(ven.Element("capacity").Value); } var townEle = ven.Element("town"); if (townEle != null) { town = ven.Element("town").Value; } Venue newVenue = new Venue { Name = venueName, Capacity = capacity, Town = town }; venues.Add(newVenue); sb.AppendLine($"Successfully imported {venueName}"); } using (var context = new WeddingsPlannerContext()) { var weddings = context.Weddings.ToList(); context.Venues.AddRange(venues); context.SaveChanges(); venues = context.Venues.ToList(); foreach (var w in weddings) { Venue venueOne = venues[GenerateRandomNumber(0, venues.Count - 1)]; Venue venueTwo = venues[GenerateRandomNumber(0, venues.Count - 1)]; while (venueOne.Equals(venueTwo)) { venueTwo = venues[GenerateRandomNumber(0, venues.Count - 1)]; } w.Venues.Add(venueOne); w.Venues.Add(venueTwo); } context.SaveChanges(); } Console.WriteLine(sb.ToString()); }