public async Task <IActionResult> Create([FromBody] ProductDTO productDTO)
        {
            var systemOfMeasurement = await _productsRepository.AssignMeasurementUnitToMeasurementSystem(productDTO.UnitOfMeasurement);

            var product = Product.Create(
                productDTO.Name,
                productDTO.Brand,
                productDTO.Quantity,
                productDTO.UnitOfMeasurement,
                systemOfMeasurement);
            await _productsRepository.Add(product);

            foreach (var price in productDTO.Prices)
            {
                var store = await _storesRepository.GetByName(price.StoreName);

                var productStore = ProductStore.Create(
                    product.Id,
                    store.FirstOrDefault().Id,
                    price.Price);
                await _productStoresRepository.Add(productStore);
            }

            return(Ok(product));
        }
Example #2
0
        public async Task AddProductCustom(string name, string brand, double quantity, string unitOfMeasurement, string storeName, double price)
        {
            Product product;

            if (!await Exists(name, brand, quantity, unitOfMeasurement))
            {
                Guid storeId;
                if (!await _storesRepository.Exists(storeName))
                {
                    Store store = Store.Create(storeName, "", "");
                    await _storesRepository.Add(store);

                    storeId = store.Id;
                }
                else
                {
                    storeId = (await _storesRepository.GetByName(storeName)).FirstOrDefault().Id;
                }

                int measurementSystem = (int)MeasureSystem.Undefined;
                if (unitOfMeasurementConversion.ContainsKey(unitOfMeasurement))
                {
                    measurementSystem = unitOfMeasurementConversion[unitOfMeasurement].Item2;
                }
                else
                {
                    measurementSystem = (int)MeasureSystem.Undefined;
                }

                product = Product.Create(name, brand, quantity, unitOfMeasurement, measurementSystem);
                await Add(product);

                Guid         productId = product.Id;
                ProductStore productStore;
                productStore = ProductStore.Create(productId, storeId, price);
                await _productStoresRepository.Add(productStore);
            }
        }