Пример #1
0
        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();
        }
Пример #2
0
 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)));
 }
Пример #3
0
        public static void SetLogisticCenter(DTO.City logisticCenter)
        {
            CitiesAndRoadsDbContext db = new CitiesAndRoadsDbContext();

            logisticCenter.IsLogisticCenter = true;
            var city = db.Cities.First(c => c.Id == logisticCenter.Id);

            city.IsLogisticCenter = logisticCenter.IsLogisticCenter;
            db.SaveChanges();
        }
Пример #4
0
        public static List <DTO.City> GetCities()
        {
            CitiesAndRoadsDbContext db        = new CitiesAndRoadsDbContext();
            List <City>             dbCities  = db.Cities.OrderBy(c => c.Name).ToList();
            List <DTO.City>         dtoCities = new List <DTO.City>();

            foreach (City item in dbCities)
            {
                var city = new DTO.City();
                city.Id               = item.Id;
                city.Name             = item.Name;
                city.IsLogisticCenter = item.IsLogisticCenter;
                dtoCities.Add(city);
            }

            return(dtoCities);
        }
Пример #5
0
        public static void AddCity(DTO.City newCity)
        {
            CitiesAndRoadsDbContext db = new CitiesAndRoadsDbContext();

            if (String.IsNullOrWhiteSpace(newCity.Name))
            {
                throw new CustomException("City requires Name.");
            }
            if (DoesCityExist(newCity, db))
            {
                throw new CustomException("City already exists.");
            }

            var city = new City();

            city.Name = newCity.Name;
            db.Cities.Add(city);
            db.SaveChanges();
        }
Пример #6
0
        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);
        }
Пример #7
0
 private static bool DoesCityExist(DTO.City newCity, CitiesAndRoadsDbContext db)
 {
     return(db.Cities.Any(c => c.Name == newCity.Name));
 }