public async Task <IActionResult> CreateCategory( [FromBody] Models.CategoriesForCreation categoriesForCreation) { // model validation if (categoriesForCreation == null) { return(BadRequest()); } if (!ModelState.IsValid) { // return 422 - Unprocessable Entity when validation fails return(new UnprocessableEntityObjectResult(ModelState)); } var categoryEntity = _mapper.Map <Categories>(categoriesForCreation); _categoriesRepository.AddCategory(categoryEntity); // save the changes await _categoriesRepository.SaveChanges(); // Fetch the movie from the data store so the director is included await _categoriesRepository.GetCategory(categoryEntity.CategoryId); return(Ok(CreatedAtRoute("GetCategory", new { categoryId = categoryEntity.CategoryId }, _mapper.Map <Models.Categories>(categoryEntity)))); }
public void AddCategory(CategoryViewModel category) { Category newCategory = new Category() { Name = category.Name }; _categoriesRepo.AddCategory(newCategory); }
public JsonResult AddCategory(CategoryModel entity) { try { Category cate = new Category(); cate.CategoryName = entity.CategoryName; cate.Description = entity.Description; cate.IsActive = entity.IsActive; cate.ParentCategoryID = entity.ParentCategoryID; cate.IsDelete = false; cate.CreatedDate = DateTime.Now; cate.ModifiedDate = DateTime.Now; if (entity.ParentCategoryID != null) { cate.Depth = (_repo.GetCategory(cate.ParentCategoryID).Depth + 1); } else { cate.Depth = 1; cate.ParentCategoryID = 1; } var data = _repo.AddCategory(cate); if (data > 0) { var resp = new { result = true, message = "Category Added Successfully.", AddedId = data }; return(Json(resp, JsonRequestBehavior.AllowGet)); } else { var resp = new { result = false, message = "Something went wrong will adding category.", AddedId = 0 }; return(Json(resp, JsonRequestBehavior.AllowGet)); } } catch (Exception ex) { var resp = new { result = false, message = ex.Message, AddedId = 0 }; return(Json(resp, JsonRequestBehavior.AllowGet)); } }
public async ValueTask <Category> AddCategory(string categoryName, int?sortOrder) { var category = new Category { Id = Guid.NewGuid(), Name = categoryName ?? "", Sort = sortOrder ?? 0 }; await _categoriesRepo.AddCategory(category); return(category); }
public IActionResult CreateCategory(Category category) { if (!ModelState.IsValid) { // store Subcategories data which has been added ViewBag.Subcategories = category.Subcategories == null ? new List <Subcategory>() { } : category.Subcategories; return(View("CreateCategory")); } _categoriesRepository.AddCategory(category); return(RedirectToAction("ManageCategories")); }
public async Task <ActionResult <Categories> > CreateCategory([FromBody] Categories category) { try { if (category == null) { return(BadRequest()); } var categoryToCreate = await categoriesRepository.AddCategory(category); return(CreatedAtAction(nameof(GetCategory), new { categoryId = categoryToCreate.CategoryId }, categoryToCreate)); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Error creating a category")); } }
public async Task <IActionResult> AddNewCategory([FromBody] Category category) { if (ModelState.IsValid) { var res = await _categoriesRepo.AddCategory(category); if (res > 0) { category = (await _categoriesRepo.GetCategoryByNameAsync(category.Name)).FirstOrDefault(); if (category != null) { return(Json(new { model = category, isValid = true, err = "" })); } } } // bad practice, but I'm lazy. return(Json(new { model = category, isValid = false, error = $"Couldn't create category with the name: {category.Name}" })); }
public CategoryView AddCategory([FromBody] CategoryView category) { var newCategory = _repository.AddCategory(category); return(newCategory); }
public void AddCategory(CategoryViewModel category) { _categoriesRepo.AddCategory(_mapper.Map <Category>(category)); }
public ActionResult AddProduct(Categories entity) { _categoriesRepository.AddCategory(entity); return(Ok(entity)); }