public ProductImageDTO ToDTO(ProductImageEntity entity)
        {
            ProductImageDTO dto = new ProductImageDTO();

            dto.Id        = entity.Id;
            dto.ImgSrc    = entity.ImgSrc;
            dto.ProductId = entity.ProductId;
            return(dto);
        }
        public void ProductImages_CreateFindUpdateDelete()
        {
            //Create API Proxy.
            var proxy = CreateApiProxy();

            //Create Product Image
            var productImage = new ProductImageDTO
            {
                AlternateText = "test alternate",
                Caption       = "test caption",
                FileName      = "ProductImage.jpg",
                ProductId     = TestConstants.TestProductBvin,
                StoreId       = 1
            };

            //Create product image
            var imagePath      = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data/ProductImage.jpg");
            var imageData      = File.ReadAllBytes(imagePath);
            var createResponse = proxy.ProductImagesCreate(productImage, imageData);

            CheckErrors(createResponse);
            Assert.IsFalse(string.IsNullOrEmpty(createResponse.Content.Bvin));

            //Find Product image
            var findResponse = proxy.ProductImagesFind(createResponse.Content.Bvin);

            CheckErrors(findResponse);
            Assert.AreEqual(createResponse.Content.Caption, findResponse.Content.Caption);
            Assert.AreEqual(createResponse.Content.FileName, findResponse.Content.FileName);

            //Find Product image
            var findByProductResponse = proxy.ProductImagesFindAllByProduct(productImage.ProductId);

            CheckErrors(findByProductResponse);

            //Update product image
            findResponse.Content.Caption = findResponse.Content.Caption + "updated";
            var updateResponse = proxy.ProductImagesUpdate(findResponse.Content);

            CheckErrors(updateResponse);
            Assert.AreEqual(findResponse.Content.Caption, updateResponse.Content.Caption);

            //Upload product image
            var imageUplaodResponse = proxy.ProductImagesUpload(TestConstants.TestProductBvin,
                                                                createResponse.Content.Bvin, "NewProductImage.jpg", imageData);

            CheckErrors(imageUplaodResponse);
            Assert.IsTrue(imageUplaodResponse.Content);

            //Delete Product Image
            var deleteResponse = proxy.ProductImagesDelete(findResponse.Content.Bvin);

            CheckErrors(deleteResponse);
            Assert.IsTrue(deleteResponse.Content);
        }
Exemple #3
0
        // DTO
        public ProductImageDTO ToDto()
        {
            var dto = new ProductImageDTO();

            dto.AlternateText  = AlternateText;
            dto.Bvin           = Bvin;
            dto.Caption        = Caption;
            dto.FileName       = FileName;
            dto.LastUpdatedUtc = LastUpdatedUtc;
            dto.ProductId      = ProductId;
            dto.SortOrder      = SortOrder;
            dto.StoreId        = StoreId;

            return(dto);
        }
        // DTO
        public ProductImageDTO ToDto()
        {
            ProductImageDTO dto = new ProductImageDTO();

            dto.AlternateText  = this.AlternateText;
            dto.Bvin           = this.Bvin;
            dto.Caption        = this.Caption;
            dto.FileName       = this.FileName;
            dto.LastUpdatedUtc = this.LastUpdatedUtc;
            dto.ProductId      = this.ProductId;
            dto.SortOrder      = this.SortOrder;
            dto.StoreId        = this.StoreId;

            return(dto);
        }
Exemple #5
0
        public void FromDto(ProductImageDTO dto)
        {
            if (dto == null)
            {
                return;
            }

            AlternateText  = dto.AlternateText ?? string.Empty;
            Bvin           = dto.Bvin ?? string.Empty;
            Caption        = dto.Caption ?? string.Empty;
            FileName       = dto.FileName ?? string.Empty;
            LastUpdatedUtc = dto.LastUpdatedUtc;
            ProductId      = dto.ProductId ?? string.Empty;
            SortOrder      = dto.SortOrder;
            StoreId        = dto.StoreId;
        }
Exemple #6
0
        // Create or Update
        public override string PostAction(string parameters, NameValueCollection querystring, string postdata)
        {
            var data     = string.Empty;
            var bvin     = FirstParameter(parameters);
            var response = new ApiResponse <ProductImageDTO>();

            ProductImageDTO postedItem = null;

            try
            {
                postedItem = Json.ObjectFromJson <ProductImageDTO>(postdata);
            }
            catch (Exception ex)
            {
                response.Errors.Add(new ApiError("EXCEPTION", ex.Message));
                return(Json.ObjectToJson(response));
            }

            var item = new ProductImage();

            item.FromDto(postedItem);

            if (string.IsNullOrEmpty(bvin))
            {
                if (HccApp.CatalogServices.ProductImageCreate(item))
                {
                    bvin = item.Bvin;
                }
            }
            else
            {
                HccApp.CatalogServices.ProductImageUpdate(item);
            }

            var resultItem = HccApp.CatalogServices.ProductImages.Find(bvin);

            if (resultItem != null)
            {
                response.Content = resultItem.ToDto();
            }

            data = Json.ObjectToJson(response);

            return(data);
        }
Exemple #7
0
        // Create or Update
        public override string PostAction(string parameters, System.Collections.Specialized.NameValueCollection querystring, string postdata)
        {
            string data = string.Empty;
            string bvin = FirstParameter(parameters);
            ApiResponse <ProductImageDTO> response = new ApiResponse <ProductImageDTO>();

            ProductImageDTO postedItem = null;

            try
            {
                postedItem = MerchantTribe.Web.Json.ObjectFromJson <ProductImageDTO>(postdata);
            }
            catch (Exception ex)
            {
                response.Errors.Add(new ApiError("EXCEPTION", ex.Message));
                return(MerchantTribe.Web.Json.ObjectToJson(response));
            }

            ProductImage item = new ProductImage();

            item.FromDto(postedItem);

            if (bvin == string.Empty)
            {
                if (MTApp.CatalogServices.ProductImages.Create(item))
                {
                    bvin = item.Bvin;
                }
            }
            else
            {
                MTApp.CatalogServices.ProductImages.Update(item);
            }
            ProductImage resultItem = MTApp.CatalogServices.ProductImages.Find(bvin);

            if (resultItem != null)
            {
                response.Content = resultItem.ToDto();
            }

            data = MerchantTribe.Web.Json.ObjectToJson(response);
            return(data);
        }
Exemple #8
0
        public static ProductImageDTO InsertProductImage(Guid ProductId, HttpPostedFile file, bool PrimaryImage)
        {
            string ImagePath = Properties.Settings.Default.ImagePath;

            var dbProductImage  = new ProductImage();
            var productImageDTO = new ProductImageDTO();

            int categoryId = 0;
            ClassifierResponse classifierResponse = null;

            string ProductName = string.Empty;
            string imagePath   = "";

            string productProperties = "{}";

            //Save image to disk
            if (file != null && file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);

                dbProductImage.Id                = Guid.NewGuid();
                dbProductImage.Inserted          = DateTime.Now;
                dbProductImage.SelectedForExport = true;

                var path = ImagePath + dbProductImage.Id + "_" + fileName;

                var inp = file.InputStream;

                var savedProduct = GetProduct(ProductId);

                SaveOriginalImage(file, ImagePath, savedProduct.Id);

                //Save with number
                var cvProvider = new CVProvider();
                cvProvider.SaveImageWithProductNumber(new FileInfo(path), savedProduct.ProductNumber, inp);

                dbProductImage.ProductId    = ProductId;
                dbProductImage.PrimaryImage = PrimaryImage;

                imagePath           = "Images/" + Path.GetFileName(path);
                dbProductImage.Path = imagePath;

                //Classify image if not classified

                var prevCategory = savedProduct.Category;

                if (prevCategory == null)
                {
                    var classifierProvider = new ClassifierProvider();
                    classifierResponse = classifierProvider.ClassifyImage(path);

                    dbProductImage.Category = classifierResponse.Category;
                    dbProductImage.ClassifierPropability = classifierResponse.CategoryPorpability;
                    //ToDo: Check i low propability
                }
                else
                {
                    dbProductImage.Category = prevCategory;
                    dbProductImage.ClassifierPropability = 0;
                }

                using (var db = new DitatEntities())
                {
                    db.ProductImages.Add(dbProductImage);
                    db.SaveChanges();

                    productImageDTO.Category = dbProductImage.Category;
                    switch (dbProductImage.Category)
                    {
                    case "DVD":
                        categoryId = 2;

                        var     eanAttributeProvider = new EANAttributeProvider();
                        EANInfo eanInfo = eanAttributeProvider.GetEANInfo(path);

                        productProperties = "[]";     //Only name is provided from ean-search.org
                        ProductName       = eanInfo.name;

                        productImageDTO.ProductProperties = productProperties;
                        productImageDTO.Name         = ProductName;
                        productImageDTO.ErrorMessage = eanInfo.ErrorMessage;

                        /*
                         * productProperties = @"[{""Property"":""Artist"",""Value"":""" + "Test" + @"""},{""Property"":""År"",""Value"":""" + "1234" + @"""}]";
                         * ProductName = dbProductImage.Category + " - " + (dbProductImage.ClassifierPropability * 100).ToString() + "%, ej klart";
                         * productImageDTO.ProductProperties = productProperties;
                         * productImageDTO.Name = ProductName;
                         */

                        break;

                    case "CD":
                        string format = string.Empty;
                        string code   = string.Empty;

                        categoryId = 3;

                        var    cdAttributeProvider = new CDAttributeProvider();
                        CDInfo cdInfo = cdAttributeProvider.GetCDInfo(path);

                        productProperties = cdInfo.ProductProperties;

                        ProductName = cdInfo.Title;

                        productImageDTO.ProductProperties = productProperties;
                        productImageDTO.Name         = ProductName;
                        productImageDTO.ErrorMessage = cdInfo.ErrorMessage;

                        break;

                    default:
                        if (savedProduct.Name == "")
                        {
                            ProductName = dbProductImage.Category + " - " + (dbProductImage.ClassifierPropability * 100).ToString() + "%, ej klart";
                        }
                        break;
                    }

                    if (dbProductImage.Category == "Bok" && (savedProduct.Name == "Bok" || savedProduct.Name == ""))
                    {
                        categoryId = 1; //Bok

                        var bookAttributeProvider = new BookAttributeProvider();
                        var bookInfo = bookAttributeProvider.GetBookInfo(path);

                        productProperties = bookInfo.ProductProperties;

                        ProductName = bookInfo.Title;

                        productImageDTO.ProductProperties = productProperties;
                        productImageDTO.Name         = bookInfo.Title;
                        productImageDTO.ErrorMessage = bookInfo.ErrorMessage;
                    }

                    if (dbProductImage.Category == "Kruka")
                    {
                        categoryId  = 7;
                        ProductName = "Kruka";

                        productProperties = @"[{ ""Key"":""Width"",""Value"":"""",""Type"":""number"",""Icon"":""panorama_horizontal""},{ ""Key"":""Height"",""Value"":"""",""Type"":""number"",""Icon"":""panorama_vertical""}]";

                        var imageDimensionResponse = ExtractDimensions(new FileInfo(path));

                        //Save processed images if they exists
                        if (imageDimensionResponse.ImageWithDimensions != null)
                        {
                            var dbProductImageWithDimensions = new ProductImage();

                            dbProductImageWithDimensions.Id = Guid.NewGuid();

                            path = ImagePath + dbProductImageWithDimensions.Id + "_" + fileName;

                            imageDimensionResponse.ImageWithDimensions.Save(path);

                            dbProductImageWithDimensions.ProductId         = ProductId;
                            dbProductImageWithDimensions.PrimaryImage      = false;
                            dbProductImageWithDimensions.Path              = "Images/" + Path.GetFileName(path);
                            dbProductImageWithDimensions.ProductId         = ProductId;
                            dbProductImageWithDimensions.PrimaryImage      = false;
                            dbProductImageWithDimensions.SelectedForExport = true;
                            db.ProductImages.Add(dbProductImageWithDimensions);
                            db.SaveChanges();

                            var dbProductImageCropped = new ProductImage();
                            dbProductImageCropped.Id = Guid.NewGuid();

                            path = ImagePath + dbProductImageCropped.Id + "_crop" + fileName;

                            //imageDimensionResponse.CroppedImage.Save(path);

                            //Save with number
                            cvProvider.SaveImageWithProductNumber(new FileInfo(path), savedProduct.ProductNumber, imageDimensionResponse.CroppedImage);

                            dbProductImageCropped.ProductId         = ProductId;
                            dbProductImageCropped.PrimaryImage      = true;
                            dbProductImageCropped.Path              = "Images/" + Path.GetFileName(path);
                            dbProductImageCropped.ProductId         = ProductId;
                            dbProductImageCropped.PrimaryImage      = false;
                            dbProductImageCropped.SelectedForExport = true;
                            db.ProductImages.Add(dbProductImageCropped);
                            db.SaveChanges();

                            //Todo: Set Primary image
                            SetPrimaryImage(dbProductImageCropped.Id);

                            //Return cropped image
                            imagePath = dbProductImageCropped.Path;

                            Decimal width  = Math.Round((Decimal)imageDimensionResponse.Width);
                            Decimal height = Math.Round((Decimal)imageDimensionResponse.Height);
                            //productProperties = @"[{""Property"":""Höjd"",""Value"":""" + height.ToString() + @" cm""},{""Property"":""Bredd"",""Value"":""" + width.ToString() + @" cm""}]";
                            //Höjd: "panorama_vertical" Bredd: "panorama_horizontal"
                            productProperties = @"[{ ""Key"":""Height"",""Value"":""" + height.ToString() + @""",""Type"":""number"",""Icon"":""panorama_vertical""},{ ""Key"":""Width"",""Value"":""" + width.ToString() + @""",""Type"":""number"",""Icon"":""panorama_horizontal""}]";
                        }
                        else
                        {
                            productImageDTO.ErrorMessage = imageDimensionResponse.ErrorMessage;
                        }
                    }
                }
            }

            //Update Product information
            using (var db = new DitatEntities())
            {
                Product dbProduct = db.Products.Single(p => p.Id == ProductId);

                productImageDTO.Id   = dbProductImage.Id;
                productImageDTO.Path = imagePath;
                //productImageDTO.PrimaryImage = dbProductImage.PrimaryImage;

                productImageDTO.ProductProperties = productProperties;
                productImageDTO.Name     = ProductName;
                productImageDTO.Category = dbProduct.Category;

                if (ProductName != null || ProductName != "" || ProductName != String.Empty)
                {
                    if (ProductName == null)
                    {
                        ProductName = "";
                    }
                    if (ProductName.Length > 1)
                    {
                        dbProduct.Name = ProductName;
                        if (!HasProperties(dbProduct.Properties))
                        {
                            dbProduct.Properties = productProperties;
                        }
                    }
                }
                if (dbProductImage.Category != null)
                {
                    dbProduct.Category = dbProductImage.Category;//classifierResponse.Category;
                }

                if (categoryId > 0)
                {
                    dbProduct.CategoryId = categoryId;
                }

                //Fel:  Skriver över properties om man lägger till en odefinierad bil till en befintlig bok:
                if (productImageDTO.ErrorMessage == null || dbProduct.Properties == null)
                {
                    if (HasProperties(dbProduct.Properties) == false)
                    {
                        dbProduct.Properties = productProperties;
                    }
                }
                db.SaveChanges();
            }

            return(productImageDTO);
        }