public void CreatePropertyState_StateNotExists_StateCreated()
        {
            // Arrange
            var unitOfWork = new UnitOfWorkMock();
            var service = new PropertyStateService(unitOfWork);
            var property = CreateProperty();
            unitOfWork.GetRepository<Property>().Insert(property);
            unitOfWork.GetRepository<Property>().Insert(property);
            unitOfWork.Save();

            // Act
            service.CreateState(property, "newValue");

            // Assert
            unitOfWork.GetRepository<PropertyState>().Get().Should().Contain(x => x.Value == "newValue");
        }
        public void CreatePropertyState_StateNotExists_NewStateReturned()
        {
            // Arrange
            var unitOfWork = new UnitOfWorkMock();
            var service = new PropertyStateService(unitOfWork);
            var property = CreateProperty();
            unitOfWork.GetRepository<Property>().Insert(property);
            unitOfWork.GetRepository<Property>().Insert(property);
            unitOfWork.Save();

            // Act
            var newState = service.CreateState(property, "newValue");

            // Assert
            newState.Should().Be(unitOfWork.GetRepository<PropertyState>().Get().Last());
        }
        public void CreatePropertyState_StateOfSamePropertyExists_StateHasUniquePropertyStateIndex()
        {
            // Arrange
            var unitOfWork = new UnitOfWorkMock();
            var service = new PropertyStateService(unitOfWork);
            var property = CreateProperty();
            var propertyState = CreatePropertyState(property);
            propertyState.Index = 2;
            unitOfWork.GetRepository<PropertyState>().Insert(propertyState);
            unitOfWork.Save();

            // Act
            service.CreateState(property, "newValue");

            // Assert
            unitOfWork.GetRepository<PropertyState>().Get().Last().Index.Should().Be(3);
        }
        public ActionResult Upload(UploadViewModel viewModel)
        {
            viewModel.Properties = UnitOfWork.GetRepository<Property>().Get();

            if (!ModelState.IsValid)
            {
                return View(viewModel);
            }

            var package = new ContentPackage();
            package.PropertyStates = new Collection<PropertyState>();
            package.Files = new Collection<ContentFile>();
            package.Description = viewModel.Description;
            package.Caption = viewModel.Caption;
            package.Price = viewModel.Price;
            package.Owner = CurrentUser;

            ImportFilesToPackage(package, viewModel.PreviewContentFiles, true);
            ImportFilesToPackage(package, viewModel.ContentFiles, false);

            var service = new PropertyStateService(UnitOfWork);
            var specifiedStates = viewModel.States.Where(pair => !string.IsNullOrEmpty(pair.Value));
            foreach (var pair in specifiedStates)
            {
                //получаем состояние или создаем новое если не существет
                var propertyState = service.GetState(pair.Key, pair.Value);
                if (propertyState == null)
                {
                    var property = UnitOfWork.GetRepository<Property>().GetByID(pair.Key);
                    propertyState = service.CreateState(property, pair.Value);
                }

                package.PropertyStates.Add(propertyState);
            }

            _fileStorageManager.Store(package);
            UnitOfWork.GetRepository<ContentPackage>().Insert(package);
            UnitOfWork.Save();

            return InformationView("Загрузка завершена",
                                   "Контент успешно загружен. Благодарим за использование нашего ресурса.",
                                   new ActionLinkInfo("Content", "Upload", "Вернуться"));
        }
        public void CreatePropertyState_StateOfSamePropertyNotExists_StateHasUniquePropertyStateIndex()
        {
            // Arrange
            var unitOfWork = new UnitOfWorkMock();
            var service = new PropertyStateService(unitOfWork);
            var propertyState = CreatePropertyState(CreateProperty(id: 1));
            unitOfWork.GetRepository<PropertyState>().Insert(propertyState);
            unitOfWork.Save();

            // Act
            service.CreateState(CreateProperty(id: 2), "newValue");

            // Assert
            unitOfWork.GetRepository<PropertyState>().Get().Should().Contain(x => x.Index == 1);
        }