public IActionResult GetMappings(ProductManufacturerMappingsParametersModel 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"));
            }

            IList <ProductManufacturerMappingsDto> mappingsAsDtos =
                _productManufacturerMappingsService.GetMappings(parameters.ProductId,
                                                                parameters.ManufacturerId,
                                                                parameters.Limit,
                                                                parameters.Page,
                                                                parameters.SinceId).Select(x => x.ToDto()).ToList();

            var productManufacturerMappingRootObject = new ProductManufacturerMappingsRootObject()
            {
                ProductManufacturerMappingsDtos = mappingsAsDtos
            };

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

            return(new RawJsonActionResult(json));
        }
        public IActionResult UpdateProductManufacturerMapping(
            [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());
            }

            if (productManufacturerDelta.Dto.ManufacturerId.HasValue)
            {
                var Manufacturer = _manufacturerApiService.GetManufacturerById(productManufacturerDelta.Dto.ManufacturerId.Value);
                if (Manufacturer == null)
                {
                    return(Error(HttpStatusCode.NotFound, "manufacturer_id", "not found"));
                }
            }

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

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

            var productManufacturerEntityToUpdate = _manufacturerService.GetProductManufacturerById(updateProductManufacturerId);

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

            productManufacturerDelta.Merge(productManufacturerEntityToUpdate);

            _manufacturerService.UpdateProductManufacturer(productManufacturerEntityToUpdate);

            //activity log
            CustomerActivityService.InsertActivity("UpdateProdutManufacturerMapping",
                                                   LocalizationService.GetResource("ActivityLog.UpdateProdutManufacturerMapping"),
                                                   productManufacturerEntityToUpdate);

            var updatedProductManufacturerDto = productManufacturerEntityToUpdate.ToDto();

            var productManufacturersRootObject = new ProductManufacturerMappingsRootObject();

            productManufacturersRootObject.ProductManufacturerMappingsDtos.Add(updatedProductManufacturerDto);

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

            return(new RawJsonActionResult(json));
        }
        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));
        }
        public IActionResult GetMappingById(int id, string fields = "")
        {
            if (id <= 0)
            {
                return(Error(HttpStatusCode.BadRequest, "id", "invalid id"));
            }

            var mapping = _productManufacturerMappingsService.GetById(id);

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

            var productManufacturerMappingsRootObject = new ProductManufacturerMappingsRootObject();

            productManufacturerMappingsRootObject.ProductManufacturerMappingsDtos.Add(mapping.ToDto());

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

            return(new RawJsonActionResult(json));
        }