public IActionResult SortByMW() { List <WageLocation> wageLocations = context.WageLocations.OrderBy(wl => wl.Wage).ToList(); ViewLocationsViewModel viewLocationsViewModel = new ViewLocationsViewModel { WageLocations = wageLocations }; return(View("Index", viewLocationsViewModel)); }
public IActionResult Index() { decimal FedMinimumWage = 7.25m; List <Location> locations = context.Locations.ToList(); List <WageLocation> wageLocations1 = context.WageLocations.ToList(); List <int> locationIDs = context.Locations.Select(l => l.ID).ToList(); // Checks to see if any locations need to be deleted from WageLocations Joined Table foreach (WageLocation wageLocation in wageLocations1) { if (!locationIDs.Contains(wageLocation.LocationID)) { context.WageLocations.Remove(wageLocation); context.SaveChanges(); } } foreach (Location location in locations) // Checks if existing locations in join table have been edited; saves the changes to the join table database { if (context.WageLocations.Where(wl => location.ID == wl.LocationID).Count() != 0) { if (context.CityWages.Where(sw => sw.State == location.State).Where(sw => sw.City == location.City).Count() != 0) { WageLocation wageLocation = context.WageLocations.FirstOrDefault(wl => wl.LocationID == location.ID); wageLocation.Wage = context.CityWages.Where(cw => cw.City == location.City).Select(cw => cw.MinWage).FirstOrDefault(); wageLocation.LocationID = location.ID; wageLocation.LocationName = location.Name; wageLocation.Address = location.Address; wageLocation.City = location.City; wageLocation.County = location.County; wageLocation.State = location.State; wageLocation.ZIP = location.ZIP; context.SaveChanges(); } else if (context.CountyWages.Where(ctw => ctw.County == location.County).Where(ctw => ctw.State == location.State).Count() != 0) { WageLocation wageLocation = context.WageLocations.FirstOrDefault(wl => wl.LocationID == location.ID); wageLocation.Wage = context.CountyWages.Where(ctw => ctw.County == location.County).Select(ctw => ctw.MinWage).FirstOrDefault(); wageLocation.LocationID = location.ID; wageLocation.LocationName = location.Name; wageLocation.Address = location.Address; wageLocation.City = location.City; wageLocation.County = location.County; wageLocation.State = location.State; wageLocation.ZIP = location.ZIP; context.SaveChanges(); } else if (context.StateWages.Where(sw => sw.State == location.State).Count() != 0) { WageLocation wageLocation = context.WageLocations.FirstOrDefault(wl => wl.LocationID == location.ID); wageLocation.Wage = context.StateWages.Where(sw => sw.State == location.State).Select(sw => sw.MinWage).FirstOrDefault(); wageLocation.LocationID = location.ID; wageLocation.LocationName = location.Name; wageLocation.Address = location.Address; wageLocation.City = location.City; wageLocation.County = location.County; wageLocation.State = location.State; wageLocation.ZIP = location.ZIP; context.SaveChanges(); } else { WageLocation wageLocation = context.WageLocations.FirstOrDefault(wl => wl.LocationID == location.ID); wageLocation.Wage = FedMinimumWage; wageLocation.LocationID = location.ID; wageLocation.LocationName = location.Name; wageLocation.Address = location.Address; wageLocation.City = location.City; wageLocation.County = location.County; wageLocation.State = location.State; wageLocation.ZIP = location.ZIP; context.SaveChanges(); } } // Adds new rows to join table and collects the correct minimum wage else { if (context.CityWages.Where(sw => sw.State == location.State).Where(sw => sw.City == location.City).Count() != 0) { WageLocation wageLocation = new WageLocation { Wage = context.CityWages.Where(cw => cw.City == location.City).Select(cw => cw.MinWage).FirstOrDefault(), LocationID = location.ID, LocationName = location.Name, Address = location.Address, City = location.City, County = location.County, State = location.State, ZIP = location.ZIP }; context.WageLocations.Add(wageLocation); context.SaveChanges(); } else if (context.CountyWages.Where(ctw => ctw.County == location.County).Where(ctw => ctw.State == location.State).Count() != 0) { WageLocation wageLocation = new WageLocation { Wage = context.CountyWages.Where(ctw => ctw.County == location.County).Select(ctw => ctw.MinWage).FirstOrDefault(), LocationID = location.ID, LocationName = location.Name, Address = location.Address, City = location.City, County = location.County, State = location.State, ZIP = location.ZIP }; context.WageLocations.Add(wageLocation); context.SaveChanges(); } else if (context.StateWages.Where(sw => sw.State == location.State).Count() != 0) { WageLocation wageLocation = new WageLocation { Wage = context.StateWages.Where(sw => sw.State == location.State).Select(sw => sw.MinWage).FirstOrDefault(), LocationID = location.ID, LocationName = location.Name, Address = location.Address, City = location.City, County = location.County, State = location.State, ZIP = location.ZIP }; context.WageLocations.Add(wageLocation); context.SaveChanges(); } else { WageLocation wageLocation = new WageLocation { Wage = FedMinimumWage, LocationID = location.ID, LocationName = location.Name, Address = location.Address, City = location.City, County = location.County, State = location.State, ZIP = location.ZIP }; context.WageLocations.Add(wageLocation); context.SaveChanges(); } } } // Gathers the updated join table and populates the view model List <WageLocation> wageLocations = context.WageLocations.ToList(); ViewLocationsViewModel viewLocationsViewModel = new ViewLocationsViewModel { WageLocations = wageLocations }; return(View(viewLocationsViewModel)); }