示例#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
        /// <summary>
        /// <see cref="IDgtAppService"/>
        /// </summary>
        /// <returns><see cref="IDgtAppService"/></returns>
        public VehicleDTO GetVehicleByLicense(string license)
        {
            if (String.IsNullOrEmpty(license))
            {
                throw new ArgumentNullException("license");
            }

            var licenseSpec = VehicleSpecifications.WithLicense(license);
            var result      = _vehicleRepository.AllMatching(licenseSpec);

            if (result != null && result.Any())
            {
                return(result.First().ProjectedAs <VehicleDTO>());
            }
            else
            {
                return(null);
            }
        }