public bool UpdateProductType(ProductTypeValueObject productType)
 {
     if (productType.Image == "")
     {
         return(_productTypeDataAccessLayer.UpdateProductTypeNotImage(productType.Id, productType.Name));
     }
     return(_productTypeDataAccessLayer.UpdateProductType(productType.Id, productType.Name, productType.Image));
 }
 public bool CreateProductType(ProductTypeValueObject productType)
 {
     try
     {
         var listProductType = GetAllProductTypes();
         if (listProductType.Any(x => x.Name.Trim() == productType.Name.Trim()))
         {
             return(false);
         }
         return(_productTypeDataAccessLayer.CreateNewProductType(productType.Name, productType.Image));
     }
     catch (Exception)
     {
         return(false);
     }
 }
示例#3
0
        private void btn_InsertProductType_Click(object sender, EventArgs e)
        {
            var name = txtB_NameProductType.Text.Trim();

            if (name == "")
            {
                MessageBox.Show("Xin điền đầy đủ thông tin trước khi thực hiện thao tác khác", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            using (var tran = new TransactionScope())
            {
                string fileNameInServer = "";
                if (isUpdateImage)
                {
                    const string actionUrl         = "http://localhost:4000/api/upload_image_productType";
                    var          fileStream        = File.OpenRead(_filePath);
                    HttpContent  fileStreamContent = new StreamContent(fileStream);
                    using (var client = new HttpClient())
                    {
                        using (var formData = new MultipartFormDataContent())
                        {
                            formData.Add(fileStreamContent, "image", "image.jpg");
                            var response = client.PostAsync(actionUrl, formData).Result;

                            var          receiveStream = response.Content.ReadAsStreamAsync().Result;
                            StreamReader readStream    = new StreamReader(receiveStream, Encoding.UTF8);

                            var     message = readStream.ReadToEnd();
                            dynamic stuff   = JsonConvert.DeserializeObject(message);
                            string  mess    = stuff.msg.ToString();
                            if (mess.Equals("Error"))
                            {
                                MessageBox.Show("Có lỗi tải hình lên, xin thử lại sau.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return;
                            }
                            fileNameInServer = stuff.link.ToString();
                        }
                    }
                }
                else
                {
                    if (_rowId == 0)//thêm mới sản phẩm
                    {
                        MessageBox.Show("Thêm mới loại sản phẩm phải có hình ảnh miêu tả", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }

                ProductTypeValueObject productTypeValueObject = new ProductTypeValueObject(_isUpdate ? _rowId : 0, name, fileNameInServer);
                var success = _isUpdate ? _productTypeBusinessLogic.UpdateProductType(productTypeValueObject) : _productTypeBusinessLogic.CreateProductType(productTypeValueObject);
                if (success)
                {
                    MessageBox.Show("Cật nhật thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Có gì đó không đúng, có thể dữ liệu đã có trong cơ sở dữ liệu", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                tran.Complete();
            }
        }