示例#1
0
        /// <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 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);
        }
示例#3
0
        /// <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>());
        }