Exemple #1
0
        public IActionResult GetProductAttributes(ProductAttributesParametersModel parameters)
        {
            if (parameters.Limit < Configurations.MinLimit || parameters.Limit > Configurations.MaxLimit)
            {
                return(Error(HttpStatusCode.BadRequest, "limit", "invalid limit parameter"));
            }

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

            var allProductAttributes = _productAttributesApiService.GetProductAttributes(parameters.Limit, parameters.Page, parameters.SinceId);

            IList <CustomeProductAttributes> productAttributesAsDtos = allProductAttributes.Select(productAttribute => _dtoHelper.PrepareProductAttributeDTO(productAttribute)).ToList();

            var productAttributesRootObject = new CustomeProductAttributesRootObjectDto()
            {
                ProductAttributes = productAttributesAsDtos
            };

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

            return(new RawJsonActionResult(json));
        }
Exemple #2
0
        public IActionResult UpdateProductAttribute([ModelBinder(typeof(JsonModelBinder <CustomeProductAttributes>))] Delta <CustomeProductAttributes> productAttributeDelta)
        {
            // Here we display the errors if the validation has failed at some point.
            if (!ModelState.IsValid)
            {
                return(Error());
            }

            var productAttribute = _productAttributesApiService.GetById(productAttributeDelta.Dto.Id);

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

            productAttributeDelta.Merge(productAttribute);


            _productAttributeService.UpdateProductAttribute(productAttribute);

            CustomerActivityService.InsertActivity("EditProductAttribute",
                                                   LocalizationService.GetResource("ActivityLog.EditProductAttribute"), productAttribute);

            // Preparing the result dto of the new product attribute
            var productAttributeDto = _dtoHelper.PrepareProductAttributeDTO(productAttribute);

            var productAttributesRootObjectDto = new CustomeProductAttributesRootObjectDto();

            productAttributesRootObjectDto.ProductAttributes.Add(productAttributeDto);

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

            return(new RawJsonActionResult(json));
        }
Exemple #3
0
        public IActionResult CreateProductAttribute([ModelBinder(typeof(JsonModelBinder <CustomeProductAttributes>))] Delta <CustomeProductAttributes> productAttributeDelta)
        {
            // Here we display the errors if the validation has failed at some point.
            if (!ModelState.IsValid)
            {
                return(Error());
            }

            // Inserting the new product
            var productAttribute = new ProductAttribute();

            productAttributeDelta.Merge(productAttribute);

            _productAttributeService.InsertProductAttribute(productAttribute);

            CustomerActivityService.InsertActivity("AddNewProductAttribute",
                                                   LocalizationService.GetResource("ActivityLog.AddNewProductAttribute"), productAttribute);

            // Preparing the result dto of the new product
            var productAttributeDto = _dtoHelper.PrepareProductAttributeDTO(productAttribute);

            var productAttributesRootObjectDto = new CustomeProductAttributesRootObjectDto();

            productAttributesRootObjectDto.ProductAttributes.Add(productAttributeDto);

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

            return(new RawJsonActionResult(json));
        }
Exemple #4
0
        public IActionResult GetProductAttributeById(int id, string fields = "")
        {
            if (id <= 0)
            {
                return(Error(HttpStatusCode.BadRequest, "id", "invalid id"));
            }

            var productAttribute = _productAttributesApiService.GetById(id);

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

            var productAttributeDto = _dtoHelper.PrepareProductAttributeDTO(productAttribute);

            var productAttributesRootObject = new CustomeProductAttributesRootObjectDto();

            productAttributesRootObject.ProductAttributes.Add(productAttributeDto);

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

            return(new RawJsonActionResult(json));
        }
Exemple #5
0
        public IActionResult CreateProductAttrs([ModelBinder(typeof(JsonModelBinder <CustomeProductAttributesDto>))] Delta <CustomeProductAttributesDto> customeProductAttributesDtoDelta)
        {
            // Here we display the errors if the validation has failed at some point.
            if (!ModelState.IsValid)
            {
                return(Error());
            }
            var list = _productAttributeService.GetAllProductAttributes().Select(l => l.Name.ToLower().Trim()).ToList();

            if (!list.Contains(customeProductAttributesDtoDelta.Dto.AttributeName.ToLower().Trim()))
            {
                var productAttribute = new ProductAttribute()
                {
                    Description = customeProductAttributesDtoDelta.Dto.Description,
                    Id          = 0,
                    Name        = customeProductAttributesDtoDelta.Dto.AttributeName
                };
                _productAttributeService.InsertProductAttribute(productAttribute);
                CustomerActivityService.InsertActivity("AddNewProductAttribute", LocalizationService.GetResource("ActivityLog.AddNewProductAttribute"), productAttribute);
            }
            //get Product By SKU
            var product = _productService.GetProductBySku(customeProductAttributesDtoDelta.Dto.Sku);

            if (product == null)
            {
                return(Error(HttpStatusCode.NotFound, "product", "not found"));
            }
            var attr = _productAttributeService.GetProductAttributeByAXId(customeProductAttributesDtoDelta.Dto.AttributeId);

            if (attr == null)
            {
                if (customeProductAttributesDtoDelta.Dto.Values.Count > 0)
                {
                    var prodAttr = _productAttributeService.GetProductAttributeByName(customeProductAttributesDtoDelta.Dto.AttributeName);
                    //create
                    var tempAttr = new ProductAttributeMapping()
                    {
                        AttrIdAX             = customeProductAttributesDtoDelta.Dto.AttributeId,
                        AttributeControlType = AttributeControlType.DropdownList,
                        ProductId            = product.Id,
                        ProductAttributeId   = prodAttr.Id,
                        IsRequired           = true,
                    };
                    _productAttributeService.InsertProductAttributeMapping(tempAttr);
                    //
                    foreach (var item in customeProductAttributesDtoDelta.Dto.Values)
                    {
                        _productAttributeService.InsertProductAttributeValue(new ProductAttributeValue()
                        {
                            Id = 0,
                            ProductAttributeMappingId = tempAttr.Id,
                            Name            = item.Text,
                            PriceAdjustment = item.Price,
                            DisplayOrder    = item.DisplayOrder,
                        });
                    }
                }
            }
            else
            {
                var prodAttr = _productAttributeService.GetProductAttributeByName(customeProductAttributesDtoDelta.Dto.AttributeName);
                if (customeProductAttributesDtoDelta.Dto.Values.Count == 0)
                {
                    _productAttributeService.DeleteProductAttributeMapping(attr);
                }
                else
                {
                    //edit
                    attr.ProductAttributeId = prodAttr.Id;
                    _productAttributeService.UpdateProductAttributeMapping(attr);
                    //
                    var listofValues = _productAttributeService.GetProductAttributeValues(attr.Id);
                    //delete
                    foreach (var item in listofValues)
                    {
                        _productAttributeService.DeleteProductAttributeValue(item);
                    }
                    foreach (var item in customeProductAttributesDtoDelta.Dto.Values)
                    {
                        _productAttributeService.InsertProductAttributeValue(new ProductAttributeValue()
                        {
                            Id = 0,
                            ProductAttributeMappingId = attr.Id,
                            Name            = item.Text,
                            PriceAdjustment = item.Price,
                            DisplayOrder    = item.DisplayOrder,
                        });
                    }
                }
            }

            var customeProductAttributesRootObjectDto = new CustomeProductAttributesRootObjectDto();

            customeProductAttributesRootObjectDto.ProductAttributes.Add(new CustomeProductAttributesDto()
            {
                AttributeId   = 123456,
                AttributeName = "Test",
                Description   = "Test",
                Sku           = "1200",
                Values        = new List <ValueDto>()
                {
                    new ValueDto()
                    {
                        DisplayOrder = 0,
                        Price        = 1222,
                        Text         = "Test"
                    }
                    ,
                    new ValueDto()
                    {
                        DisplayOrder = 0,
                        Price        = 1222,
                        Text         = "Test"
                    }
                }
            });

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

            return(new RawJsonActionResult("Done"));
        }