コード例 #1
0
ファイル: DgtAppService.cs プロジェクト: okfiera/dgt
        /// <summary>
        /// <see cref="IDgtAppService"/>
        /// </summary>
        /// <returns><see cref="IDgtAppService"/></returns>
        public VehicleDTO AddNewVehicle(VehicleDTO vehicleDTO)
        {
            if (vehicleDTO == null)
            {
                throw new ArgumentNullException("vehicleDTO");
            }

            // Check vehicle license is unique
            var licenseSpec            = VehicleSpecifications.WithLicense(vehicleDTO.License);
            var repeatedLicenseVehicle = _vehicleRepository.AllMatching(licenseSpec);

            if (repeatedLicenseVehicle != null && repeatedLicenseVehicle.Any())
            {
                throw new InvalidOperationException(String.Format(CommonMessages.exception_ItemAlreadyExistsWithProperty, Names.Vehicle, Names.License, vehicleDTO.License));
            }

            // Cast dto to vehicle and save
            var vehicle = MaterializeVehicleFromDto(vehicleDTO);

            vehicle.GenerateNewIdentity();
            vehicle.CreatedDate = DateTime.Now;

            // Add habitual driver
            var driver = _driverRepository.Get(vehicleDTO.DriverId);

            if (driver == null)
            {
                throw new InvalidOperationException(String.Format(CommonMessages.exception_EntityWithIdNotExists,
                                                                  Names.Driver, vehicleDTO.DriverId));
            }

            // Check driver vehicles
            var driverVehicles = _vehicleDriverRepository.GetFiltered(vd => vd.DriverId == driver.Id);

            if (driverVehicles.Count() > 10)
            {
                throw new InvalidOperationException(String.Format(CommonMessages.exception_DriverMaxVehicles,
                                                                  driver.Identifier, 10));
            }

            // Add VehicleDriver item
            var vehicleDriver = new VehicleDriver()
            {
                VehicleId = vehicle.Id, DriverId = driver.Id
            };

            vehicleDriver.GenerateNewIdentity();
            vehicleDriver.CreatedDate = DateTime.Now;

            // Validate items and save
            vehicle.Validate();
            vehicleDriver.Validate();

            _vehicleRepository.Add(vehicle);
            _vehicleRepository.UnitOfWork.Commit();
            _vehicleDriverRepository.Add(vehicleDriver);
            _vehicleDriverRepository.UnitOfWork.Commit();

            return(vehicle.ProjectedAs <VehicleDTO>());
        }
コード例 #2
0
    public void OnPoolSizeChange(Slider slider)
    {
        int newSize = Mathf.FloorToInt(slider.value);
        int diff    = newSize - pool.PoolSize;

        pool.PoolSize = newSize;

        if (diff > 0)
        {
            Debug.Log("Adding " + diff);
            for (int i = 0; i < diff; i++)
            {
                VehicleDriver car = (Instantiate(CarPrototype) as GameObject).GetComponent <VehicleDriver>();
                Cars.Add(car);
            }
        }
        else if (diff < 0)
        {
            Debug.Log("Removing " + -diff);
            for (int i = 0; i < -diff; i++)
            {
                VehicleDriver car = Cars.Last();
                Destroy(car.gameObject);
                Cars.Remove(car);
            }
        }
    }
コード例 #3
0
        public string CreateVehicleDriver(VehicleDriver vehicleDriver)
        {
            // check if exists the driver
            var driver = driverService.GetDriver(vehicleDriver.DriverId);

            if (driver == null)
            {
                return("driver not found");
            }

            // check if the driver is allowed to have one more vehicle
            if (driver.NumVehicles >= 10)
            {
                return("this driver has reached the maximal number of vehicles (10)");
            }

            // link the driver to the vehicle
            vehicleDriverRepository.Add(vehicleDriver);
            SaveVehicle();

            // update the number of vehicles linked to this driver
            driver.NumVehicles++;
            driverService.UpdateDriver(driver);

            return("");
        }
コード例 #4
0
    // Use this for initialization
    void Start()
    {
        Cars          = new List <VehicleDriver>();
        pool          = new GenePool();
        pool.PoolSize = NumIndividuals;
        pool.Initialize(new int[] { 17, 10, 10, 2 });

        for (int i = 0; i < pool.PoolSize; i++)
        {
            VehicleDriver car = (Instantiate(CarPrototype) as GameObject).GetComponent <VehicleDriver>();
            car.network = pool.Individuals[i];
            Cars.Add(car);
        }

        GetComponent <TrackGenerator>().GenerateTrack();
        Cars[0].FollowedCar = true;
    }
コード例 #5
0
ファイル: InitialDataVehicles.cs プロジェクト: okfiera/dgt
        public static void Seed(UnitOfWork.MainBCUnitOfWork context)
        {
            var ferrariGTO = new Vehicle()
            {
                BrandId = EntityGuids.Brands.FerrariBrandId, Model = "GTO", License = "1234-ABC", CreatedDate = DateTime.Now
            };

            ferrariGTO.ChangeCurrentIdentity(EntityGuids.Vehicles.FerrariGTO);
            context.Vehicles.AddOrUpdate(ferrariGTO);

            var iagoAspas_Ferrari = new VehicleDriver()
            {
                DriverId = EntityGuids.Drivers.IagoAspas, VehicleId = EntityGuids.Vehicles.FerrariGTO, CreatedDate = DateTime.Now
            };

            iagoAspas_Ferrari.GenerateNewIdentity();
            context.VehiclesDrivers.Add(iagoAspas_Ferrari);



            var bmwM3 = new Vehicle()
            {
                BrandId = EntityGuids.Brands.BMWBrandId, Model = "M3", License = "4546-BBF", CreatedDate = DateTime.Now
            };

            bmwM3.ChangeCurrentIdentity(EntityGuids.Vehicles.BmwM3);
            context.Vehicles.AddOrUpdate(bmwM3);

            var nemanjaRadoja_BMW = new VehicleDriver()
            {
                DriverId = EntityGuids.Drivers.NemanjaRadoja, VehicleId = EntityGuids.Vehicles.BmwM3, CreatedDate = DateTime.Now
            };

            nemanjaRadoja_BMW.GenerateNewIdentity();
            context.VehiclesDrivers.Add(nemanjaRadoja_BMW);



            var seatLeon = new Vehicle()
            {
                BrandId = EntityGuids.Brands.SeatBrandId, Model = "Leon", License = "66547-AC", CreatedDate = DateTime.Now
            };

            seatLeon.ChangeCurrentIdentity(EntityGuids.Vehicles.SeatLeon);
            context.Vehicles.AddOrUpdate(seatLeon);

            var seatLeon_FranBeltran = new VehicleDriver()
            {
                DriverId = EntityGuids.Drivers.FranBeltran, VehicleId = EntityGuids.Vehicles.SeatLeon, CreatedDate = DateTime.Now
            };

            seatLeon_FranBeltran.GenerateNewIdentity();
            context.VehiclesDrivers.Add(seatLeon_FranBeltran);

            var iagoAspas_SeatLeon = new VehicleDriver()
            {
                DriverId = EntityGuids.Drivers.IagoAspas, VehicleId = EntityGuids.Vehicles.SeatLeon, CreatedDate = DateTime.Now
            };

            iagoAspas_SeatLeon.GenerateNewIdentity();
            context.VehiclesDrivers.Add(iagoAspas_SeatLeon);
        }
コード例 #6
0
        public async Task <string> Handle(CreateVehicleCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // Check vehicle not exists
                var vehicle = await _uow.VehicleRepository.FirstOrDefaultAsync(v => v.Matricula == request.Matricula);

                if (vehicle != null)
                {
                    throw new VehicleExistsException(vehicle.Matricula);
                }

                // Check driver exists
                var driver = await _uow.DriverRepository.FirstOrDefaultAsync(d => d.Dni == request.Dni);

                if (driver == null)
                {
                    throw new DriverNotExistsException(request.Dni);
                }

                // Check driver does not have that car yet
                var vehicleDriver = await _uow.VehicleDriverRepository
                                    .FirstOrDefaultAsync(vd => vd.VehicleMatricula == request.Matricula && vd.DriverDni == driver.Dni);

                if (vehicleDriver != null)
                {
                    throw new DriverHadThatVehicleException(driver.Dni, request.Matricula);
                }

                // Check driver dont have 10 cars yet
                var numCars = await _uow.VehicleDriverRepository.CountAsync(vd => vd.DriverDni == driver.Dni);

                if (numCars == 10)
                {
                    throw new VehiclesLimitException(driver.Dni);
                }

                // Retgister car
                vehicle = new Vehicle
                {
                    Matricula = request.Matricula,
                    Brand     = request.Brand,
                    Model     = request.Model
                };
                await _uow.VehicleRepository.AddAsync(vehicle);

                await _uow.SaveChangesAsync();

                // Register car relation
                vehicleDriver = new VehicleDriver
                {
                    DriverDni        = driver.Dni,
                    VehicleMatricula = vehicle.Matricula,
                };
                await _uow.VehicleDriverRepository.AddAsync(vehicleDriver);

                await _uow.SaveChangesAsync();

                return(vehicle.Matricula);
            }
            catch
            {
                throw;
            }
        }
コード例 #7
0
ファイル: DgtAppService.cs プロジェクト: okfiera/dgt
        /// <summary>
        /// <see cref="IDgtAppService"/>
        /// </summary>
        /// <param name="vehicleLicense"><see cref="IDgtAppService"/></param>
        /// <param name="driverIdentifier"><see cref="IDgtAppService"/></param>
        /// <returns></returns>
        public VehicleDriverDTO AttachDriverToVehicle(string vehicleLicense, string driverIdentifier)
        {
            // Check arguments
            if (String.IsNullOrEmpty(vehicleLicense))
            {
                throw new ArgumentNullException(vehicleLicense);
            }

            if (String.IsNullOrEmpty(driverIdentifier))
            {
                throw new ArgumentNullException(driverIdentifier);
            }

            //Get vehicle
            Vehicle vehicle;
            var     vehicleResult = _vehicleRepository.GetFiltered(v => v.License.ToLower() == vehicleLicense.ToLower());

            if (vehicleResult == null || !vehicleResult.Any())
            {
                throw new InvalidOperationException(String.Format(CommonMessages.exception_EntityWithIdNotExists,
                                                                  Names.Vehicle, vehicleLicense));
            }
            else
            {
                vehicle = vehicleResult.First();
            }

            // Get driver
            Driver driver;
            var    driverResult = _driverRepository.GetFiltered(v => v.Identifier.ToLower() == driverIdentifier.ToLower());

            if (driverResult == null || !driverResult.Any())
            {
                throw new InvalidOperationException(String.Format(CommonMessages.exception_EntityWithIdNotExists,
                                                                  Names.Driver, driverIdentifier));
            }
            else
            {
                driver = driverResult.First();
            }

            // Check driver is not assigned to vehicle
            var repeatedDriver = _vehicleDriverRepository.GetFiltered(v => v.DriverId == driver.Id && v.VehicleId == vehicle.Id);

            if (repeatedDriver != null && repeatedDriver.Any())
            {
                throw new InvalidOperationException(String.Format(CommonMessages.exception_DriverAlreadyIsHabitualDriverOfVehicle,
                                                                  driver.Identifier, vehicle.License));
            }

            // Create assignation
            var vd = new VehicleDriver {
                DriverId = driver.Id, VehicleId = vehicle.Id, CreatedDate = DateTime.Now
            };

            vd.GenerateNewIdentity();

            // Save changes
            _vehicleDriverRepository.Add(vd);
            _vehicleDriverRepository.UnitOfWork.Commit();

            return(vd.ProjectedAs <VehicleDriverDTO>());
        }