public IHttpActionResult GetCategoryById(int id, string fields = "") { if (id <= 0) { return(Error(HttpStatusCode.BadRequest, "id", "invalid id")); } Category category = _categoryApiService.GetCategoryById(id); if (category == null) { return(Error(HttpStatusCode.NotFound, "category", "category not found")); } CategoryDto categoryDto = _dtoHelper.PrepareCategoryDTO(category); var categoriesRootObject = new CategoriesRootObject(); categoriesRootObject.Categories.Add(categoryDto); var json = _jsonFieldsSerializer.Serialize(categoriesRootObject, fields); return(new RawJsonActionResult(json)); }
public async Task <IActionResult> CreateCategory( [ModelBinder(typeof(JsonModelBinder <CategoryDto>))] Delta <CategoryDto> categoryDelta) { // Here we display the errors if the validation has failed at some point. if (!ModelState.IsValid) { return(Error()); } //If the validation has passed the categoryDelta object won't be null for sure so we don't need to check for this. Picture insertedPicture = null; // We need to insert the picture before the category so we can obtain the picture id and map it to the category. if (categoryDelta.Dto.Image?.Binary != null) { insertedPicture = await PictureService.InsertPictureAsync(categoryDelta.Dto.Image.Binary, categoryDelta.Dto.Image.MimeType, string.Empty); } // Inserting the new category var category = await _factory.InitializeAsync(); categoryDelta.Merge(category); if (insertedPicture != null) { category.PictureId = insertedPicture.Id; } await _categoryService.InsertCategoryAsync(category); await UpdateAclRolesAsync(category, categoryDelta.Dto.RoleIds); await UpdateDiscountsAsync(category, categoryDelta.Dto.DiscountIds); await UpdateStoreMappingsAsync(category, categoryDelta.Dto.StoreIds); //search engine name if (categoryDelta.Dto.SeName != null) { var seName = await _urlRecordService.ValidateSeNameAsync(category, categoryDelta.Dto.SeName, category.Name, true); await _urlRecordService.SaveSlugAsync(category, seName, 0); } await CustomerActivityService.InsertActivityAsync("AddNewCategory", await LocalizationService.GetResourceAsync("ActivityLog.AddNewCategory"), category); // Preparing the result dto of the new category var newCategoryDto = await _dtoHelper.PrepareCategoryDTOAsync(category); var categoriesRootObject = new CategoriesRootObject(); categoriesRootObject.Categories.Add(newCategoryDto); var json = JsonFieldsSerializer.Serialize(categoriesRootObject, string.Empty); return(new RawJsonActionResult(json)); }
public IActionResult UpdateCategory( [ModelBinder(typeof(JsonModelBinder <CategoryDto>))] Delta <CategoryDto> categoryDelta) { // Here we display the errors if the validation has failed at some point. if (!ModelState.IsValid) { return(Error()); } var category = _categoryApiService.GetCategoryByAXId(categoryDelta.Dto.CategoryId); int tempId = category.Id; if (category == null) { return(Error(HttpStatusCode.NotFound, "category", "category not found")); } categoryDelta.Merge(category); category.Id = tempId; category.UpdatedOnUtc = DateTime.UtcNow; if (categoryDelta.Dto.ParentId != null) { if (categoryDelta.Dto.ParentId == 0) { category.ParentCategoryId = 0; } else { category.ParentCategoryId = category.Id; } } _categoryService.UpdateCategory(category); UpdatePicture(category, categoryDelta.Dto.Image); UpdateAclRoles(category, categoryDelta.Dto.RoleIds); UpdateDiscounts(category, categoryDelta.Dto.DiscountIds); UpdateStoreMappings(category, categoryDelta.Dto.StoreIds); //search engine name if (categoryDelta.Dto.SeName != null) { var seName = _urlRecordService.ValidateSeName(category, categoryDelta.Dto.SeName, category.Name, true); _urlRecordService.SaveSlug(category, seName, 0); } _categoryService.UpdateCategory(category); CustomerActivityService.InsertActivity("UpdateCategory", LocalizationService.GetResource("ActivityLog.UpdateCategory"), category); var categoryDto = _dtoHelper.PrepareCategoryDTO(category); var categoriesRootObject = new CategoriesRootObject(); categoriesRootObject.Categories.Add(categoryDto); var json = JsonFieldsSerializer.Serialize(categoriesRootObject, string.Empty); return(new RawJsonActionResult(json)); }