Exemple #1
0
        public async Task <IActionResult> PutProduct([FromRoute] int id, [FromBody] APIModel.Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (id != product.Id)
            {
                return(BadRequest());
            }
            try
            {
                var p = _mapper.Map <Product>(product);
                p.CreatedDate           = DateTime.Now;
                _context.Entry(p).State = EntityState.Modified;
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemple #2
0
        public async Task <IActionResult> PostProduct([FromBody] APIModel.Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                var p = _mapper.Map <Product>(product);
                p.CreatedDate = DateTime.Now;
                p.SettingId   = 1;
                p.CategoryId  = 20;
                _context.Product.Add(p);
                await _context.SaveChangesAsync();

                _logger.LogInformation("creating a new product.");
                return(CreatedAtAction("GetProduct", new { id = p.Id }, p));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Threw exception while save product: {ex}");
            }
            return(BadRequest());
        }