예제 #1
0
        public Product Add(Product Product)
        {
            //Thêm mới Product vào database để sinh ra Product.ID để thêm vào bảng ProductTag phía dưới
            var product = _productRepository.Add(Product);

            _unitOfWork.Commit();
            if (!string.IsNullOrEmpty(Product.Tags))
            {
                string[] tags = Product.Tags.Split(',');
                for (var i = 0; i < tags.Length; i++)
                {
                    var tagId = StringHelper.ToUnsignString(tags[i]);
                    if (_tagRepository.Count(x => x.ID == tagId) == 0)
                    {
                        Tag tag = new Tag();
                        tag.ID   = tagId;
                        tag.Name = tags[i];
                        tag.Type = CommonConstants.ProductTag;
                        _tagRepository.Add(tag);
                    }

                    ProductTag productTag = new ProductTag();
                    productTag.ProductID = Product.ID;
                    productTag.TagID     = tagId;
                    _productTagRepository.Add(productTag);
                }
                //Lưu tất cả thay đổi vào csdl
                _unitOfWork.Commit();
            }
            return(product);
        }
예제 #2
0
        public Product Add(Product product)
        {
            var productResult = _productRepository.Add(product);
            var saveResult    = _uow.Commit();

            if (saveResult)
            {
                var tags = product.Tags.Split(',');
                foreach (var tag in tags)
                {
                    var unsignTag = StringHelper.ToUnsignString(tag);
                    var tagInDb   = _tagRepository.GetMulti(x => x.ID == unsignTag).FirstOrDefault();
                    if (tagInDb == null)
                    {
                        tagInDb = new Tag()
                        {
                            ID   = unsignTag,
                            Name = tag,
                            Type = TagTypeConstant.PRODUCT
                        };
                        _tagRepository.Add(tagInDb);
                    }

                    _productTagRepository.Add(new ProductTag()
                    {
                        TagID     = tagInDb.ID,
                        ProductID = productResult.ID
                    });
                }
                _uow.Commit();
                return(productResult);
            }
            return(null);
        }
예제 #3
0
        public Product Add(Product product)
        {
            product.CreatedDate = DateTime.Now;
            var addedProduct = _productRepository.Add(product);

            _unitOfWork.Commit();
            if (!string.IsNullOrEmpty(product.Tags))
            {
                string[] tags = product.Tags.Split(',');
                for (int i = 0; i < tags.Length; i++)
                {
                    var tagID = StringHelper.ToUnsignString(tags[i]);
                    if (_tagRepository.Count(p => p.ID == tagID) == 0)
                    {
                        Tag tag = new Tag()
                        {
                            ID   = tagID,
                            Name = tags[i],
                            Type = CommonConstants.ProductTag
                        };
                        _tagRepository.Add(tag);
                    }
                    ProductTag productTag = new ProductTag()
                    {
                        ProductID = addedProduct.ID,
                        TagID     = tagID
                    };
                    _productTagRepository.Add(productTag);
                }
            }

            return(addedProduct);
        }
예제 #4
0
        public Product Add(Product product)
        {
            var productReturn = _productRepository.Add(product);

            _unitOfWork.Commit();
            if (!string.IsNullOrEmpty(product.Tags))
            {
                string[] tags     = product.Tags.Split(',');
                var      length   = tags.Length;
                var      dataTags = _tagRepository.GetAll().ToList();
                for (var i = 0; i < length; i++)
                {
                    var tagId = StringHelperUtility.ToUnsignString(tags[i]);
                    if (!dataTags.Any(x => x.ID == tagId))
                    {
                        Tag tag = new Tag()
                        {
                            ID   = tagId,
                            Name = tags[i],
                            Type = Constant.ProductTag
                        };
                        _tagRepository.Add(tag);
                    }
                    ProductTag productTag = new ProductTag()
                    {
                        ProductID = productReturn.ID,
                        TagID     = tagId
                    };
                    _productTagRepository.Add(productTag);
                }
                _unitOfWork.Commit();
            }
            return(productReturn);
        }
예제 #5
0
        public Product Add(Product Product)
        {
            var product = _productRepository.Add(Product);

            _unitOfWork.Commit();
            if (!string.IsNullOrEmpty(Product.Tags))
            {
                string[] tags = Product.Tags.Split(',');
                for (var i = 0; i < tags.Length; i++)
                {
                    var tagId = StringHelper.ToUnsignString(tags[i]);
                    if (_tagRepository.Count(x => x.ID == tagId) == 0)
                    {
                        Tag tag = new Tag();
                        tag.ID   = tagId;
                        tag.Name = tags[i];
                        tag.Type = CommonConstants.ProductTag;
                        _tagRepository.Add(tag);
                    }

                    ProductTag productTag = new ProductTag();
                    productTag.ProductID = Product.ID;
                    productTag.TagID     = tagId;
                    _productTagRepository.Add(productTag);
                }
            }
            return(product);
        }
예제 #6
0
        public Product Add(Product Product)
        {
            var product = _ProductRepository.Add(Product);

            Save();
            if (!string.IsNullOrEmpty(Product.Tags))
            {
                string[] ListTags = Product.Tags.Split(',');
                foreach (var item in ListTags)
                {
                    var TagID = StringHelper.ToUnsignString(item);
                    if (_tagRepository.Count(s => s.ID == TagID) == 0)
                    {
                        Tag tag = new Tag();
                        tag.ID   = TagID;
                        tag.Name = item;
                        tag.Type = CommonConstants.ProductTag;
                        _tagRepository.Add(tag);
                    }
                    ProductTag productTag = new ProductTag();
                    productTag.ProductID = Product.ID;
                    productTag.TagID     = TagID;
                    _productTagRepository.Add(productTag);
                }
            }
            return(product);
        }
예제 #7
0
        public Product Add(Product Product)
        {
            var product = _productRepository.Add(Product);

            _unitOfWork.Commit();

            if (!String.IsNullOrEmpty(Product.Tags))
            {
                string[] tags = Product.Tags.Split(',');

                for (var i = 0; i < tags.Length; i++)
                {
                    // tagID in database with data type string.
                    var tagID = HelperMethods.ToUnsignString(tags[i]);

                    // Check string tag not in database will create it.
                    if (_tagRepository.Count(x => x.ID == tagID) == 0)
                    {
                        Tag tag = new Tag();
                        tag.ID   = tagID;
                        tag.Name = tags[i];
                        tag.Type = CommonConstants.ProductTag;
                        _tagRepository.Add(tag);
                    }

                    ProductTag productTag = new ProductTag();
                    productTag.ProductID = Product.ID;
                    productTag.TagID     = tagID;
                    _productTagRepository.Add(productTag);
                }
            }
            return(product);
        }
예제 #8
0
        public Product Add(Product Product)
        {
            var product = _productRepository.Add(Product);

            _unitOfWork.Commit();                        //commit lần 1 để có giá trị(Id, name,..) để truyền xuống if
            if (!string.IsNullOrEmpty(Product.Tags))     //nếu product.tag null or empty
            {
                string[] tags = Product.Tags.Split(','); //tách chuỗi nhập vào bằng dấu [,]
                for (var i = 0; i < tags.Length; i++)
                {
                    var tagId = StringHelper.ToUnsignString(tags[i]);  //chuyển đổi tag theo StringHelper
                    if (_tagRepository.Count(x => x.ID == tagId) == 0) //nếu = 0thì tạo mới tag
                    {
                        Tag tag = new Tag();
                        tag.ID   = tagId;
                        tag.Name = tags[i];
                        tag.Type = CommonConstants.ProductTag;
                        _tagRepository.Add(tag);
                    }
                    ProductTag productTag = new ProductTag();
                    productTag.ProductID = Product.ID;
                    productTag.TagID     = tagId;
                    _productTagRepository.Add(productTag);
                }
            }
            return(product);
        }
예제 #9
0
        public Product Add(Product product)
        {
            product.PromotionPrice = 0;
            var _product = _productRepository.Add(product);

            _unitOfWork.Commit();
            if (!string.IsNullOrEmpty(product.Tags))
            {
                string[] tags = product.Tags.Split(',');
                for (var i = 0; i < tags.Length; i++)
                {
                    var tagId = StringHelper.ToUnsignString(tags[i]);

                    if (_tagRepository.Count(x => x.ID == tagId) == 0)
                    {
                        Tag tag = new Tag
                        {
                            ID   = tagId,
                            Name = tags[i],
                            Type = CommonConstants.ProductTag
                        };
                        _tagRepository.Add(tag);
                    }

                    ProductTag productTag = new ProductTag
                    {
                        ProductID = product.ID,
                        TagID     = tagId
                    };
                    _productTagRepository.Add(productTag);
                }
            }
            return(_product);
        }
예제 #10
0
        public Product Add(Product product)
        {
            var productAdd = _productRepository.Add(product);

            _unitOfWork.Commit();
            if (!String.IsNullOrEmpty(product.Tags))
            {
                String[] tags = product.Tags.Split(',');
                foreach (var tagItem in tags)
                {
                    var tagId = StringHelper.ToUnsignString(tagItem);
                    if (_tagRepository.Count(x => x.ID == tagId) == 0)
                    {
                        var tag = new Tag();
                        tag.ID   = tagId;
                        tag.Name = tagItem;
                        tag.Type = CommonConstants.ProductTag;

                        _tagRepository.Add(tag);
                        _unitOfWork.Commit();
                    }

                    ProductTag productTag = new ProductTag();
                    productTag.ProductID = productAdd.ID;
                    productTag.TagID     = tagId;
                    _productTagRepository.Add(productTag);
                    _unitOfWork.Commit();
                }
            }
            return(productAdd);
        }
예제 #11
0
        public Product Add(Product product)
        {
            var lstStrIDTag = new List <string>();
            var addproduct  = _productRepository.Add(product);

            _unitOfWork.Commit();

            if (!string.IsNullOrEmpty(product.Tags))
            {
                string[] tags = product.Tags.Split(',');
                for (int i = 0; i < tags.Length; i++)
                {
                    var tagId = StringHelper.ToUnsignString(tags[i]);
                    var xx    = lstStrIDTag.Where(x => x.Equals(tagId));
                    if (xx.Count() == 0)
                    {
                        if (_tagRepository.Count(x => x.ID == tagId) == 0)
                        {
                            Tag tag = new Tag();
                            tag.ID   = tagId;
                            tag.Name = tags[i];
                            tag.Type = CommonConstants.ProductTag;
                            _tagRepository.Add(tag);
                            lstStrIDTag.Add(tagId);
                        }

                        ProductTag productTag = new ProductTag();
                        productTag.ProductID = product.ProductID;
                        productTag.TagID     = tagId;
                        _productTagRepository.Add(productTag);
                    }
                }
            }
            return(addproduct);
        }
예제 #12
0
        public Product Add(Product product)
        {
            Product query = _productRepository.Add(product);

            _unitOfWork.Commit();
            if (!string.IsNullOrEmpty(product.Tags))
            {
                string[] listTag = product.Tags.Split(',');
                for (int i = 0; i < listTag.Length; i++)
                {
                    var tagId = StringHelper.ToUnsignString(listTag[i]);
                    if (_tagReponsitory.Count(x => x.ID == tagId) == 0)
                    {
                        Tag tag = new Tag()
                        {
                            ID   = tagId,
                            Name = listTag[i],
                            Type = CommonConstant.ProductTag,
                        };
                        _tagReponsitory.Add(tag);
                    }
                    ProductTag productTag = new ProductTag()
                    {
                        ProductID = query.ID,
                        TagID     = tagId,
                    };
                    _productTagRepository.Add(productTag);
                }
            }
            return(query);
        }
예제 #13
0
 public void createProductTag(ProductTag obj)
 {
     try
     {
         ProductTagRepository.Add(obj);
         SaveChange();
     }
     catch
     {
     }
 }
예제 #14
0
        public Product Create(Product product)
        {
            var resultProduct = _productRepository.Add(product);

            _unitOfWork.Commit();

            if (!string.IsNullOrEmpty(product.Tags))
            {
                string[] strTag = product.Tags.Split(',');

                for (int i = 0; i < strTag.Length; i++)
                {
                    if (!string.IsNullOrEmpty(strTag[i]))
                    {
                        var tagID = StringHelper.ToUnsignString(strTag[i]);

                        if (_tagRepository.Count(x => x.ID == tagID) == 0)
                        {
                            var tag = new Tag();

                            tag.ID   = tagID;
                            tag.Name = strTag[i];
                            tag.Type = CommonConstants.ProductTag;

                            _tagRepository.Add(tag);
                        }

                        ProductTag productTag = new ProductTag();

                        productTag.ProductID = product.ID;
                        productTag.TagID     = tagID;

                        _productTagRepository.Add(productTag);
                    }
                }
            }

            return(resultProduct);
        }
예제 #15
0
        public Product Add(Product Product)
        {
            //thêm mới 1 sản phẩm
            var product = _ProductRepository.Add(Product);

            _unitOfWork.Commit();
            // lấy trường Tags bên sản phẩm đẩy quá bảng Tags
            if (!string.IsNullOrEmpty(Product.Tags))
            {
                string[] tags = Product.Tags.Split(',');

                for (var i = 0; i < tags.Length; i++)
                {
                    // chuyển đổi chuỗi ký tự bằng  static method  trong common
                    var tagID = StringHelper.ToUnsignString(tags[i]);
                    if (_tagRepository.Count(x => x.ID == tagID) == 0)
                    {
                        // tạo ra 1 đối tượng tag mới add vô bảng Tags
                        Tag tag = new Tag
                        {
                            ID   = tagID,
                            Name = tags[i],
                            Type = CommonConstants.ProductTag
                        };
                        _tagRepository.Add(tag);
                    }

                    // add tag vừa tạo add vào bảng productTags
                    ProductTag productTag = new ProductTag
                    {
                        ProductID = Product.ID,
                        TagID     = tagID
                    };
                    _productTagRepository.Add(productTag);
                }
            }
            return(product);
        }
예제 #16
0
 public ProductTag Add(ProductTag ProductTag)
 {
     return(_productTagRepository.Add(ProductTag));
 }
예제 #17
0
        public ActionResult Create(ProductViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var           ProductId        = Guid.NewGuid().ToString();
                    List <string> productImageList = ProcessUploadedFile(model);
                    _productRepository.Add(new Product()
                    {
                        Id            = ProductId,
                        ProductCode   = model.ProductCode,
                        ProductName   = model.ProductName,
                        Slug          = model.Slug,
                        Description   = model.Description,
                        ProductTypeId = model.ProductTypeId,
                        MaterialId    = model.MaterialId,
                        CategoryId    = model.CategoryId,
                        PriceType     = int.Parse(model.PriceType),
                        Price         = model.Price
                    });
                    _productRepository.Save(requestContext);

                    if (productImageList != null && productImageList.Count > 0)
                    {
                        foreach (var image in productImageList)
                        {
                            _productImageRepository.Add(new ProductImage()
                            {
                                Id        = Guid.NewGuid().ToString(),
                                ProductId = ProductId,
                                Url       = image,
                                CreateAt  = DateTime.UtcNow
                            });
                        }
                        _productImageRepository.Save(requestContext);
                    }
                    if (model.TagList != null && model.TagList.Count > 0)
                    {
                        foreach (var item in model.TagList)
                        {
                            _productTagRepository.Add(new ProductTag()
                            {
                                Id        = Guid.NewGuid().ToString(),
                                ProductId = ProductId,
                                TagId     = item
                            });
                        }
                    }
                    _productTagRepository.Save(requestContext);
                }
                catch (Exception)
                {
                    SetComboData();
                    return(View());
                }
                return(RedirectToAction("Index"));
            }
            SetComboData();
            return(View());
        }
예제 #18
0
        public async Task <IActionResult> Create(ProductViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var           productId        = Guid.NewGuid().ToString();
                    List <string> productImageList = ProcessUploadedFile(model);
                    await _productRepository.AddAsync(new Product
                    {
                        Id            = productId,
                        ProductCode   = model.ProductCode,
                        ProductName   = model.ProductName,
                        Slug          = model.Slug,
                        Description   = model.Description,
                        ProductTypeId = model.ProductTypeId,
                        MaterialId    = model.MaterialId,
                        CategoryId    = model.CategoryId,
                        PriceType     = int.Parse(model.PriceType),
                        Price         = model.Price,
                        IsFeatured    = model.IsFeatured,
                        IsNew         = model.IsNew,
                        Actived       = model.Actived,
                        Discount      = model.Discount,
                        ExtraDiscount = model.ExtraDiscount,
                        SeoAlias      = TextHelper.ToUnsignString(model.ProductName),
                        PriceAfter    = Math.Round((double)((1 - model.Discount / 100 - model.ExtraDiscount / 100) * model.Price.GetValueOrDefault()), 1,
                                                   MidpointRounding.AwayFromZero)
                    });

                    await _productRepository.SaveAsync(RequestContext);

                    _cache.Remove("CACHE_MASTER_PRODUCT");
                    if (productImageList != null && productImageList.Count > 0)
                    {
                        foreach (var image in productImageList)
                        {
                            _productImageRepository.Add(new ProductImage()
                            {
                                Id        = Guid.NewGuid().ToString(),
                                ProductId = productId,
                                Url       = image,
                                CreateAt  = DateTime.UtcNow
                            });
                        }
                        _productImageRepository.Save(RequestContext);
                    }
                    if (model.TagList != null && model.TagList.Count > 0)
                    {
                        foreach (var item in model.TagList)
                        {
                            _productTagRepository.Add(new ProductTag()
                            {
                                Id        = Guid.NewGuid().ToString(),
                                ProductId = productId,
                                TagId     = item
                            });
                        }
                    }
                    _productTagRepository.Save(RequestContext);
                }
                catch (Exception)
                {
                    SetComboData();
                    ViewBag.ProductCode = HttpContext.Session.GetString("ProductCode");
                    return(View());
                }
                HttpContext.Session.Remove("ProductCode");
                return(RedirectToAction("Index"));
            }
            SetComboData();
            ViewBag.ProductCode = HttpContext.Session.GetString("ProductCode");
            return(View());
        }
예제 #19
0
 public void Add(ProductTag productTag)
 {
     _productTagRepository.Add(productTag);
 }