Пример #1
0
        public virtual void Execute(int userId, ICreateVehicleCommand vehicleForm, HttpPostedFileBase photoFile)
        {
            if (vehicleForm == null)
            {
                throw new ArgumentNullException("vehicleForm");
            }

            try
            {
                var vehicle = vehicleForm.ConvertToEntity(userId);
                _vehicleRepository.Create(userId, vehicle);

                if (photoFile == null)
                {
                    return;
                }

                // the double reference between vehicle and photo is a potential source of pain
                var photo = photoFile.ConvertToEntity();
                _photoRepository.Create(vehicle.VehicleId, photo);
                vehicle.PhotoId = photo.VehiclePhotoId;

                _vehicleRepository.Update(vehicle);
            }
            catch (InvalidOperationException ex)
            {
                throw new BusinessServicesException(Resources.UnableToCreateVehicleExceptionMessage, ex);
            }
        }
Пример #2
0
 public VehicleService(ILogger <VehicleService> logger,
                       ICreateVehicleCommand createVehicleCommand,
                       ICreateVehicleOtherPropertyCommand createVehicleOtherPropertyCommand)
 {
     _logger = logger ?? throw new ArgumentNullException(nameof(logger));
     _createVehicleCommand = createVehicleCommand;
     _createVehicleOtherPropertyCommand = createVehicleOtherPropertyCommand;
 }
 public CreateMaintenanceBookingCommand(IDatabaseService database,
                                        IMaintenanceBookingFactory factory,
                                        ICreateCustomerCommand customerCommand,
                                        ICreateVehicleCommand vehicleCommand)
 {
     _database        = database;
     _factory         = factory;
     _customerCommand = customerCommand;
     _vehicleCommand  = vehicleCommand;
 }
Пример #4
0
        public virtual IEnumerable <ValidationResult> Execute(int userId, ICreateVehicleCommand vehicle)
        {
            var vehicles = _vehicleRepository.GetVehicles(userId);

            if (vehicles.Count() >= MaxNumberOfVehiclesPerUser)
            {
                yield return(new ValidationResult(Resources.TooManyVehicles));
            }

            foreach (var result in _validateVehicleYearMakeAndModel.Execute(vehicle))
            {
                yield return(result);
            }
        }
Пример #5
0
        public virtual IEnumerable<ValidationResult> Execute(int userId, ICreateVehicleCommand vehicle)
        {
            var vehicles = _vehicleRepository.GetVehicles(userId);

            if (vehicles.Count() >= MaxNumberOfVehiclesPerUser)
            {
                yield return new ValidationResult(Resources.TooManyVehicles);
            }

            foreach (var result in _validateVehicleYearMakeAndModel.Execute(vehicle))
            {
                yield return result;
            }
        }
        public virtual int Execute(int userId, ICreateVehicleCommand vehicleForm, HttpPostedFileBase photoFile)
        {
            if (vehicleForm == null) throw new ArgumentNullException("vehicleForm");

            var vehicle = vehicleForm.ConvertToEntity(userId);
            _vehicleRepository.Create(userId, vehicle);

            if (photoFile != null)
            {
                // the double reference between vehicle and photo is a potential source of pain
                var photo = photoFile.ConvertToEntity();
                _photoRepository.Create(vehicle.VehicleId, photo);
                vehicle.PhotoId = photo.VehiclePhotoId;

                _vehicleRepository.Update(vehicle);
            }

            return vehicle.Id;
        }
        public virtual void Execute(int userId, ICreateVehicleCommand vehicleForm, HttpPostedFileBase photoFile)
        {
            if (vehicleForm == null) throw new ArgumentNullException("vehicleForm");

            try
            {
                var vehicle = vehicleForm.ConvertToEntity(userId);
                _vehicleRepository.Create(userId, vehicle);

                if (photoFile == null) return;

                // the double reference between vehicle and photo is a potential source of pain
                var photo = photoFile.ConvertToEntity();
                _photoRepository.Create(vehicle.VehicleId, photo);
                vehicle.PhotoId = photo.VehiclePhotoId;

                _vehicleRepository.Update(vehicle);
            }
            catch (InvalidOperationException ex)
            {
                throw new BusinessServicesException(Resources.UnableToCreateVehicleExceptionMessage, ex);
            }
        }
Пример #8
0
        public virtual int Execute(int userId, ICreateVehicleCommand vehicleForm, HttpPostedFileBase photoFile)
        {
            if (vehicleForm == null)
            {
                throw new ArgumentNullException("vehicleForm");
            }

            var vehicle = vehicleForm.ConvertToEntity(userId);

            _vehicleRepository.Create(userId, vehicle);

            if (photoFile != null)
            {
                // the double reference between vehicle and photo is a potential source of pain
                var photo = photoFile.ConvertToEntity();
                _photoRepository.Create(vehicle.VehicleId, photo);
                vehicle.PhotoId = photo.VehiclePhotoId;

                _vehicleRepository.Update(vehicle);
            }

            return(vehicle.Id);
        }
Пример #9
0
        public virtual void Execute(int userId, ICreateVehicleCommand vehicleForm, HttpPostedFileBase photoFile)
        {
            try
            {
                var existing = _vehicleRepository.GetVehicle(userId, vehicleForm.VehicleId);
                int? photoId = null;

                if (existing != null)
                {
                    if (photoFile != null)
                    {
                        if (existing.PhotoId > 0)
                        {
                            _photoRepository.Delete(existing.PhotoId);
                        }

                        var dataPhoto = photoFile.ConvertToEntity();

                        // should we put this in its own try block?
                        _photoRepository.Create(vehicleForm.VehicleId, dataPhoto);
                        photoId = dataPhoto.VehiclePhotoId = dataPhoto.VehiclePhotoId;
                    }

                    var vehicle = vehicleForm.ConvertToEntity(userId, includeVehicleId: true);
                    vehicle.PhotoId = (photoId != null) ? photoId.Value : existing.PhotoId;
                    _vehicleRepository.Update(vehicle);
                }
                else
                {
                    throw new BusinessServicesException(Resources.UnableToUpdateVehicleExceptionMessage);
                }
            }
            catch (InvalidOperationException ex)
            {
                throw new BusinessServicesException(Resources.CannotFindVehicleToUpdateExceptionMessage, ex);
            }
        }
        public virtual void Execute(int userId, ICreateVehicleCommand vehicleForm, HttpPostedFileBase photoFile)
        {
            try
            {
                var existing = _vehicleRepository.GetVehicle(userId, vehicleForm.VehicleId);
                int? photoId = null;

                if (existing != null)
                {
                    if (photoFile != null)
                    {
                        if (existing.PhotoId > 0)
                        {
                            _photoRepository.Delete(existing.PhotoId);
                        }

                        var dataPhoto = photoFile.ConvertToEntity();

                        // should we put this in its own try block?
                        _photoRepository.Create(vehicleForm.VehicleId, dataPhoto);
                        photoId = dataPhoto.VehiclePhotoId = dataPhoto.VehiclePhotoId;
                    }

                    var vehicle = vehicleForm.ConvertToEntity(userId, includeVehicleId: true);
                    vehicle.PhotoId = (photoId != null) ? photoId.Value : existing.PhotoId;
                    _vehicleRepository.Update(vehicle);
                }
                else
                {
                    throw new BusinessServicesException(Resources.UnableToUpdateVehicleExceptionMessage);
                }
            }
            catch (InvalidOperationException ex)
            {
                throw new BusinessServicesException(Resources.CannotFindVehicleToUpdateExceptionMessage, ex);
            }
        }
Пример #11
0
        public static Vehicle ConvertToEntity(this ICreateVehicleCommand vehicleForm, int userId, bool includeVehicleId = false)
        {
            if (vehicleForm == null)
            {
                return(null);
            }

            var vehicle = new Vehicle
            {
                MakeName  = vehicleForm.MakeName,
                ModelName = vehicleForm.ModelName,
                Name      = vehicleForm.Name,
                SortOrder = vehicleForm.SortOrder,
                Year      = vehicleForm.Year,
                UserId    = userId
            };

            if (includeVehicleId)
            {
                vehicle.VehicleId = vehicleForm.VehicleId;
            }

            return(vehicle);
        }
        public virtual IEnumerable<ValidationResult> Execute(ICreateVehicleCommand vehicle)
        {
            bool isYearSet = vehicle.Year.HasValue;
            bool isMakeSet = !string.IsNullOrEmpty(vehicle.MakeName);
            bool isModelSet = !string.IsNullOrEmpty(vehicle.ModelName);

            bool isYearValid = true;
            bool isMakeValid = true;
            bool isModelValid = true;

            // Make requires a year
            if ((!isYearSet) && isMakeSet)
            {
                yield return new ValidationResult("MakeName", Resources.VehicleMissingYearForMake);
                isMakeValid = false;
            }

            // Model requires a year and make
            if (isModelSet)
            {
                if (!isYearSet)
                {
                    yield return new ValidationResult("ModelName", Resources.VehicleMissingYearForModel);
                    isModelValid = false;
                }
                else if (!isMakeSet)
                {
                    yield return new ValidationResult("ModelName", Resources.VehicleMissingMakeForModel);
                    isModelValid = false;
                }
            }

            // Validate Year value (if not already invalid)
            if (isYearValid)
            {
                isYearValid = ((!isYearSet) || _manufacturerRepository.IsValidYear(vehicle.Year.Value));
                if (!isYearValid)
                {
                    yield return new ValidationResult("Year", Resources.VehicleYearInvalid);
                }
            }

            // Validate Make value (if not already invalid)
            if (isMakeValid)
            {
                isMakeValid = ((!isMakeSet) ||
                               (isYearSet &&
                                _manufacturerRepository.IsValidMake(vehicle.Year.Value, vehicle.MakeName)));
                if (!isMakeValid)
                {
                    yield return new ValidationResult("MakeName", Resources.VehicleMakeInvalid);
                }
            }

            // Validate Model value (if not already invalid)
            if (isModelValid)
            {
                isModelValid = ((!isModelSet) ||
                                (isYearSet && isMakeSet &&
                                 _manufacturerRepository.IsValidModel(vehicle.Year.Value, vehicle.MakeName,
                                                                      vehicle.ModelName)));
                if (!isModelValid)
                {
                    yield return new ValidationResult("ModelName", Resources.VehicleModelInvalid);
                }
            }
        }
        public virtual IEnumerable <ValidationResult> Execute(ICreateVehicleCommand vehicle)
        {
            bool isYearSet  = vehicle.Year.HasValue;
            bool isMakeSet  = !string.IsNullOrEmpty(vehicle.MakeName);
            bool isModelSet = !string.IsNullOrEmpty(vehicle.ModelName);

            bool isYearValid  = true;
            bool isMakeValid  = true;
            bool isModelValid = true;

            // Make requires a year
            if ((!isYearSet) && isMakeSet)
            {
                yield return(new ValidationResult("MakeName", Resources.VehicleMissingYearForMake));

                isMakeValid = false;
            }

            // Model requires a year and make
            if (isModelSet)
            {
                if (!isYearSet)
                {
                    yield return(new ValidationResult("ModelName", Resources.VehicleMissingYearForModel));

                    isModelValid = false;
                }
                else if (!isMakeSet)
                {
                    yield return(new ValidationResult("ModelName", Resources.VehicleMissingMakeForModel));

                    isModelValid = false;
                }
            }

            // Validate Year value (if not already invalid)
            if (isYearValid)
            {
                isYearValid = ((!isYearSet) || _manufacturerRepository.IsValidYear(vehicle.Year.Value));
                if (!isYearValid)
                {
                    yield return(new ValidationResult("Year", Resources.VehicleYearInvalid));
                }
            }

            // Validate Make value (if not already invalid)
            if (isMakeValid)
            {
                isMakeValid = ((!isMakeSet) ||
                               (isYearSet &&
                                _manufacturerRepository.IsValidMake(vehicle.Year.Value, vehicle.MakeName)));
                if (!isMakeValid)
                {
                    yield return(new ValidationResult("MakeName", Resources.VehicleMakeInvalid));
                }
            }

            // Validate Model value (if not already invalid)
            if (isModelValid)
            {
                isModelValid = ((!isModelSet) ||
                                (isYearSet && isMakeSet &&
                                 _manufacturerRepository.IsValidModel(vehicle.Year.Value, vehicle.MakeName,
                                                                      vehicle.ModelName)));
                if (!isModelValid)
                {
                    yield return(new ValidationResult("ModelName", Resources.VehicleModelInvalid));
                }
            }
        }