private void addProduct()
        {
            ProductInfo pro = new ProductInfo();

            pro.title               = txtTitle.Text;
            pro.description         = txtDesciption.Text;
            pro.price               = float.Parse(txtPrice.Text);
            pro.amount              = int.Parse(txtAmount.Text);
            pro.product_category_id = ddlCategory.SelectedValue;
            pro.create_by           = (SessionApp.user_info == null) ? "No Login" : SessionApp.user_info.user_name;
            var result = ProductController.AddProduct(pro);

            if (result == null)
            {
                ShowMessage(Page, "ชื่อสินค้าในหมวดหมู่นี้มีอยู่ในระบบแล้ว");
            }
            else
            {
                foreach (var current in fileImage.PostedFiles)
                {
                    string exttension   = System.IO.Path.GetExtension(current.FileName);
                    string newNameImage = Guid.NewGuid().ToString();
                    current.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Images/"), newNameImage + exttension));
                    listofuploadedfiles.Text += String.Format("{0}<br />", newNameImage + exttension);

                    ProductController.AddProductImage(result.product_id, newNameImage + exttension, result.create_by);
                }
                ShowMessage(Page, "เพิ่มหมวดหมู่สำเร็จ");
            }
        }
        public async Task AddProductImage_InitializedNegatively_ReturnsInternalServerError()
        {
            // Arrange
            var formFileMock = new Mock <IFormFile>();

            var productController = new ProductController(_loggerMock.Object, _docService.Object, _blobService.Object);

            // Act
            var result = await productController.AddProductImage(formFileMock.Object);

            // Assert
            var codeRsult = result as StatusCodeResult;

            Assert.AreEqual(500, codeRsult.StatusCode);
        }
        public async Task AddProductImage_InitializedSuccessfully_ReturnsOkay()
        {
            // Arrange
            _blobService.Setup(x => x.UploadBlobAsync(It.IsAny <string>(), It.IsAny <Stream>())).ReturnsAsync("some string");
            _docService.Setup(x => x.AddImageToProductAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Returns(Task.CompletedTask);

            var formFileMock = new Mock <IFormFile>();
            var stream       = new MemoryStream();

            formFileMock.Setup(x => x.OpenReadStream()).Returns(stream);

            var routeData = new RouteData();

            routeData.Values.Add("id", "123");
            routeData.Values.Add("camera", "Diana");

            var requestMock = new Mock <HttpRequest>();

            requestMock.Setup(x => x.HasFormContentType).Returns(true);
            Mock <HttpContext> httpContextMock = new Mock <HttpContext>();

            httpContextMock.Setup(x => x.Request).Returns(requestMock.Object);

            var controllerContext = new ControllerContext()
            {
                HttpContext = httpContextMock.Object, RouteData = routeData
            };

            var productController = new ProductController(_loggerMock.Object, _docService.Object, _blobService.Object)
            {
                ControllerContext = controllerContext
            };


            // Act
            var result = await productController.AddProductImage(formFileMock.Object);

            // Assert
            var okResult = result as OkResult;

            Assert.AreEqual(200, okResult.StatusCode);
        }
        private void setProduct()
        {
            ProductInfo pro = new ProductInfo();

            pro.product_id          = product_id;
            pro.title               = txtTitle.Text;
            pro.description         = txtDesciption.Text;
            pro.price               = float.Parse(txtPrice.Text);
            pro.amount              = int.Parse(txtAmount.Text);
            pro.product_category_id = ddlCategory.SelectedValue;
            pro.lastupdate_by       = (SessionApp.user_info == null) ? "No Login" : SessionApp.user_info.user_name;

            var result = ProductController.SetProduct(pro);

            if (result == null)
            {
                ShowMessage(Page, "แก้ไขผิดพลาด");
            }
            else
            {
                foreach (RepeaterItem item in rptProductImages.Items)
                {
                    FileUpload file = (FileUpload)item.FindControl("fileItemImage");

                    TextBox txtImageId   = (TextBox)item.FindControl("txtImageId");
                    TextBox txtImageName = (TextBox)item.FindControl("txtImageName");
                    //updateImage
                    if (file.HasFile)
                    {
                        string exttension   = System.IO.Path.GetExtension(file.FileName);
                        string newNameImage = Guid.NewGuid().ToString();
                        file.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Images/"), newNameImage + exttension));

                        //File.Delete(System.IO.Path.Combine(Server.MapPath("~/Images/"), txtImageName.Text));

                        ProductImageInfo proi = new ProductImageInfo();
                        proi.image_id      = txtImageId.Text;
                        proi.product_id    = result.product_id;
                        proi.image         = newNameImage + exttension;
                        proi.lastupdate_by = result.lastupdate_by;
                        ProductController.SetProductImage(proi);
                    }
                }
                //add new Image
                if (fileImage.HasFiles)
                {
                    foreach (var current in fileImage.PostedFiles)
                    {
                        string exttension   = System.IO.Path.GetExtension(current.FileName);
                        string newNameImage = Guid.NewGuid().ToString();
                        current.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Images/"), newNameImage + exttension));
                        listofuploadedfiles.Text += String.Format("{0}<br />", newNameImage + exttension);

                        ProductController.AddProductImage(result.product_id, newNameImage + exttension, result.create_by);
                    }
                }
                bindProduct();
                bindProductImage();
                ShowMessage(Page, "แก้ไขสำเร็จ");
            }
        }