public IActionResult GetCategoryById(int id, string fields = "") { if (id <= 0) { return(Error(HttpStatusCode.BadRequest, "id", "invalid id")); } var category = _categoryApiService.GetCategoryByAXId(id); if (category == null) { return(Error(HttpStatusCode.NotFound, "category", "category not found")); } var categoryDto = _dtoHelper.PrepareCategoryDTO(category); var categoriesRootObject = new CategoriesRootObject(); categoriesRootObject.Categories.Add(categoryDto); var json = JsonFieldsSerializer.Serialize(categoriesRootObject, fields); return(new RawJsonActionResult(json)); }
public IActionResult CreateProductCategoryMapping([ModelBinder(typeof(JsonModelBinder <ProductCategoryMappingDto>))] Delta <ProductCategoryMappingDto> productCategoryDelta) { // Here we display the errors if the validation has failed at some point. if (!ModelState.IsValid) { return(Error()); } var product = _productApiService.GetProductBySKU(productCategoryDelta.Dto.ProductSku); if (product == null) { return(Error(HttpStatusCode.NotFound, "product_sku", "not found")); } var categories = _categoryService.GetByProductId(product.Id); //delete for (int i = 0; i < categories.Count; i++) { if (categories[i].Category.CategoryId == null || !productCategoryDelta.Dto.Categories.Contains((int)categories[i].Category.CategoryId) || categories[i].Category.CategoryId == null) { _categoryService.DeleteProductCategory(categories[i]); //activity log CustomerActivityService.InsertActivity("DeleteProductCategoryMapping", LocalizationService.GetResource("ActivityLog.DeleteProductCategoryMapping"), categories[i]); } } //insert for (int i = 0; i < productCategoryDelta.Dto.Categories.Count; i++) { if (!categories.Any(l => l.Id == productCategoryDelta.Dto.Categories[i])) { var cateTemp = _categoryApiService.GetCategoryByAXId(productCategoryDelta.Dto.Categories[i]); if (cateTemp != null) { var temp = new ProductCategory() { Id = 0, ProductId = product.Id, IsFeaturedProduct = productCategoryDelta.Dto.IsFeaturedProduct ?? false, DisplayOrder = productCategoryDelta.Dto.DisplayOrder ?? 0, CategoryId = cateTemp.Id }; _categoryService.InsertProductCategory(temp); //activity log CustomerActivityService.InsertActivity("AddNewProductCategoryMapping", LocalizationService.GetResource("ActivityLog.AddNewProductCategoryMapping"), temp); } } } //var category = _categoryApiService.GetCategoryByAXId(productCategoryDelta.Dto.CategoryIdDynamics.Value); //if (category == null) //{ // return Error(HttpStatusCode.NotFound, "category_id", "not found"); //} //productCategoryDelta.Dto.CategoryId = category.Id; //var mappingsCount = _productCategoryMappingsService.GetMappingsCount(product.Id, category.Id); //if (mappingsCount > 0) //{ // return Error(HttpStatusCode.BadRequest, "product_category_mapping", "already exist"); //} //var newProductCategory = new ProductCategory(); //productCategoryDelta.Merge(newProductCategory); //newProductCategory.ProductId = product.Id; //newProductCategory.CategoryId = category.Id; ////inserting new category //_categoryService.InsertProductCategory(newProductCategory); //// Preparing the result dto of the new product category mapping //var newProductCategoryMappingDto = newProductCategory.ToDto(); //var productCategoryMappingsRootObject = new ProductCategoryMappingsRootObject(); //productCategoryMappingsRootObject.ProductCategoryMappingDtos.Add(newProductCategoryMappingDto); //var json = JsonFieldsSerializer.Serialize(productCategoryMappingsRootObject, string.Empty); ////activity log //CustomerActivityService.InsertActivity("AddNewProductCategoryMapping", LocalizationService.GetResource("ActivityLog.AddNewProductCategoryMapping"), newProductCategory); return(new RawJsonActionResult("Done")); }