Example #1
0
        public async Task <IActionResult> ImportProducts([FromBody] InventoryImportModel model)
        {
            var result = await _inventoryService.ImportProducts(model);

            return(ApiResponder.RespondSuccess(result));
        }
        public async Task <List <InventoryReturnModel> > ImportProducts(InventoryImportModel model)
        {
            using (var transaction = await _context.Database.BeginTransactionAsync())
            {
                try
                {
                    var productUniqueIdentifiers =
                        model.InventoryItems.Select(ii => ii.ProductUniqueIdentifier).ToList();
                    if (productUniqueIdentifiers.Distinct().Count() != productUniqueIdentifiers.Count)
                    {
                        throw new InventoryItemWithSameUniqueIdentifierExistsException();
                    }

                    var inventoryItems = new List <ProductInventory>();
                    foreach (var inventoryProductItemModel in model.InventoryItems)
                    {
                        // Check existing SKU
                        var inventoryProductItem = await _context.ProductInventories.FirstOrDefaultAsync(pi =>
                                                                                                         string.Equals(pi.ProductUniqueIdentifier, inventoryProductItemModel.ProductUniqueIdentifier,
                                                                                                                       StringComparison.InvariantCultureIgnoreCase));

                        if (inventoryProductItem != null)
                        {
                            throw new InventoryItemWithSameUniqueIdentifierExistsException();
                        }

                        // Check for existing supplier
                        var supplier =
                            await _context.Suppliers.FirstOrDefaultAsync(s =>
                                                                         s.Id == inventoryProductItemModel.SupplierId);

                        if (supplier == null)
                        {
                            throw new SupplierNotFoundException();
                        }

                        // Check for existing product
                        var product =
                            await _context.Products.FirstOrDefaultAsync(
                                p => p.Id == inventoryProductItemModel.ProductId);

                        if (product == null)
                        {
                            throw new ProductNotFoundException();
                        }

                        // Add to inventory
                        var inventoryItem =
                            _mapper.Map <InventoryProductImportModel, ProductInventory>(inventoryProductItemModel);
                        inventoryItem.ImportedDate = model.ImportedDate;
                        inventoryItems.Add(inventoryItem);
                    }

                    _context.ProductInventories.AddRange(inventoryItems);
                    await _context.SaveChangesAsync();

                    transaction.Commit();

                    var inventoryItemsIds       = inventoryItems.Select(ii => ii.Id).ToList();
                    var populatedInventoryItems =
                        await _context.ProductInventories.Where(pi => inventoryItemsIds.Contains(pi.Id)).ToListAsync();

                    var populatedInventoryItemsModels =
                        _mapper.Map <List <ProductInventory>, List <InventoryReturnModel> >(populatedInventoryItems);

                    foreach (var populatedInventoryItemsModel in populatedInventoryItemsModels)
                    {
                        if (populatedInventoryItemsModel.ProductDetails == null)
                        {
                            continue;
                        }

                        if (string.IsNullOrEmpty(populatedInventoryItemsModel.ProductDetails.Image) ||
                            string.IsNullOrWhiteSpace(populatedInventoryItemsModel.ProductDetails.Image))
                        {
                            populatedInventoryItemsModel.ProductDetails.Image = DefaultImageUrl;
                        }
                    }

                    return(populatedInventoryItemsModels);
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }