public IActionResult GetProductById(int id, string fields = "")
        {
            if (id <= 0)
            {
                return(Error(HttpStatusCode.BadRequest, "id", "invalid id"));
            }

            var product = _productApiService.GetProductById(id);

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

            var productDto = _dtoHelper.PrepareProductDTO(product);

            productDto.AdmindId = _genericAttributeService.GetAttribute <int>(product, "nop.product.admindid");

            var productsRootObject = new ProductsRootObjectDto();

            productsRootObject.Products.Add(productDto);

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

            return(new RawJsonActionResult(json));
        }
        public IHttpActionResult GetProductById(int id, string fields = "")
        {
            if (id <= 0)
            {
                return(Error(HttpStatusCode.BadRequest, "id", "invalid id"));
            }

            Product product = _productApiService.GetProductById(id);

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

            ProductDto productDto = product.ToDto();

            MapAdditionalPropertiesToDTO(product, productDto);

            var productsRootObject = new ProductsRootObjectDto();

            productsRootObject.Products.Add(productDto);

            var json = _jsonFieldsSerializer.Serialize(productsRootObject, fields);

            return(new RawJsonActionResult(json));
        }
Esempio n. 3
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));
        }
Esempio n. 4
0
        public void HandleEvent(EntityInserted <ProductPicture> eventMessage)
        {
            var product = _productApiService.GetProductById(eventMessage.Entity.ProductId);

            if (product != null)
            {
                ProductDto productDto = _dtoHelper.PrepareProductDTO(product);

                ProductUpdated(productDto);
            }
        }
        public IActionResult CreateProductManufacturerMapping(
            [ModelBinder(typeof(JsonModelBinder <ProductManufacturerMappingsDto>))]
            Delta <ProductManufacturerMappingsDto> productManufacturerDelta)
        {
            // Here we display the errors if the validation has failed at some point.
            if (!ModelState.IsValid)
            {
                return(Error());
            }

            var Manufacturer = _manufacturerApiService.GetManufacturerById(productManufacturerDelta.Dto.ManufacturerId.Value);

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

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

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

            var mappingsCount = _productManufacturerMappingsService.GetMappingsCount(product.Id, Manufacturer.Id);

            if (mappingsCount > 0)
            {
                return(Error(HttpStatusCode.BadRequest, "product_manufacturer_mapping", "already exist"));
            }

            var newProductManufacturer = new ProductManufacturer();

            productManufacturerDelta.Merge(newProductManufacturer);

            //inserting new Manufacturer
            _manufacturerService.InsertProductManufacturer(newProductManufacturer);

            // Preparing the result dto of the new product Manufacturer mapping
            var newProductManufacturerMappingDto = newProductManufacturer.ToDto();

            var productManufacturerMappingsRootObject = new ProductManufacturerMappingsRootObject();

            productManufacturerMappingsRootObject.ProductManufacturerMappingsDtos.Add(newProductManufacturerMappingDto);

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

            //activity log
            CustomerActivityService.InsertActivity("AddNewProductManufacturerMapping",
                                                   LocalizationService.GetResource("ActivityLog.AddNewProductManufacturerMapping"), newProductManufacturer);

            return(new RawJsonActionResult(json));
        }
Esempio n. 6
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));
        }
        private Product GetProduct(int?productId)
        {
            Product product = null;

            if (productId.HasValue)
            {
                int id = productId.Value;

                product = _productApiService.GetProductById(id);
            }
            return(product);
        }
Esempio n. 8
0
        public IActionResult UpdateProduct([ModelBinder(typeof(JsonModelBinder <ProductPriceDto>))] Delta <ProductPriceDto> productDelta)
        {
            // Here we display the errors if the validation has failed at some point.
            if (!ModelState.IsValid)
            {
                return(Error());
            }
            CustomerActivityService.InsertActivity("APIService", "Starting Product Update", null);

            var product = _productApiService.GetProductById(productDelta.Dto.Id);

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

            product.GrossPrice            = productDelta.Dto.Price;
            product.Price                 = productDelta.Dto.NetPrice;
            product.SupplierPrice         = productDelta.Dto.SupplierPrice;
            product.Supplier              = productDelta.Dto.Supplier;
            product.SupplierPriceCurrency = productDelta.Dto.SupplierPriceCurrency;
            product.LastPriceRefresh      = productDelta.Dto.LastPriceRefresh;
            product.Margin                = productDelta.Dto.Margin;
            product.MaxPrice              = productDelta.Dto.MaxPrice;
            product.AutoCalculatePrice    = productDelta.Dto.AutoCalculatePrice;
            product.WaitingForDelivery    = productDelta.Dto.WaitingForDelivery;
            product.ManageInventoryMethod = ManageInventoryMethod.ManageStockByProps;
            product.UpdatedOnUtc          = DateTime.UtcNow;
            product.CallForPrice          = productDelta.Dto.NetPrice <= 0;
            product.TaxCategoryId         = 2;//electronics
            _productService.UpdateProduct(product);

            CustomerActivityService.InsertActivity("APIService", $"Update product: '{product.Sku}' price: {productDelta.Dto.Price} ", product);
            var result = new ProductPricesRootObjectDto();

            result.Products.Add(productDelta.Dto);
            return(new RawJsonActionResult(JsonFieldsSerializer.Serialize(result, string.Empty)));
        }
        public async Task <IActionResult> GetProductById(int id, string fields = "")
        {
            if (id <= 0)
            {
                return(Error(HttpStatusCode.BadRequest, "id", "invalid id"));
            }

            var product = _productApiService.GetProductById(id);

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

            var productDto = await _dtoHelper.PrepareProductDTOAsync(product);

            var productsRootObject = new ProductsRootObjectDto();

            productsRootObject.Products.Add(productDto);

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

            return(new RawJsonActionResult(json));
        }
        private void HandleStoreMappingEvent(int entityId, string entityName)
        {
            // When creating or editing a category after saving the store mapping the category is not updated
            // so we should listen for StoreMapping update/delete and fire a webhook with the updated entityDto(with correct storeIds).
            if (entityName == "Category")
            {
                var category = _categoryApiService.GetCategoryById(entityId);

                if (category != null)
                {
                    CategoryDto categoryDto = _dtoHelper.PrepareCategoryDTO(category);

                    string webhookEvent = WebHookNames.CategoriesUpdate;

                    if (categoryDto.Deleted == true)
                    {
                        webhookEvent = WebHookNames.CategoriesDelete;
                    }

                    NotifyRegisteredWebHooks(categoryDto, webhookEvent, categoryDto.StoreIds);
                }
            }
            else if (entityName == "Product")
            {
                var product = _productApiService.GetProductById(entityId);

                if (product != null)
                {
                    ProductDto productDto = _dtoHelper.PrepareProductDTO(product);

                    string webhookEvent = WebHookNames.ProductsUpdate;

                    if (productDto.Deleted == true)
                    {
                        webhookEvent = WebHookNames.ProductsDelete;
                    }

                    NotifyRegisteredWebHooks(productDto, webhookEvent, productDto.StoreIds);
                }
            }
        }
Esempio n. 11
0
        public IActionResult GetProductById(int id, string fields = "")
        {
            var consumerKey    = Request.Headers.GetInstantCheckoutConsumerKey();
            var consumerSecret = Request.Headers.GetInstantCheckoutConsumerSecret();

            if (consumerKey == Guid.Empty || consumerSecret == Guid.Empty)
            {
                return(Unauthorized());
            }

            var storeScope = _storeContext.ActiveStoreScopeConfiguration;
            var instantCheckoutSettings = _settingService.LoadSetting <InstantCheckoutSettings>(storeScope);

            if (consumerKey != instantCheckoutSettings.ConsumerKey &&
                consumerSecret != instantCheckoutSettings.ConsumerSecret)
            {
                return(Unauthorized());
            }

            if (id <= 0)
            {
                return(Error(HttpStatusCode.BadRequest, "id", "invalid id"));
            }

            var product = _productApiService.GetProductById(id);

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

            var productDto = _dtoHelper.PrepareProductDTO(product);

            var productsRootObject = new ProductsRootObjectDto();

            productsRootObject.Products.Add(productDto);

            var json = _jsonFieldsSerializer.Serialize(productsRootObject, fields);

            return(new RawJsonActionResult(json));
        }