예제 #1
0
        public async Task <IActionResult> Post([FromBody] CategoryViewModel model)
        {
            try
            {
                var newCategory = _mapper.Map <CategoryViewModel, Category>(model);

                _repository.Add(newCategory);
                if (await _repository.SaveAllAsync())
                {
                    return(Created($"/api/categories/{newCategory.CategoryId}", _mapper.Map <Category, CategoryViewModel>(newCategory)));
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest("Error: " + ex.Message + " - ModelState: " + ModelState));
            }
        }
예제 #2
0
        public async Task <IActionResult> Post([FromBody] ProductViewModel model)
        {
            try
            {
                var newProduct = _mapper.Map <ProductViewModel, Product>(model);

                _repository.Add(newProduct);
                if (await _repository.SaveAllAsync())
                {
                    return(Created($"/api/products/{newProduct.ProductId}", _mapper.Map <Product, ProductViewModel>(newProduct)));
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest("Error: " + ex.Message + " - ModelState: " + ModelState));
            }
        }