public async Task <IActionResult> Put(int id, [FromBody] CreateAndUpdateProductDto product)
        {
            try
            {
                await _service.UpdateProduct(id, product);

                return(Ok());
            }
            catch (EntityNotFoundException)
            {
                return(NotFound(id));
            }
        }
Beispiel #2
0
        public async Task <int> CreateProduct(CreateAndUpdateProductDto productDto)
        {
            if (productDto == null)
            {
                throw new ArgumentNullException(nameof(productDto));
            }

            var product = new Product
            {
                Cost = productDto.Cost,
                Name = productDto.Name
            };

            var result = await _context.Products.AddAsync(product);

            await _context.SaveChangesAsync();

            return(result.Entity.Id);
        }
Beispiel #3
0
        public async Task UpdateProduct(int id, CreateAndUpdateProductDto productDto)
        {
            if (productDto == null)
            {
                throw new ArgumentNullException(nameof(productDto));
            }

            var product = await GetProductById(id);

            if (product.Cost != productDto.Cost)
            {
                product.Cost = productDto.Cost;
            }

            if (!string.IsNullOrWhiteSpace(productDto.Name) && product.Name != productDto.Name)
            {
                product.Name = productDto.Name;
            }

            _context.Products.Update(product);
            await _context.SaveChangesAsync();
        }
        public async Task <IActionResult> Post([FromBody] CreateAndUpdateProductDto product)
        {
            var id = await _service.CreateProduct(product);

            return(Ok(id));
        }