Exemplo n.º 1
0
        public IActionResult GetMappings(ProductCategoryMappingsParametersModel parameters)
        {
            if (parameters.Limit < Constants.Configurations.MinLimit || parameters.Limit > Constants.Configurations.MaxLimit)
            {
                return(Error(HttpStatusCode.BadRequest, "limit", "invalid limit parameter"));
            }

            if (parameters.Page < Constants.Configurations.DefaultPageValue)
            {
                return(Error(HttpStatusCode.BadRequest, "page", "invalid page parameter"));
            }

            IList <ProductCategoryMappingDto> mappingsAsDtos =
                _productCategoryMappingsService.GetMappings(parameters.ProductId,
                                                            parameters.CategoryId,
                                                            parameters.Limit,
                                                            parameters.Page,
                                                            parameters.SinceId).Select(x => x.ToDto()).ToList();

            var productCategoryMappingRootObject = new ProductCategoryMappingsRootObject
            {
                ProductCategoryMappingDtos = mappingsAsDtos
            };

            var json = JsonFieldsSerializer.Serialize(productCategoryMappingRootObject, parameters.Fields);

            return(new RawJsonActionResult(json));
        }
Exemplo n.º 2
0
        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 category = _categoryApiService.GetCategoryById(productCategoryDelta.Dto.CategoryId.Value);

            if (category == null)
            {
                return(Error(HttpStatusCode.NotFound, "category_id", "not found"));
            }

            var product = _productApiService.GetProductById(productCategoryDelta.Dto.ProductId.Value);

            if (product == null)
            {
                return(Error(HttpStatusCode.NotFound, "product_id", "not found"));
            }

            var mappingsCount = _productCategoryMappingsService.GetMappingsCount(product.Id, category.Id);

            ProductCategory newProductCategory = new ProductCategory();

            productCategoryDelta.Merge(newProductCategory);

            if (mappingsCount == 0)
            {
                _categoryService.InsertProductCategory(newProductCategory);
                CustomerActivityService.InsertActivity("AddNewProductCategoryMapping", LocalizationService.GetResource("ActivityLog.AddNewProductCategoryMapping"), newProductCategory);
                //return Error(HttpStatusCode.BadRequest, "product_category_mapping", "already exist");
            }
            else
            {
                var mapping = _productCategoryMappingsService.GetMappings(product.Id, category.Id);
                newProductCategory.Id = mapping.FirstOrDefault().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(json));
        }
Exemplo n.º 3
0
        public IActionResult UpdateProductCategoryMapping(
            [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());
            }

            if (productCategoryDelta.Dto.CategoryId.HasValue)
            {
                var category = _categoryApiService.GetCategoryById(productCategoryDelta.Dto.CategoryId.Value);
                if (category == null)
                {
                    return(Error(HttpStatusCode.NotFound, "category_id", "not found"));
                }
            }

            if (productCategoryDelta.Dto.ProductId.HasValue)
            {
                var product = _productApiService.GetProductById(productCategoryDelta.Dto.ProductId.Value);
                if (product == null)
                {
                    return(Error(HttpStatusCode.NotFound, "product_id", "not found"));
                }
            }

            // We do not need to validate the category id, because this will happen in the model binder using the dto validator.
            var updateProductCategoryId = productCategoryDelta.Dto.Id;

            var productCategoryEntityToUpdate = _categoryService.GetProductCategoryById(updateProductCategoryId);

            if (productCategoryEntityToUpdate == null)
            {
                return(Error(HttpStatusCode.NotFound, "product_category_mapping", "not found"));
            }

            productCategoryDelta.Merge(productCategoryEntityToUpdate);

            _categoryService.UpdateProductCategory(productCategoryEntityToUpdate);

            //activity log
            CustomerActivityService.InsertActivity("UpdateProdutCategoryMapping",
                                                   LocalizationService.GetResource("ActivityLog.UpdateProdutCategoryMapping"), productCategoryEntityToUpdate);

            var updatedProductCategoryDto = productCategoryEntityToUpdate.ToDto();

            var productCategoriesRootObject = new ProductCategoryMappingsRootObject();

            productCategoriesRootObject.ProductCategoryMappingDtos.Add(updatedProductCategoryDto);

            var json = JsonFieldsSerializer.Serialize(productCategoriesRootObject, string.Empty);

            return(new RawJsonActionResult(json));
        }
Exemplo n.º 4
0
        public IActionResult GetMappingById(int id, string fields = "")
        {
            if (id <= 0)
            {
                return(Error(HttpStatusCode.BadRequest, "id", "invalid id"));
            }

            var mapping = _productCategoryMappingsService.GetById(id);

            if (mapping == null)
            {
                return(Error(HttpStatusCode.NotFound, "product_category_mapping", "not found"));
            }

            var productCategoryMappingsRootObject = new ProductCategoryMappingsRootObject();

            productCategoryMappingsRootObject.ProductCategoryMappingDtos.Add(mapping.ToDto());

            var json = JsonFieldsSerializer.Serialize(productCategoryMappingsRootObject, fields);

            return(new RawJsonActionResult(json));
        }