public IHttpActionResult AddNewCategory(NewCategoryDTO category)
        {
            try
            {
                categoryService.CreateCategory(category);
            }
            catch (DatabaseException)
            {
                return(StatusCode(HttpStatusCode.InternalServerError));
            }
            catch (AuctionException ex)
            {
                return(BadRequest(ex.Message));
            }

            return(StatusCode(HttpStatusCode.Created));
        }
        public async Task <ActionResult> AddCategory([FromBody] NewCategoryDTO newCategory)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var category = await _context.Categories
                           .Include(c => c.Subcategories)
                           .SingleOrDefaultAsync(c =>
                                                 c.Name == newCategory.CategoryName &&
                                                 c.ParentCategory == null);

            if (!string.IsNullOrEmpty(newCategory.SubcategoryName)) // Adding sucategory to existing category
            {
                if (category == null)
                {
                    return(NotFound(new SerializableError()
                    {
                        { nameof(newCategory.CategoryName), "Category not found" }
                    }));
                }

                if (category.Subcategories.Any(c => c.Name == newCategory.SubcategoryName)) // subcategory already exist
                {
                    return(Ok());
                }
                _context.Categories.Add(new Category()
                {
                    Name = newCategory.SubcategoryName, ParentCategory = category
                });
            }
            else if (category == null) // Adding a new parent category
            {
                _context.Categories.Add(new Category()
                {
                    Name = newCategory.CategoryName
                });
            }

            await _context.SaveChangesAsync();

            return(Ok());
        }
        public IHttpActionResult UpdateCategoryName(int id, NewCategoryDTO category)
        {
            try
            {
                categoryService.ChangeCategoryName(id, category.Name);
            }
            catch (DatabaseException)
            {
                return(StatusCode(HttpStatusCode.InternalServerError));
            }
            catch (NotFoundException)
            {
                return(NotFound());
            }
            catch (AuctionException ex)
            {
                return(BadRequest(ex.Message));
            }

            return(Ok());
        }
        /// <summary>
        /// Creates new category
        /// </summary>
        /// <param name="category">New category</param>
        public void CreateCategory(NewCategoryDTO category)
        {
            try
            {
                if (!IsCategoryNameFree(category.Name))
                {
                    throw new AuctionException($"Category {category.Name} already exists.");
                }

                Database.Categories.AddCategory(new Category {
                    Name = category.Name
                });
                Database.Save();
            }
            catch (AuctionException ex)
            {
                throw ex;
            }
            catch (Exception)
            {
                throw new DatabaseException();
            }
        }
 public Task <ActionResult> AddCategory2([FromBody] NewCategoryDTO newCategory)
 {
     return(AddCategory(newCategory));
 }