Beispiel #1
0
        public async Task Delete(Guid userId, Guid id)
        {
            var productEntryLine = await _productEntryLineRepository.GetById(id, x => x.Include(pel => pel.ProductEntry));

            if (productEntryLine == null)
            {
                throw new KeyNotFoundException($"Product entry line with id: {id} not found.");
            }

            var product = await _productRepository.GetById(productEntryLine.ProductId);

            if (product == null)
            {
                product = (await _productRepository.FindDeleted(x => x.Id == productEntryLine.ProductId)).FirstOrDefault();
            }
            if (product == null)
            {
                throw new KeyNotFoundException($"Product with id: {productEntryLine.ProductId} not found.");
            }

            product.Stock -= productEntryLine.Quantity * (productEntryLine.ProductEntry.IsEntry ? 1 : -1);
            await _productRepository.Update(product);

            await _productEntryLineRepository.Delete(userId, productEntryLine.Id);

            await _productRepository.CommitAsync();

            await _productEntryLineRepository.CommitAsync();
        }
Beispiel #2
0
        public async Task Delete(Guid userId, Guid id)
        {
            var productEntry = _productEntryRepository.AllIncluding(x => x.ProductEntryLines).FirstOrDefault(x => x.Id == id);

            if (productEntry == null)
            {
                throw new KeyNotFoundException($"Product entry with id: {id} not found.");
            }

            productEntry.ProductEntryLines.RemoveAll(x => x.IsDeleted);

            foreach (var pel in productEntry.ProductEntryLines)
            {
                var product = await _productRepository.GetById(pel.ProductId);

                if (product == null)
                {
                    product = (await _productRepository.FindDeleted(x => x.Id == pel.ProductId)).FirstOrDefault();
                }
                if (product == null)
                {
                    throw new KeyNotFoundException($"Product with id: {pel.ProductId} not found.");
                }

                product.Stock -= pel.Quantity * (productEntry.IsEntry ? 1 : -1);
                await _productRepository.Update(product);

                await _productEntryLineRepository.Delete(userId, pel.Id);
            }

            await _productEntryRepository.Delete(userId, id);

            await _productRepository.CommitAsync();

            await _productEntryLineRepository.CommitAsync();

            await _productEntryRepository.CommitAsync();
        }