public bool CreateNewProduct(ProductValueObject productValueObject, ImageValueObject image)
        {
            try
            {
                var listProduct = GetAllProduct();
                if (listProduct.Any(x => x.IdType == productValueObject.IdType && x.Name.Trim() == productValueObject.Name.Trim()))
                {
                    return(false);
                }
                _productDataAccessLayer.CreateNewProduct(productValueObject.Name, productValueObject.IdType, productValueObject.Price,
                                                         productValueObject.Description, productValueObject.Inew);

                listProduct = GetAllProduct();
                var insertedProduct =
                    listProduct.First(x => x.IdType == productValueObject.IdType && x.Name == productValueObject.Name);
                image.idSp = insertedProduct.Id;

                imageDataAccessLayer.CreateImage(image.link, image.idSp);
                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
        public bool UpdateProduct(ProductValueObject productValueObject, ImageValueObject image = null)
        {
            if (image == null)
            {
                return(_productDataAccessLayer.UpdateProduct(productValueObject.Id, productValueObject.Name,
                                                             productValueObject.IdType, productValueObject.Price, productValueObject.Description,
                                                             productValueObject.Inew));
            }

            var x = imageDataAccessLayer.UpdateImage(image.idSp, image.link);

            if (!x)
            {
                throw new Exception("Cannot update image.");
            }
            return(_productDataAccessLayer.UpdateProduct(productValueObject.Id, productValueObject.Name,
                                                         productValueObject.IdType, productValueObject.Price, productValueObject.Description,
                                                         productValueObject.Inew));
        }
Example #3
0
        public NewProductForm(int?rowId = null)
        {
            InitializeComponent();
            _productTypeBusinessLogic = new ProductTypeBusinessLogic();
            _productBusinessLogic     = new ProductBusinessLogic();


            var productTypes = _productTypeBusinessLogic.GetAllProductTypes();

            cmbProductType.DisplayMember = "Name";
            cmbProductType.ValueMember   = "Id";
            cmbProductType.DataSource    = productTypes;

            if (rowId == null)
            {
                return;
            }
            btn_NewProduct.Text = @"Cập nhật thay đổi";
            _isUpdate           = true;
            _rowId = (int)rowId;

            product = _productBusinessLogic.GetProductById(rowId);

            txtB_ProductName.Text         = product.Name;
            txtB_ProductPrice.Text        = product.Price.ToString();
            cmbProductType.SelectedValue  = product.IdType;
            chB_TopProduct.Checked        = product.Inew == 1;
            rTxtB_DescriptionProduct.Text = product.Description;

            try
            {
                var image = GetImage(rowId);
                picB_ImageProduct.Image     = image;
                picB_ImageProduct.SizeMode  = PictureBoxSizeMode.CenterImage;
                picB_ImageProduct.BackColor = Color.AliceBlue;
            }
            catch (Exception e)
            {
                // do not thing
            }
        }
Example #4
0
        private void btn_NewProduct_Click(object sender, EventArgs e)
        {
            var name = txtB_ProductName.Text.Trim();
            var des  = rTxtB_DescriptionProduct.Text.Trim();

            if (name == "" || des == "")
            {
                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;
            }
            uint price;

            uint.TryParse(txtB_ProductPrice.Text.Trim(), out price);
            if (price == 0)
            {
                MessageBox.Show("Giá bán phải là số nguyên không âm.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            var type  = int.Parse(cmbProductType.SelectedValue.ToString());
            var isNew = chB_TopProduct.Checked ? 1 : 0;

            var productValueObject = new ProductValueObject(_isUpdate ? _rowId : 0, name, type, price, des, isNew, 0, 0);

            using (var tran = new TransactionScope())
            {
                ImageValueObject image = null;
                if (isUpdateImage)
                {
                    const string actionUrl  = "http://localhost:4000/api/upload_image_product";
                    var          fileStream = File.OpenRead(_filePath);
                    string       fileNameInServer;
                    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();
                        }
                    }
                    image = new ImageValueObject(null, fileNameInServer, productValueObject.Id);
                }
                else
                {
                    if (_rowId == 0)//thêm mới sản phẩm
                    {
                        MessageBox.Show("Thêm mới sản phẩm phải có hình ảnh miêu tả", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }

                var success = _isUpdate ? _productBusinessLogic.UpdateProduct(productValueObject, image) : _productBusinessLogic.CreateNewProduct(productValueObject, image);
                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();
            }
        }
 public bool UpdateUnitInStock(ProductValueObject product)
 {
     return(_productDataAccessLayer.UpdateUnitInStock(product.Id, product.unitInStock));
 }
 public bool UpdateUnitOnBill(ProductValueObject product)
 {
     return(_productDataAccessLayer.UpdateUnitOnBill(product.Id, product.unitOnBill));
 }