Пример #1
0
        public async Task <IEnumerable <ProductDto> > AddProductsAsync(
            IEnumerable <ProductForCreationDto> newProducts, CancellationToken cancellationToken = default(CancellationToken))
        {
            var productEntities = newProducts.Select(ProductMapper.CreationToEntityFunc).ToList();

            _context.Products.AddRange(productEntities);
            var affectedRow = await _context.SaveChangesAsync(cancellationToken);

            var expectedAffectedRow = productEntities.Count + productEntities.Sum(p => p.ProductImages.Count);

            HelperCore.CheckSaveChange(affectedRow, expectedAffectedRow);

            var returnProducts = productEntities.Select(ProductMapper.EntityToDtoFunc);

            return(returnProducts);
        }
Пример #2
0
        public async Task DeleteProductAsync(int id, CancellationToken cancellationToken = default(CancellationToken))
        {
            var productEntity = await _context.Products
                                .FirstOrDefaultAsync(p => p.ProductId == id, cancellationToken);

            if (productEntity == null)
            {
                throw  new EntityNotFoundException($"Cannot find product with id={id} in order to delete");
            }

            _context.Products.Remove(productEntity);

            var affectedRow = await _context.SaveChangesAsync(cancellationToken);

            HelperCore.CheckSaveChange(affectedRow, 1);
        }