Example #1
0
        public JsonResult Post([FromBody] Product product)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newProduct = Mapper.Map <Product>(product);

                    _repository.AddProduct(newProduct);

                    if (_repository.SaveAll())
                    {
                        Response.StatusCode = (int)HttpStatusCode.Created;
                        return(Json(Mapper.Map <Product>(newProduct)));
                    }
                }
            }
            catch (Exception ex)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new { Message = ex.Message }));
            }

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json(new { Message = "Failed", ModelState = ModelState }));
        }
Example #2
0
 public IActionResult Create(Product product)
 {
     _logger.Log(_userManager.GetUserId(HttpContext.User), "CreateProduct");
     if (ModelState.IsValid)
     {
         _repository.AddProduct(product);
         _repository.SaveAll();
         return(RedirectToAction("Products"));
     }
     return(View(product));
 }