Beispiel #1
0
        //DONE
        public ActionResult PostCategory(CreateCategoryVM model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var categoryToCreate = _mapper.Map <Category>(model);

                    var result = _categoryServices.AddCategory(categoryToCreate);

                    if (!result)
                    {
                        return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal, contacta el Administrador"));
                    }

                    return(Json("La categoría ha sido agregada."));
                }

                return(BadRequest(FormatedModelStateErrors.GetErrorsFormated(ModelState)));
            }
            catch (Exception ex)
            {
                //TODO: Log the exception
                return(StatusCode(StatusCodes.Status500InternalServerError, "Algo salio mal, contacta el Administrador"));
            }
        }
Beispiel #2
0
        private void AddNewCategory(object sender, EventArgs e)
        {
            if (YesNoDialog() != DialogResult.Yes)
            {
                return;
            }

            var model = new CategoryModel
            {
                Code = txtCode.Text.Trim(),
                Name = txtName.Text.Trim()
            };

            try
            {
                if (_categoryServices.AddCategory(model) != 0)
                {
                    LoadData();
                    SuccessMessage();
                    ClearForm();
                }
                else
                {
                    ErrorMessage();
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message);
                ErrorMessage();
            }
        }
Beispiel #3
0
        public IActionResult AddCategory([FromBody] AddCategoryRequest request)
        {
            if (request == null)
            {
                return(BadRequest(new ErrorViewModel
                {
                    ErrorCode = "400",
                    ErrorMessage = "Please provide input information correctly."
                }));
            }

            if (request.ParentId <= 0)
            {
                return(BadRequest(new ErrorViewModel
                {
                    ErrorCode = "400",
                    ErrorMessage = "Parent category not found"
                }));
            }

            var categoryQuery = _categoryRepository.FindByName(request.CategoryName);

            if (categoryQuery != null)
            {
                return(BadRequest(new ErrorViewModel
                {
                    ErrorCode = "400",
                    ErrorMessage = "Category have been create"
                }));
            }

            var category = new Category()
            {
                Name     = request.CategoryName,
                ParentId = request.ParentId,
                Slug     = request.Slug
            };

            var response = _categoryServices.AddCategory(category);

            if (response != "OK")
            {
                return(BadRequest("Can not execute. Plz contact admin"));
            }
            return(Ok(response));
        }
        public ActionResult Create([Bind("CategoryName")] CategoryViewModel categoryViewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var categoryDto = _mapper.MapToCategory(categoryViewModel);
                    _categoryServices.AddCategory(categoryDto);

                    return(RedirectToAction(nameof(Index)));
                }

                return(View());
            }
            catch
            {
                return(View());
            }
        }
Beispiel #5
0
        public IActionResult AddCategory([FromBody] AddCategoryRequest request)
        {
            try
            {
                if (request == null)
                {
                    return(BadRequest(new ErrorViewModel
                    {
                        ErrorCode = "400",
                        ErrorMessage = "Please provide input information correctly."
                    }));
                }

                if (request.ParentId != null)
                {
                    var parent = _categoryRepository.FindById((int)request.ParentId);
                    if (parent == null)
                    {
                        return(BadRequest(new ErrorViewModel
                        {
                            ErrorCode = "400",
                            ErrorMessage = "Parent category not found"
                        }));
                    }
                }

                var categoryQuery = _categoryRepository.FindByName(request.CategoryName);
                if (categoryQuery != null)
                {
                    return(BadRequest(new ErrorViewModel
                    {
                        ErrorCode = "400",
                        ErrorMessage = "Category have been create"
                    }));
                }

                var category = new Category()
                {
                    Name     = request.CategoryName,
                    ParentId = request.ParentId,
                };

                var response = _categoryServices.AddCategory(category);
                if (response != "OK")
                {
                    return(BadRequest(new ErrorViewModel
                    {
                        ErrorCode = "400",
                        ErrorMessage = "Can not execute. Plz contact admin"
                    }));
                }
                return(Ok(response));
            }
            catch (Exception e)
            {
                return(BadRequest(new ErrorViewModel
                {
                    ErrorCode = "400",
                    ErrorMessage = $"Server Error: {e.Message}"
                }));
            }
        }
 public ActionResult <Category> Post(Category category)
 {
     return(_services.AddCategory(category));
 }
Beispiel #7
0
 public Category Post(Category category)
 {
     return(_categoryServices.AddCategory(category));
 }