Exemple #1
0
        public string AddProduct(IList <string> parameters)
        {
            string            productName;
            decimal           price;
            string            category;
            PaymentMethodEnum paymentMethod;
            decimal           shippingDetailsCost = 0;
            int     shippingDetailsDeliveryTIme   = 0;
            decimal priceReduction;
            bool    shippingDetailsInUse = true;

            try
            {
                //validation
                productName = parameters[0].ToLower();
                if (this.context.Products.Any(x => x.ProductName == productName))
                {
                    return("Product already exists.");
                }
                price = decimal.Parse(parameters[1]);
                if (price < 0)
                {
                    throw new ArgumentException("Price cannot be negative.");
                }
                category      = parameters[2].ToLower();
                paymentMethod = (PaymentMethodEnum)Enum.Parse(typeof(PaymentMethodEnum), parameters[3], true);
                if ((int)paymentMethod <= -1 || (int)paymentMethod >= 6)
                {
                    throw new ArgumentOutOfRangeException("Invalid payment method.");
                }

                if (parameters[4] != "-1" && parameters[5] != "-1")
                {
                    shippingDetailsCost = decimal.Parse(parameters[4]);
                    if (price < 0)
                    {
                        throw new ArgumentException("Shipping cost cannot be negative.");
                    }
                    shippingDetailsDeliveryTIme = int.Parse(parameters[5]);
                    if (price < 0)
                    {
                        throw new ArgumentException("Delivery time cannot be negative.");
                    }
                }
                else
                {
                    shippingDetailsInUse = false;
                }

                priceReduction = decimal.Parse(parameters[6]);
                if (price < 0)
                {
                    throw new ArgumentException("Price reduction cannot be negative.");
                }
            }
            catch
            {
                return("One or more parameters for the AddProduct command are invalid.");
            }

            Product product = this.modelFactory.CreateProduct();

            product.ProductName   = productName;
            product.Price         = price;
            product.Date          = DateTime.Now;
            product.PaymentMethod = paymentMethod;
            product.Instock       = true;
            product.SellerId      = this.loggedUserProvider.CurrentUserId;

            if (this.context.Categories.Any(x => x.CategoryName == category))
            {
                product.Categories.Add(this.context.Categories.First(x => x.CategoryName == category));
            }
            else
            {
                Category newCategory = this.modelFactory.CreateCategory();
                newCategory.CategoryName = category;
                product.Categories.Add(newCategory);
            }

            if (shippingDetailsInUse)
            {
                ShippingDetails newShippingDetails = modelFactory.CreateShippingDetails();
                newShippingDetails.DeliveryTime = shippingDetailsDeliveryTIme;
                newShippingDetails.Cost         = shippingDetailsCost;
                product.ShippingDetails         = newShippingDetails;
            }

            if (priceReduction != -1)
            {
                Sale newSale = this.modelFactory.CreateSale();
                newSale.PriceReduction = priceReduction;
                product.Sale           = newSale;
            }

            //this.context.Sellers.Single(s => s.UserId == this.loggedUserProvider.CurrentUserId)
            //  .Products.Add(product);

            context.Products.Add(product);
            context.SaveChanges();

            this.writer.CleanScrean();
            return("Product added succesfully.");
        }