Example #1
0
        public Parcel GetById(int id)
        {
            var package = _packageManager.GetById(id);

            if (package == default(Parcel))
            {
                throw new Exception($"Parcel identified as { id } not found.");
            }

            return(package);
        }
Example #2
0
        public Parcel GetById(int id)
        {
            var parcel = _parcelManager.GetById(id);

            if (parcel == default(Parcel))
            {
                throw new ParcelNotFoundInDatabaseException(id);
            }

            return(parcel);
        }
Example #3
0
        public Route CreateRoute(int driverId, int carId, int[] parcelIds)
        {
            Employee driver = _employeeManager.GetEmployeeById(driverId);

            if (driver == default(Employee))
            {
                throw new NewRouteInvalidDataException(new Employee());
            }

            Vehicle car = _vehicleManager.GetVehicleByID(carId);

            if (car == default(Vehicle))
            {
                throw new NewRouteInvalidDataException(new Vehicle());
            }

            // parcel ids in array are already in delviery order
            List <Parcel> parcels = new List <Parcel>();

            foreach (int pid in parcelIds)
            {
                Parcel parcel = _parcelManager.GetById(pid);
                if (parcel == default(Parcel))
                {
                    throw new NewRouteInvalidDataException(new Parcel());
                }
                if (parcel.ParcelStatus != Model.Enums.ParcelStatus.InWarehouse)
                {
                    throw new NewRouteInvalidDataException(parcel.ParcelStatus);
                }
                parcels.Add(parcel);
            }

            CargoDetails cargoDetails = CalculateCargoDetails(car, parcels.ToArray());

            if (cargoDetails.IsOverloaded())
            {
                throw new OverloadedCargoForRouteException();
            }


            Route route = new Route()
            {
                EmployeeId       = driver.Id,
                VehicleId        = car.Id,
                CreationDateTime = DateTime.Now
            };
            List <RoutePoint> routePoints = new List <RoutePoint>();
            int index = 1;

            foreach (Parcel parcel in parcels)
            {
                routePoints.Add(new RoutePoint()
                {
                    ParcelId = parcel.Id,
                    Index    = index++
                });
            }

            int newRouteId = _routeManager.Add(route, routePoints.ToArray());

            return(GetRouteByID(newRouteId));
        }