public AllLinkViewModel() { Bag = new Bagsmvc(); Person = new Peoplemvc(); Link = new Linksmvccore(); People = new List <Peoplemvc>(); Bags = new List <Bagsmvc>(); Links = new List <Linksmvccore>(); }
public async Task <IActionResult> Details(int?id) { if (id == null) { return(NotFound()); } // Fetch all people and bags (like always) var people = await _cache.GetFromTable(_context.People) as List <Peoplemvc>; var allbags = await _cache.GetFromTable(_context.Bags); // A Patron instance contains a person and their bags // Instantiate patron with the person the user clicked as well as a blank list of bags Patron patron = new Patron { Person = people.FirstOrDefault(x => x.PersonNumber == id), Bags = new List <Bagsmvc>() }; // Slightly faster way to find all links for the person using Navigation Properties var links = patron.Person.Links; // Find all links for this person - THIS WORKS!! //var alllinks = await _cache.GetFromTable(_context.Links); //var links = alllinks.Where(x => x.PersonId == id).ToList(); // When someone clicks "See this person's bags", but they haven't sent me any (such as Starter Kit people), put in dummy record if (links == null) { // Instantiate and initialize new dummy bag Bagsmvc bag = new Bagsmvc { Airline = "No Bags Found For " + patron.Person.FirstName + ' ' + patron.Person.MiddleName + ' ' + patron.Person.LastName }; patron.Bags.Add(bag); } else { foreach (var l in links) { var x = allbags.Find(x => x.Id == l.BagId); if (x != null) { patron.Bags.Add(x); } else { throw new FieldAccessException("Link to non-existent bag, Link #" + l.LinkNumber); } } } return(View(patron)); }
public async Task <IActionResult> Edit(int?id, bool?copy) { Bagsmvc bag = new Bagsmvc(); BagViewModel bvm = new BagViewModel(); bvm.People = await _cache.GetFromTable(_context.People); bvm.People = bvm.People.OrderBy(x => x.FirstName).ToList(); ViewBag.Operation = "Edit This"; // First set up the bagtype dropdown // When id is not null, we are editing or copying. Otherwise, we're creating // EDIT a bag if (id != null) { var allbags = await _cache.GetFromTable(_context.Bags); bag = allbags.FirstOrDefault(x => x.Id == id); } else // CREATE a new blank bag { ViewBag.Operation = "Newly Created"; await _context.Bags.AddAsync(bag); await _context.SaveChangesAsync();; await _cache.GetFromTable(true, _context.Bags); } // COPY a bag // This is a real hack. Once the button is hit, the bag is copied and saved to the db. Setting Id = 0 somehow // indicates to the Entity Framework to add the record in with a newly generated ID. // Also, since we don't want an exact copy, bring user to edit screen to make the changes. if (copy != null) { ViewBag.Operation = "Recently Copied"; bag.Id = 0; bag.Links = null; bag.Person = null; // This is required because the instance of Person is being 'tracked' elsewhere await _context.Bags.AddAsync(bag); await _context.SaveChangesAsync(); // await _cache.AddItem(bag, _context.Bags); await _cache.GetFromTable(true, _context.Bags); } bvm.Bag = bag; bvm.TypeOfBag = await _cache.GetFromTable(_context.Types); return(View(bvm)); }