Esempio n. 1
0
        public void Edit(EditCarServiceDto dto, string directory, string currentUserId)
        {
            UserManager.IsUserInCarServiceRole(currentUserId);

            var service = GetCarService(dto.Id);

            if (service.ApplicationUser.Id != currentUserId)
            {
                DeleteDirectoryWithFiles(directory);
                throw new BusinessFaultException(BusinessLogicExceptionResources.CarServiceDoesNotBelongToUser);
            }

            var validationResult = _validationManager.ValidateEditCarServiceDto(dto);

            if (validationResult.HasErrors)
            {
                DeleteDirectoryWithFiles(directory);
                throw new BusinessFaultException(validationResult.GetErrors());
            }

            if (service.State == CarServiceState.Rejected)
            {
                service.State         = CarServiceState.UnderСonsideration;
                service.DeclineReason = null;
            }

            service.Name    = dto.Name;
            service.Address = dto.Address;
            service.Email   = dto.Email;

            if (dto.Logo != null)
            {
                var logo = service.Files.SingleOrDefault(file => !file.IsDeleted && file.Type == FileType.Logo);
                if (logo != null)
                {
                    logo.IsDeleted = true;
                    logo.Updated   = DateTime.UtcNow;
                }
                logo      = Mapper.Map <CarServiceFile>(dto.Logo);
                logo.Type = FileType.Logo;
                service.Files.Add(logo);
            }

            if (dto.Photos != null && dto.Photos.Any())
            {
                var photos = service.Files.Where(file => !file.IsDeleted && file.Type == FileType.Photo).ToList();
                if (photos.Any())
                {
                    foreach (var photo in photos)
                    {
                        photo.IsDeleted = true;
                        photo.Updated   = DateTime.UtcNow;
                    }
                }
                foreach (var photo in dto.Photos)
                {
                    var file = Mapper.Map <CarServiceFile>(photo);
                    service.Files.Add(file);
                }
            }

            foreach (var phone in service.Phones)
            {
                phone.IsDeleted = true;
                phone.Updated   = DateTime.UtcNow;
            }
            service.Phones = Mapper.Map <List <CarServicePhone> >(dto.Phones);

            if (dto.WorkTypes != null && dto.WorkTypes.Any())
            {
                var repository = UnitOfWork.Repository <IWorkTypeRepository>();
                service.WorkTypes.Clear();
                foreach (var workTypeId in dto.WorkTypes)
                {
                    service.WorkTypes.Add(repository.Get(workTypeId));
                }
            }

            service.ManagerName    = dto.ManagerName;
            service.Site           = dto.Site;
            service.TimetableWorks = dto.TimetableWorks;
            service.About          = dto.About;
            service.Updated        = DateTime.UtcNow;

            UnitOfWork.SaveChanges();
        }
        public ValidationResult ValidateEditCarServiceDto(EditCarServiceDto dto)
        {
            var validationResult = new ValidationResult("Редактирование автосервиса");

            if (string.IsNullOrEmpty(dto.Name))
            {
                validationResult.AddError(ValidationErrorResources.CarServiceNameIsEmpty);
            }

            if (string.IsNullOrEmpty(dto.Address))
            {
                validationResult.AddError(ValidationErrorResources.CarServiceAddressIsEmpty);
            }

            if (IsInvalidEmail(dto.Email))
            {
                validationResult.AddError(ValidationErrorResources.CarServiceContactEmailIsInvalid);
            }

            if (dto.Phones == null || !dto.Phones.Any())
            {
                validationResult.AddError(ValidationErrorResources.CarServicePhonesIsEmpty);
            }
            if (dto.Phones != null)
            {
                foreach (var phone in dto.Phones)
                {
                    if (IsInvalidPhoneNumber(phone))
                    {
                        validationResult.AddError(string.Format(ValidationErrorResources.CarServicePhoneIsIncorrect, phone));
                    }
                }
            }

            if (string.IsNullOrEmpty(dto.ManagerName))
            {
                validationResult.AddError(ValidationErrorResources.CarServiceManagerNameIsEmpty);
            }

            var regex = new Regex("^(http|https)://");

            if (string.IsNullOrEmpty(dto.Site) || !regex.IsMatch(dto.Site))
            {
                validationResult.AddError(ValidationErrorResources.CarServiceSiteIsIncorrect);
            }

            if (dto.Logo != null && !IsImage(dto.Logo.Name))
            {
                validationResult.AddError(string.Format(ValidationErrorResources.InvalidFileExtension, dto.Logo.Name));
            }

            if (dto.Photos != null && dto.Photos.Any())
            {
                if (dto.Photos.Count > 5)
                {
                    validationResult.AddError(ValidationErrorResources.PhotosToMuch);
                }
                foreach (var photo in dto.Photos)
                {
                    if (!IsImage(photo.Name))
                    {
                        validationResult.AddError(string.Format(ValidationErrorResources.InvalidFileExtension, photo.Name));
                    }
                }
            }

            return(validationResult);
        }