public LogisticCenter LogisticCenter() { var logisticCetner = _context.LogisticCenter.FirstOrDefault(); var newCenter = new LogisticCenter(); if (logisticCetner != null) { // check timestamps on last road update and last logistic center generation so we know if we should do all the logic if (_context.Roads.OrderByDescending(x => x.UpdateDate).First().UpdateDate < logisticCetner.UpdatedDate) { newCenter = new LogisticCenter { Id = logisticCetner.Id, Name = logisticCetner.Name }; return(newCenter); } else { newCenter = GenerateLogisticCenter(); if (newCenter == null) { // could make custom exception throw new ArgumentException("Something went wrong when generating a logistic center."); } else { logisticCetner.Name = newCenter.Name; _context.LogisticCenter.Update(logisticCetner); _context.SaveChangesAsync(); } return(newCenter); } } else { newCenter = GenerateLogisticCenter(); if (newCenter == null) { // could make custom exception throw new ArgumentException("Something went wrong when generating a logistic center."); } var logisticCenterEntity = new LogisticCenterEntity() { Name = newCenter.Name, UpdatedDate = DateTime.Now }; _context.LogisticCenter.Add(logisticCenterEntity); _context.SaveChanges(); return(newCenter); } }
private LogisticCenter GenerateLogisticCenter() { var logisticCenterName = new LogisticCenter(); if (_context.Roads.Any()) { var cities = _context.Cities.Include(x => x.RoadFrom).Include(x => x.To); // collection to store how many times a city has been marked as "furthest" from another city in the map Dictionary <CityEntity, int> furthestCityIds = new Dictionary <CityEntity, int>(); //find furthest city for each city we have in db that has roads foreach (var city in cities) { if (city.RoadFrom.Count == 0) { continue; } int totalDistance = 0; List <CityEntity> visited = new List <CityEntity>(); visited.Add(city); foreach (var road in city.RoadFrom) { if (visited.Contains(road.To) == false) { // using depth first search and keeping track of the distance to the bottom(a.k.a furthest) most city in the search. DFS(road.To, road.Distance, furthestCityIds, totalDistance, visited); } } } // Find the most common furthest city KeyValuePair <CityEntity, int> max = new KeyValuePair <CityEntity, int>(new CityEntity(), 0); foreach (var city in furthestCityIds) { if (city.Value > max.Value) { max = city; } } // we found our furthest city in max. Now lets find the logistic center. // this will be the city that has the shortest road to max. logisticCenterName.Name = cities.First(x => x.Name == max.Key.Name).To.OrderBy(r => r.Distance).First().From.Name; } return(logisticCenterName); }
public LogisticCenter GetLogisticCenter() { var logCenterName = new LogisticCenter() { Id = 1, Name = "Not Generated" }; var center = _context.LogisticCenter.FirstOrDefault(); if (center != null) { logCenterName.Name = center.Name; logCenterName.Id = center.Id; } return(logCenterName); }
public async Task SetLocation(string cityName) { City city = await this.cities.GetByNameAsync(cityName); if (city != null) { LogisticCenter lc = await this.Db.LogisticCenters.FirstOrDefaultAsync(); if (lc == null) { lc = new LogisticCenter { City = city }; await this.Db.LogisticCenters.AddAsync(lc); } else { lc.City = city; } await this.Db.SaveChangesAsync(); } }
public async Task <string> GetCurrent() { LogisticCenter result = await this.Db.LogisticCenters.Include(lc => lc.City).FirstOrDefaultAsync(); return(result?.City?.Name); }