示例#1
0
        public async Task <ActionResult> Move([FromBody] ListEntriesMoveRequest moveRequest)
        {
            var authorizationResult = await _authorizationService.AuthorizeAsync(User, moveRequest, new CatalogAuthorizationRequirement(ModuleConstants.Security.Permissions.Update));

            if (!authorizationResult.Succeeded)
            {
                return(Unauthorized());
            }

            var dstCatalog = (await _catalogService.GetByIdsAsync(new[] { moveRequest.Catalog })).FirstOrDefault();

            if (dstCatalog.IsVirtual)
            {
                return(BadRequest("Unable to move to a virtual catalog"));
            }

            var categories = await _categoryMover.PrepareMoveAsync(moveRequest);

            var products = await _productMover.PrepareMoveAsync(moveRequest);

            await _categoryMover.ConfirmMoveAsync(categories);

            await _productMover.ConfirmMoveAsync(products);

            return(NoContent());
        }
        public override async Task <List <Category> > PrepareMoveAsync(ListEntriesMoveRequest moveInfo)
        {
            await ValidateOperationArguments(moveInfo);

            var result = new List <Category>();

            foreach (var listEntryCategory in moveInfo.ListEntries.Where(
                         listEntry => listEntry.Type.EqualsInvariant(CategoryListEntry.TypeName)))
            {
                var category = (await _categoryService.GetByIdsAsync(new[] { listEntryCategory.Id }, CategoryResponseGroup.Info.ToString())).FirstOrDefault();
                if (category.CatalogId != moveInfo.Catalog)
                {
                    category.CatalogId = moveInfo.Catalog;
                }

                if (category.ParentId != moveInfo.Category)
                {
                    category.ParentId = moveInfo.Category;
                }

                result.Add(category);
            }

            return(result);
        }
        public async Task Validate_PasteNotUnderItself_Valid(string targetCategoryPath, string movedCategoryPath)
        {
            // Arrange
            var targetCateroryId = targetCategoryPath.Split("/").Last();
            var movedCateroryId  = movedCategoryPath.Split("/").Last();

            MockCategoryGetById(targetCategoryPath);
            MockCategoryGetById(movedCategoryPath);

            var moveRequest = new ListEntriesMoveRequest()
            {
                Category    = targetCateroryId,
                ListEntries = new[]
                {
                    new CategoryListEntry()
                    {
                        Type    = CategoryListEntry.TypeName,
                        Id      = movedCateroryId,
                        Outline = movedCategoryPath.Split("/")
                    }
                },
            };

            // Act
            var validationResult = await _validator.ValidateAsync(moveRequest);

            // Assert
            validationResult.IsValid.Should().BeTrue();
        }
        public async Task Validate_PasteUnderItself_NotValid(string targetCategoryPath, string movedCategoryPath)
        {
            // Arrange
            var targetCateroryId = targetCategoryPath.Split("/").Last();
            var movedCateroryId  = movedCategoryPath.Split("/").Last();

            MockCategoryGetById(targetCategoryPath);

            var moveRequest = new ListEntriesMoveRequest()
            {
                Category    = targetCateroryId,
                ListEntries = new[]
                {
                    new CategoryListEntry()
                    {
                        Type    = CategoryListEntry.TypeName,
                        Id      = movedCateroryId,
                        Outline = movedCategoryPath.Split("/")
                    }
                },
            };

            // Act
            var validationResult = await _validator.ValidateAsync(moveRequest);

            // Assert
            validationResult.IsValid.Should().BeFalse();
            validationResult.Errors.Should().Contain(x => x.ErrorMessage == "Cannot move category under itself.");
        }
        public async Task Validate_NoTargetCategory_NotValid()
        {
            // Arrange
            var targetCateroryId = "targetCat";
            var movedCateroryId  = "movedCat";

            var moveRequest = new ListEntriesMoveRequest()
            {
                Category    = targetCateroryId,
                ListEntries = new[]
                {
                    new CategoryListEntry()
                    {
                        Type = CategoryListEntry.TypeName,
                        Id   = movedCateroryId,
                    }
                },
            };

            // Act
            var validationResult = await _validator.ValidateAsync(moveRequest);

            // Assert
            validationResult.IsValid.Should().BeFalse();
            validationResult.Errors.Should().Contain(x => x.ErrorMessage == "Destination category does not exist.");
        }
        protected virtual async Task ValidateOperationArguments(ListEntriesMoveRequest moveInfo)
        {
            if (moveInfo == null)
            {
                throw new ArgumentNullException(nameof(moveInfo));
            }

            var validator = new ListEntriesMoveRequestValidator(_categoryService);
            await validator.ValidateAndThrowAsync(moveInfo);
        }
        private static void ValidateMoveInfo(ListEntriesMoveRequest moveInfo)
        {
            var exception = new Exception("Cannot be moved to a subcategory or into the same category");

            foreach (var entry in moveInfo.ListEntries)
            {
                if (IsEqual(moveInfo.Category, entry.Id))
                {
                    throw exception;
                }
            }
        }
        public override async Task <List <CatalogProduct> > PrepareMoveAsync(ListEntriesMoveRequest moveInfo)
        {
            var result = new List <CatalogProduct>();

            foreach (var listEntryProduct in moveInfo.ListEntries.Where(
                         listEntry => listEntry.Type.EqualsInvariant(ProductListEntry.TypeName)))
            {
                var product = await _itemService.GetByIdAsync(listEntryProduct.Id, ItemResponseGroup.ItemLarge.ToString());

                if (product.CatalogId == moveInfo.Catalog)
                {
                    // idle
                }
                else
                {
                    product.CatalogId  = moveInfo.Catalog;
                    product.CategoryId = null;
                    foreach (var variation in product.Variations)
                    {
                        variation.CatalogId  = moveInfo.Catalog;
                        variation.CategoryId = null;
                    }
                }

                if (product.CategoryId == moveInfo.Category)
                {
                    // idle
                }
                else
                {
                    product.CategoryId = moveInfo.Category;
                    foreach (var variation in product.Variations)
                    {
                        variation.CategoryId = moveInfo.Category;
                    }
                }

                result.Add(product);
                result.AddRange(product.Variations);
            }

            return(result);
        }
        public async Task <BulkActionResult> ExecuteAsync(IEnumerable <IEntity> entities)
        {
            var entries  = entities.Cast <ListEntryBase>().ToArray();
            var moveInfo = new ListEntriesMoveRequest
            {
                Catalog     = _context.CatalogId,
                Category    = _context.CategoryId,
                ListEntries = entries
            };

            ValidateMoveInfo(moveInfo);

            var categories = await _categoryListEntryMover.PrepareMoveAsync(moveInfo);

            var products = await _productListEntryMover.PrepareMoveAsync(moveInfo);

            await _categoryListEntryMover.ConfirmMoveAsync(categories);

            await _productListEntryMover.ConfirmMoveAsync(products);

            return(BulkActionResult.Success);
        }
 public virtual Task <List <T> > PrepareMoveAsync(ListEntriesMoveRequest moveInfo)
 {
     return(Task.FromResult(Enumerable.Empty <T>().ToList()));
 }