public bool UserHasPermissions(User user, ContentPackage package)
        {
            if (package.Owner.ID == user.ID)
                return true;

            return UserHasOrder(user, package);
        }
 public bool UserHasOrder(User user, ContentPackage package)
 {
     var profileService = new ProfileService(UnitOfWork);
     var profile = profileService.GetUserProfile(user);
     var order = UnitOfWork.GetRepository<Order>().Get(o => o.ContentPackage.ID == package.ID
                                                     && o.UserProfile.ID == profile.ID)
         .FirstOrDefault();
     return order != null;
 }
 public void Store(ContentPackage package)
 {
     var propertyStates = package.PropertyStates;
     package.Path = GetDirectoryPath(propertyStates);
     foreach (var file in package.Files)
     {
         var filePath = Path.Combine(package.Path, file.Name);
         Provider.Write(filePath, file.Stream);
     }
 }
        public void UserHasPermissions_UserIsOwner_UserHasPermissions()
        {
            // Arrange
            var unitOfWork = new UnitOfWorkMock();
            var user = new User();
            var package = new ContentPackage {Owner = user};

            // Act
            var financeService = new FinanceService(unitOfWork);
            var userHasPermissions = financeService.UserHasPermissions(user, package);

            // Assert
            userHasPermissions.Should().BeTrue();
        }
        public void UserHasPermissions_UserNotOrderedPackage_UserHasNoPermissions()
        {
            // Arrange
            var unitOfWork = new UnitOfWorkMock();
            var user = new User();
            var userProfile = new UserProfile {User = user};
            var package = new ContentPackage {Owner = new User()};
            unitOfWork.GetRepository<User>().Insert(user);
            unitOfWork.GetRepository<UserProfile>().Insert(userProfile);
            unitOfWork.GetRepository<ContentPackage>().Insert(package);
            unitOfWork.Save();

            // Act
            var financeService = new FinanceService(unitOfWork);
            var userHasPermissions = financeService.UserHasPermissions(user, package);

            // Assert
            userHasPermissions.Should().BeFalse();
        }
        private ContentPackage CreatePackage(int price = 100)
        {
            _ownerUser = new User {UserName = "******"};
            _ownerProfile = new UserProfile {User = _ownerUser};
            _unitOfWork.GetRepository<User>().Insert(_ownerUser);
            _unitOfWork.GetRepository<UserProfile>().Insert(_ownerProfile);

            var package = new ContentPackage
                          {
                              Price = price,
                              Owner = _ownerUser
                          };

            return package;
        }
        private void InitUnitOfWork(UnitOfWorkMock unitOfWork)
        {
            var property1 = new Property {Name = "property1", Order = 1, ID = 1};
            var property2 = new Property {Name = "property2", Order = 2, ID = 2};

            var state1 = new PropertyState {Property = property1, Value = "state1"};
            var state2 = new PropertyState {Property = property1, Value = "state2"};
            var state3 = new PropertyState {Property = property2, Value = "state3"};
            var state4 = new PropertyState {Property = property2, Value = "state4"};

            property1.States = new List<PropertyState> {state1, state2};
            property2.States = new List<PropertyState> {state3, state4};

            var package1 = new ContentPackage
                           {
                               ID = 1,
                               PropertyStates = new Collection<PropertyState> {state1, state3}
                           };

            var package2 = new ContentPackage
                           {
                               ID = 2,
                               PropertyStates = new Collection<PropertyState> {state2, state4}
                           };

            state1.ContentPackages = new Collection<ContentPackage> {package1};
            state2.ContentPackages = new Collection<ContentPackage> {package2};
            state3.ContentPackages = new Collection<ContentPackage> {package1};
            state4.ContentPackages = new Collection<ContentPackage> {package2};

            unitOfWork.GetRepository<Property>().Insert(property1);
            unitOfWork.GetRepository<Property>().Insert(property2);

            unitOfWork.GetRepository<PropertyState>().Insert(state1);
            unitOfWork.GetRepository<PropertyState>().Insert(state2);
            unitOfWork.GetRepository<PropertyState>().Insert(state3);
            unitOfWork.GetRepository<PropertyState>().Insert(state4);

            unitOfWork.GetRepository<ContentPackage>().Insert(package1);
            unitOfWork.GetRepository<ContentPackage>().Insert(package2);

            unitOfWork.Save();
        }
        public void SearchPost_ManyMatchedPackages_AllMatchedPackagesFound()
        {
            // Arrange
            var property = _unitOfWork.GetRepository<Property>().GetByID(1);
            var state1 = property.States.First();
            var state2 = property.States.Last();
            var newPackage = new ContentPackage
                             {ID = 3, PropertyStates = new Collection<PropertyState> {state1, state2}};
            state1.ContentPackages.Add(newPackage);
            state2.ContentPackages.Add(newPackage);
            _unitOfWork.GetRepository<ContentPackage>().Insert(newPackage);

            var viewModel = new SearchViewModel();
            viewModel.States = new Dictionary<int, string> {{1, "state1"}, {2, ""}};

            // Act
            var result = _controller.Search(viewModel);

            // Assert
            var model = (SearchViewModel) result.Model;
            model.Packages.Should().HaveCount(2);
        }
 public void Store(ContentPackage package)
 {
     Package = package;
 }
 private void ImportFilesToPackage(ContentPackage package,
     IEnumerable<HttpPostedFileBase> files,
     bool isPreview)
 {
     foreach (var postedFile in files)
     {
         if (postedFile != null)
         {
             var file = new ContentFile();
             file.Name = postedFile.FileName;
             file.Stream = postedFile.InputStream;
             file.IsPreview = isPreview;
             package.Files.Add(file);
         }
     }
 }
        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", "Вернуться"));
        }
        private ContentPackage CreateContent()
        {
            var file1 = new ContentFile {Name = "file1.txt", Stream = CreateInputStream()};
            var file2 = new ContentFile {Name = "file2.txt", Stream = CreateInputStream()};
            var content = new ContentPackage {Files = new List<ContentFile> {file1, file2}};

            file1.ContentPackage = content;
            file2.ContentPackage = content;

            var property1 = new Property {ID = 1, Order = 1};
            var property2 = new Property {ID = 2, Order = 2};
            var state1 = new PropertyState {ID = 1, Value = "prop1", Property = property1,Index = 1};
            var state2 = new PropertyState {ID = 2, Value = "prop2", Property = property2,Index = 2};

            content.PropertyStates = new Collection<PropertyState> {state1, state2};

            return content;
        }