public static void AddRoad(DTO.Road newRoad) { CitiesAndRoadsDbContext db = new CitiesAndRoadsDbContext(); if (newRoad.Length == 0) { throw new CustomException("Road requires actual length."); } if (newRoad.SideCityOneId == newRoad.SideCityTwoId) { throw new CustomException("Cities must be different."); } if (DoesRoadExist(newRoad, db)) { throw new CustomException("Road already exist."); } var road = new Road(); road.Length = newRoad.Length; road.SideCityOneId = newRoad.SideCityOneId; road.SideCityTwoId = newRoad.SideCityTwoId; db.Roads.Add(road); db.SaveChanges(); }
private static bool DoesRoadExist(DTO.Road newRoad, CitiesAndRoadsDbContext db) { return(db.Roads.Any(r => (r.SideCityOneId == newRoad.SideCityOneId && r.SideCityTwoId == newRoad.SideCityTwoId) || (r.SideCityOneId == newRoad.SideCityTwoId && r.SideCityTwoId == newRoad.SideCityOneId))); }
public static void AddRoad(DTO.Road road) { try { RoadsRepository.AddRoad(road); } catch (Exception ex) { // Log it throw; } }
public static string GetLogisticCentername() { IEnumerable <DTO.City> cities = GetCities(); DTO.City logisticCenter = cities.FirstOrDefault(c => c.IsLogisticCenter == true); IEnumerable <DTO.Road> roads = RoadsManager.GetRoads(); IEnumerable <DTO.Road> connectedRoadsTmp = null; DTO.Road furthestRoad = new DTO.Road(); int newLogisticCenterId = 0; foreach (DTO.City city in cities) { connectedRoadsTmp = roads.Where(r => r.SideCityOneId == city.Id || r.SideCityTwoId == city.Id); if (connectedRoadsTmp != null && connectedRoadsTmp.Count() > 0) { DTO.Road closestRoadTmp = connectedRoadsTmp.OrderBy(r => r.Length).First(); if (closestRoadTmp.Length > furthestRoad.Length) { furthestRoad = closestRoadTmp; // Get logistic center city id which is opposite side of the furthest city newLogisticCenterId = city.Id == furthestRoad.SideCityOneId ? furthestRoad.SideCityTwoId : furthestRoad.SideCityOneId; } } } if (logisticCenter != null && logisticCenter.Id == newLogisticCenterId) { throw new CustomException("Logistic center hasn't changed."); } else if (newLogisticCenterId == 0) { throw new CustomException("Logistic center doesn't exist."); } else { logisticCenter = cities.First(c => c.Id == newLogisticCenterId); CitiesRepository.SetLogisticCenter(logisticCenter); } return(logisticCenter.Name); }
public static List <DTO.Road> GetRoads() { CitiesAndRoadsDbContext db = new CitiesAndRoadsDbContext(); List <Road> dbRoads = db.Roads.Include("SideCityOne").Include("SideCityTwo").OrderBy(r => r.Length).ToList(); List <DTO.Road> dtoRoads = new List <DTO.Road>(); foreach (Road item in dbRoads) { var road = new DTO.Road(); road.Id = item.Id; road.Length = item.Length; road.SideCityOneId = item.SideCityOneId; road.SideCityOneName = item.SideCityOne.Name; road.SideCityTwoId = item.SideCityTwoId; road.SideCityTwoName = item.SideCityTwo.Name; dtoRoads.Add(road); } return(dtoRoads); }