Ejemplo n.º 1
0
        public async Task <ActionResult <ProductDto> > PutProduct(int id, ProductDto product)
        {
            if (id != product.Id)
            {
                return(BadRequest());
            }


            var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);

            try
            {
                await m_productsService.UpdateAsync(product);


                var productsHelper = new ProductIngredientHelper(m_productIngredientService);
                await productsHelper.DeleteIngredientsAsync(product.Id);

                await productsHelper.AddIngredientsAsync(product.Id, product.Ingredients);

                var targetProduct = await m_productsService.GetAsync(product.Id);


                transaction.Complete();


                return(Ok(targetProduct));
            }
            catch (Exception)
            {
                return(StatusCode(500));
            }
            finally
            {
                transaction.Dispose();
            }
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <ProductDto> > PostProduct(ProductDto product)
        {
            var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);

            try
            {
                var insertedProduct = await m_productsService.AddAsync(product);


                var productsHelper = new ProductIngredientHelper(m_productIngredientService);
                await productsHelper.AddIngredientsAsync(insertedProduct.Id, product.Ingredients);


                var targetProduct = await m_productsService.GetAsync(insertedProduct.Id);



                transaction.Complete();


                return(CreatedAtAction
                       (
                           nameof(GetProduct),
                           new { id = targetProduct.Id },
                           targetProduct
                       ));
            }
            catch (Exception ex)
            {
                return(StatusCode(500));
            }
            finally
            {
                transaction.Dispose();
            }
        }