コード例 #1
0
        public async Task <IActionResult> PutProductsModel(int id, ProductsModel ProductsModel)
        {
            ProductsModel.IdProduct = id;

            _context.Entry(ProductsModel).State = EntityState.Modified;

            if (id != ProductsModel.IdProduct)
            {
                return(BadRequest());
            }

            _context.Entry(ProductsModel).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductsModelExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #2
0
        public async Task <IActionResult> PutOrderModel(int id, OrderModel OrderModel)
        {
            if (id != OrderModel.IdOrder)
            {
                return(BadRequest());
            }

            _context.Entry(OrderModel).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!OrderModelExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #3
0
        public async Task <IActionResult> PutProduct([FromRoute] Guid id, [FromBody] Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (id != product.Id)
            {
                return(BadRequest("Product id is required"));
            }

            _context.Entry(product).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok("Product updated " + product.Id));
        }
コード例 #4
0
        public async Task <ActionResult <Product> > Create([FromBody] Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            await _productsDbContext.Products.AddAsync(product);

            await _productsDbContext.SaveChangesAsync();

            return(Ok(product));
        }
コード例 #5
0
        public async Task <ProductDto> AddProductAsync(ProductDto product)
        {
            try
            {
                product.ProductId = new Guid();
                var addedProduct = await _context.Products.AddAsync(_mapper.Map <ProductEntity>(product));

                await _context.SaveChangesAsync();

                return(_mapper.Map <ProductDto>(addedProduct));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }