public async Task <AffiliateProduct> SaveAffiliateProduct(AffiliateProduct affiliateProduct)
        {
            if (affiliateProduct.AffiliateProductID == 0)
            {
                _context.AffiliateProducts.Add(affiliateProduct);
                Save();
                return(affiliateProduct);
            }
            var dbEntry = await _context.AffiliateProducts.FindAsync(affiliateProduct.AffiliateProductID);

            if (dbEntry != null)
            {
                dbEntry.AffiliateProductID = affiliateProduct.AffiliateProductID;
                dbEntry.AffiliateName      = affiliateProduct.AffiliateName;
                dbEntry.Approved           = affiliateProduct.Approved;
                dbEntry.Category           = affiliateProduct.Category;
                dbEntry.Description        = affiliateProduct.Description;
                dbEntry.Name = affiliateProduct.Name;
                dbEntry.PendingAdminApproval = affiliateProduct.PendingAdminApproval;
                dbEntry.Picture  = affiliateProduct.Picture;
                dbEntry.Price    = affiliateProduct.Price;
                dbEntry.Quantity = affiliateProduct.Quantity;
                dbEntry.Size     = affiliateProduct.Size;
                dbEntry.Unit     = affiliateProduct.Unit;
            }

            Save();
            return(dbEntry);
        }
Exemplo n.º 2
0
        public ActionResult EditAffiliateProduct(AffiliateProduct affiliateProduct)
        {
            if (ModelState.IsValid)
            {
                CheckAffiliateData(affiliateProduct);

                ViewBag.Message = $"Edit {affiliateProduct.Name} success";
                _apRepository.SaveAffiliateProduct(affiliateProduct);
                return(View(affiliateProduct));
            }
            ViewBag.Message = ("Something went wrong");
            return(View(affiliateProduct));
        }
Exemplo n.º 3
0
        private void CheckAffiliateData(AffiliateProduct affiliateProduct)
        {
            if (!string.IsNullOrEmpty(affiliateProduct.AffiliateName) && !string.IsNullOrEmpty(affiliateProduct.AffiliateId))
            {
                return;
            }

            var affiliateUser = _aRepository.GetAffiliateByUserId(User.Identity.GetUserId());

            if (string.IsNullOrEmpty(affiliateProduct.AffiliateName))
            {
                affiliateProduct.AffiliateName = affiliateUser.AffiliateName;
            }
            if (string.IsNullOrEmpty(affiliateProduct.AffiliateId))
            {
                affiliateProduct.AffiliateId = affiliateUser.AffiliateId;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Imports products for an affiliate
        /// </summary>
        public void ImportProducts(int affiliateId, Stream stream)
        {
            // NOTE: This is hardcoded for Midwest for now
            // NOTE: If we get more affiliartes (and we should)
            // NOTE: This should use a factory and parser classes/methods

            // Func to Get Node Value
            Func <XElement, string, string> getValueFunc = ((XElement node, string name) =>
            {
                var match = node.Elements().FirstOrDefault(x => x.Name.LocalName == name);
                if (match == null)
                {
                    return(null);
                }

                return(match.Value);
            });

            // Get Existing Products
            var products = this.Repository.GetSet <AffiliateProduct>();

            // Get Xml from Stream and Parse as XML
            var xmlContent = new StreamReader(stream).ReadToEnd();
            var xDoc       = XDocument.Parse(xmlContent);

            var productNodes = xDoc.Elements().FirstOrDefault(x => x.Name.LocalName == "catalog")
                               .Elements().Where(x => x.Name.LocalName == "product");

            // Add and Update based on feed content
            foreach (var productNode in productNodes)
            {
                var sku          = getValueFunc(productNode, "sku");
                var dateModified = DateTime.Parse(getValueFunc(productNode, "lastupdated"));

                var product = new AffiliateProduct();

                // Check Existing Products
                product = products.FirstOrDefault(x => x.Sku == sku);

                if (product != null && dateModified <= (product.DateModified ?? DateTime.MinValue))
                {
                    continue;
                }

                // Doesn't Exist, new it up (otherwise set modify date)
                if (product == null)
                {
                    product             = new AffiliateProduct();
                    product.DateCreated = DateTime.Now;
                    product.AffiliateId = affiliateId;

                    this.Repository.Add(product);
                }
                else
                {
                    product.DateModified = DateTime.Now;
                }

                // Set Values
                product.DateModified = dateModified;
                product.Name         = getValueFunc(productNode, "name");

                // Only set Description if it is a new product
                if (product.AffiliateProductId <= 0)
                {
                    product.Description = getValueFunc(productNode, "description");
                }

                product.Sku      = getValueFunc(productNode, "sku");
                product.Price    = Decimal.Parse(getValueFunc(productNode, "price"));
                product.Url      = getValueFunc(productNode, "buyurl");
                product.ImageUrl = getValueFunc(productNode, "imageurl");
                product.Category = getValueFunc(productNode, "advertisercategory");
                product.InStock  = getValueFunc(productNode, "instock") == "YES";
            }

            // Deactivate Anything Not in the Feed Anymore
            foreach (var product in products)
            {
                if (productNodes.Select(x => x.Elements().FirstOrDefault(y => y.Name.LocalName == "sku").Value)
                    .FirstOrDefault(x => x == product.Sku) == null)
                {
                    product.IsActive     = false;
                    product.DateModified = DateTime.Now;
                }
            }
        }